[英]Excel VBA: Cannot delete multi columns by using if statement and for loop
例如:如果列名=“名字”,“姓氏”,“ DoB”,“性別”,“年份”,則刪除整個列。
我使用vba來做到這一點。
這是我的代碼
Sub SPO_EditDocument_BUttonClick()
'Declare constant variable
Dim CellValue As String
'Edit SPO Documents.
'Consider worksheet"SPO"
With Worksheets("Sheet1")
'1. Loop and delete unnecessary columns
LastCol = Sheets("Sheet1").Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
For i = 1 To LastCol
On Error Resume Next
CellValue = Sheets("Sheet1").Cells(1, i) 'Get Column header
If CellValue = "Firstname" Then
Sheets("Sheet1").Columns(i).EntireColumn.Delete
ElseIf CellValue = "Surname" Then
Sheets("Sheet1").Columns(i).EntireColumn.Delete
ElseIf CellValue = "DoB" Then
Sheets("Sheet1").Columns(i).EntireColumn.Delete
ElseIf CellValue = "Gender" Then
Sheets("Sheet1").Columns(i).EntireColumn.Delete
ElseIf CellValue = "Year" Then
Sheets("Sheet1").Columns(i).EntireColumn.Delete
End If
Next i
End With 'End to process Worksheets
Application.ScreenUpdating = True
End Sub
任何幫助將不勝感激。 非常感謝您的關注。
以相反的順序遍歷各列。 為此,請更改行:
For i = 1 To LastCol
至:
For i = LastCol to 1 Step -1
另一種快速的方法
Option Explicit
Sub SPO_EditDocument_BUttonClick()
Dim colNames As Variant, colName As Variant, actualColNames As Variant, foundColName As Variant
Dim colsToDelete As String
colNames = Array("Firstname", "Surname", "DoB", "Gender", "Year") ' build a list of column headers to be deleted
With Worksheets("Sheet1") ' reference data sheet
actualColNames = Application.Index(.Range("A1", .Cells(1, .Columns.count).End(xlToLeft)).Value, 1, 0) ' collect referenced sheet actual column headers
For Each colName In colNames ' loop through your column headers to be deleted list
foundColName = Application.Match(colName, actualColNames, 0) ' try finding current header to be deleted in actual headers list
If Not IsError(foundColName) Then colsToDelete = colsToDelete & Columns(foundColName).Address(False, False) & " " 'if found update columns to be deleted index list
Next
If colsToDelete <> "" Then .Range(Replace(Trim(colsToDelete), " ", ",")).EntireColumn.Delete ' if columns to be deleted index list not empty, then delete thsoe columns
End With
刪除多個列的一種更快的方法是使用Range
對象,在我的代碼中是DelRng
,它使用Union
函數合並通過要刪除的所有符合條件的Columns
。 因此,最后,您只需使用Delete
函數,該函數需要很長時間才能處理一次。
您可以用Select Case
替換多個ElseIf
。
嵌套在With Worksheets("Sheet1")
所有對象無需再次具有With Worksheets("Sheet1")
,只需使用即可.
作為所有Cells
和Range
對象的前綴。
您可以使用更短,更快的代碼版本,例如以下代碼:
修改后的代碼
Sub SPO_EditDocument_BUttonClick()
'Declare constant variable
Dim CellValue As String
Dim DelRng As Range
'Edit SPO Documents.
'Consider worksheet"SPO"
Application.ScreenUpdating = False
With Worksheets("Sheet1")
'1. Loop and delete unnecessary columns
LastCol = .Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
For i = 1 To LastCol
CellValue = .Cells(1, i) 'Get Column header
Select Case CellValue
Case "Firstname", "Surname", "DoB", "Gender", "Year"
If Not DelRng Is Nothing Then
Set DelRng = Application.Union(DelRng, .Columns(i))
Else
Set DelRng = .Columns(i)
End If
Case Else
' Do nothing
End Select
Next i
End With 'End to process Worksheets
' if there's at least 1 column in DelRng >> delete the entire range (all columns) in 1-shot
If Not DelRng Is nothign Then DelRng.Delete
Application.ScreenUpdating = True
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.