如果范围为空则删除列

Delete column if range is blank

我正在寻找有关构建宏以根据范围是否为空删除列的指南。我在定义我的范围时遇到问题,因此它会随着每个循环而变化,我是 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

试试这个

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