有没有办法使用 VBA 将 excel 中的单元格向左移动?
Is there a way to shift over cells to the left in excel using VBA?
有没有办法计算从年份到姓氏的每一列中的值,如果计数为 0,则将右侧的列移到它的位置?例如,列 2020 没有值。我可以将 2021 年和任何后续列移到左侧吗?我附上了一张前后图像来展示我想要完成的事情。有人能指出我正确的方向吗?谢谢!
开始 - https://ibb.co/WfQ91D4
完成 - https://ibb.co/Jv6gf7Q
删除空白数据(整个)列
Sub RemoveBlankDataColumns()
Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
Dim rg As Range: Set rg = ws.Range("A1").CurrentRegion
Dim rCount As Long: rCount = rg.Rows.Count - 1
Dim drg As Range: Set drg = rg.Resize(rCount).Offset(1)
Dim rgDel As Range
Dim crg As Range
For Each crg In drg.Columns
If Application.CountBlank(crg) = rCount Then
If rgDel Is Nothing Then
Set rgDel = crg
Else
Set rgDel = Union(rgDel, crg)
End If
End If
Next crg
If rgDel Is Nothing Then
MsgBox "No blank columns found.", vbExclamation
Else
rgDel.EntireColumn.Delete
MsgBox "Blank data columns removed.", vbInformation
End If
End Sub
有没有办法计算从年份到姓氏的每一列中的值,如果计数为 0,则将右侧的列移到它的位置?例如,列 2020 没有值。我可以将 2021 年和任何后续列移到左侧吗?我附上了一张前后图像来展示我想要完成的事情。有人能指出我正确的方向吗?谢谢!
开始 - https://ibb.co/WfQ91D4 完成 - https://ibb.co/Jv6gf7Q
删除空白数据(整个)列
Sub RemoveBlankDataColumns()
Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
Dim rg As Range: Set rg = ws.Range("A1").CurrentRegion
Dim rCount As Long: rCount = rg.Rows.Count - 1
Dim drg As Range: Set drg = rg.Resize(rCount).Offset(1)
Dim rgDel As Range
Dim crg As Range
For Each crg In drg.Columns
If Application.CountBlank(crg) = rCount Then
If rgDel Is Nothing Then
Set rgDel = crg
Else
Set rgDel = Union(rgDel, crg)
End If
End If
Next crg
If rgDel Is Nothing Then
MsgBox "No blank columns found.", vbExclamation
Else
rgDel.EntireColumn.Delete
MsgBox "Blank data columns removed.", vbInformation
End If
End Sub