[英]Delete entire blank columns
Need to delete blank columns in a worksheet.需要删除工作表中的空白列。 I need the whole column delete sho it move the imported columns with text into in to be at the start of the sheet.
我需要整个列删除,它将带有文本的导入列移动到工作表的开头。 The placement of the columns with text in them will change weekly so I need something that can delete full columns until the last text column.
带有文本的列的位置每周都会改变,所以我需要一些可以删除完整列的东西,直到最后一个文本列。 Code I was trying is listed below.
下面列出了我正在尝试的代码。
Sub Delete_Columns
C = ActiveSheet.Cells.SpecialCells(xlLastCell).Column
Do Until C = 0
If WorksheetFunction.CountA(Columns(C)) = 0 Then
Columns(C).Delete
End If
C = C - 1
Loop
End Sub
Option Explicit
Sub DeleteBlankColumns()
Dim ws As Worksheet: Set ws = ActiveSheet
Dim rg As Range: Set rg = ws.UsedRange
Dim rCount As Long: rCount = rg.Rows.Count
Dim dcrg As Range
Dim crg As Range
For Each crg In rg.Columns
If Application.CountBlank(crg) = rCount Then
If dcrg Is Nothing Then
Set dcrg = crg
Else
Set dcrg = Union(dcrg, crg)
End If
End If
Next crg
If dcrg Is Nothing Then Exit Sub
dcrg.EntireColumn.Delete
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.