ゆるいSEのIT徒然草

SE向けナレッジ(ほぼ自分用メモ)。技術以外にも、システム関連のもろもろ雑多なノウハウを公開します。内容保証しません!!!

VBAサンプル

Sub A列からEG列の情報をEG列に寄せる()
'
' A列からEG列の情報をEG列に寄せる
' 使用方法:本SubをThisWorkbookに貼り付けて実行する。
'
Dim fromCol As Integer
Dim toCol As Integer
Dim row As Long

'各シートでループ
For Each sht In Sheets
    sht.Activate
    'MsgBox (sht.Name)
    If sht.Name <> "Sheet1 (2)" Then
        '各行のループ
        row = 2
        Do Until Cells(row, 138).Value = ""    '138:EH列
            fromCol = 137  '137:EG列
            toCol = 137
            'EG列からA列までループ
            Do While fromCol > 100  '1~100列目は対象外
                'MsgBox ("fromCol:" & fromCol & ",toCol:" & toCol & ",Value:" & Cells(i, fromCol).Value)
                If Cells(row, fromCol).Value <> "" Then
                    If fromCol <> toCol Then
                        'セルの移動
                        'Cells(row, toCol).Value = Cells(row, fromCol).Value
                        Cells(row, fromCol).Cut
                        Cells(row, toCol).Select
                        ActiveSheet.Paste
                        'If toCol <> fromCol Then
                        '    Cells(row, fromCol).Value = ""
                        'End If
                    End If
                    toCol = toCol - 1
                End If
                fromCol = fromCol - 1
            Loop
            'スタイルのクリア
            Range(Cells(row, 1), Cells(row, toCol)).Select
            Selection.Interior.ColorIndex = xlNone
            Selection.Borders.LineStyle = xlNone
            row = row + 1
        Loop
        MsgBox ("end sheet:" & sht.Name & ",行数:" & row)
    End If
Next
End Sub