LoginSignup
0
0

参照用

Posted at

'''

Sub RemoveSpacesFromFirstRowAndSpecificColumn()
Dim ws As Worksheet
Dim cell As Range
Dim targetColumn As Integer

For Each ws In ThisWorkbook.Worksheets
    ' 1行目の全てのセルに対してスペース削除
    For Each cell In ws.Range(ws.Cells(1, 1), ws.Cells(1, ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column))
        cell.Value = Replace(cell.Value, " ", "")
        cell.Value = Replace(cell.Value, Chr(12288), "")
    Next cell

    ' "取引先名"列を見つける
    For i = 1 To ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
        If ws.Cells(1, i).Value = "取引先名" Then
            targetColumn = i
            Exit For
        End If
    Next i

    ' "取引先名"列の各セルに対してスペース削除
    If targetColumn > 0 Then
        For Each cell In ws.Range(ws.Cells(2, targetColumn), ws.Cells(ws.Rows.Count, targetColumn).End(xlUp))
            cell.Value = Replace(cell.Value, " ", "")
            cell.Value = Replace(cell.Value, Chr(12288), "")
        Next cell
    End If
Next ws

End Sub

'''

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0