VBA循环删除多个列

时间:2016-05-25 17:40:25

标签: excel vba

我想搜索/遍历打开文件的第1行上的所有列标题,如果它匹配dColumns则删除它,dColumns是我不需要的列列表,我放入一个范围。

Sub LLextract()

'Last cell in column
Dim WS As Worksheet
Dim LastCell As Range
Dim LastCellRowNumber As Long

Set WS = ThisWorkbook.Worksheets("Consolidated Data")
With WS
    Set LastCell = .Cells(.Rows.Count, "A").End(xlUp)
    LastCellRowNumber = LastCell.Row + 0
End With

Dim wb As Workbook, wb2 As Workbook
Dim vFile As Variant

'Set source workbook
Set wb = ActiveWorkbook

'Open the target workbook
vFile = Application.GetOpenFilename("CSV Files (*.csv), *.csv", , _
      "Select a CSV file", , False)

'if the user didn't select a file, exit sub
If TypeName(vFile) = "Boolean" Then Exit Sub
Workbooks.Open vFile

'Set selectedworkbook
Set wb2 = ActiveWorkbook

Dim dColumns As Range
Set dColumns = wb.Worksheets("LL Columns to Delete").Range("A:A")

    Dim i As Integer
    Dim A As Range
    For i = 94 To 1 Step -1
        Set A = wb2.Cells(1, i)
        If wb2.Cells(1, i) = dColumns Then A.EntireColumn.Delete
    Next i

'wb2.Worksheets(1).Range("A1").Select

End Sub

2 个答案:

答案 0 :(得分:3)

您不能只使用Range("A")替换Range("A:A")

(但你想用dColumns做什么?)

答案 1 :(得分:0)

我打开wb2时只删除了列就解决了这个问题。这个问题不再需要回答或解决。