我希望找到有关构建宏以删除列的指导,具体取决于范围是否为空。我无法定义我的范围,所以它随着每个循环而变化,我是VBA的新手,因此设置此范围的信息将有助于此代码和未来构建代码!
Sub sbDelete_Columns_IF_Cell_Is_empty()
Dim lColumn As Long
Dim iCntr As Long
lColumn = 103
For iCntr = lColumn To 1 Step -1
If IsEmpty(Range()
Columns(iCntr).Delete
End If
Next
End Sub
第二次尝试,这很奇怪并删除了我不想删除的列
Sub sbDelete_Columns_IF_Cell_Is_Blank()
Dim lColumn As Long
Dim iCntr As Long
Dim i As Integer
Dim f As Integer
i = 4
f = 100
lColumn = 103
For iCntr = lColumn To 1 Step -1
If IsEmpty(Range(Cells(f, i), (Cells(f, i)))) = True Then
Columns(iCntr).Delete
End If
Next
End Sub
答案 0 :(得分:2)
试试这个
Sub sbDelete_Columns_IF_Cell_Is_empty()
Dim lColumn As Long
Dim iCntr As Long
Dim ws As Worksheet
Dim rwsToCheck As Long
Set ws = ActiveSheet
rwsToCheck = 1000 'Change to what ever you want
lColumn = 103
For iCntr = lColumn To 1 Step -1
If WorksheetFunction.CountA(ws.Range(ws.Cells(4, iCntr), ws.Cells(rwsToCheck, iCntr))) = 0 Then
ws.Columns(iCntr).Delete
End If
Next
End Sub