2

I was trying to write a macro that performed the following:

For every sheet in the workbook: 1. Select a column 2. Search in the selected column for a specific value in the Row number 5 3. If that value matches another cel (A1), then Insert two columns before the selected column

  1. Perform the same for every column in the sheet. (i mean, those columns where there is something, I know that there are an infinite amount of potential columns).

Any help or direction toward an answer would be more than helpful.

4

1 回答 1

4

我想,这会让你得到你所追求的。

Dim wks As Worksheet

For Each wks In ThisWorkbook.Worksheets

    With wks
        Dim intCol As Integer, intCnt As Integer

        intCol = .UsedRange.Columns.Count

        For intCnt = intCol To 2 Step -1 'assumes you ignore col A since your match value is there
            If .Cells(5, intCnt) = .Cells(1, 1) Then
                .Range(.Cells(1, intCnt), .Cells(1, intCnt + 1)).EntireColumn.Insert Shift:=xlToLeft
            End If
        Next

    End With

Next
于 2012-05-18T19:44:40.647 回答