vba Excel 宏:如何在检查列时忽略标题行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13830280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:46:41 来源:igfitidea点击:
Excel macro: how to ignore header row when checking columns
提问by Rob Bednark
This HideEmptyColumns()
macro hides all columns that don't have any data. How do I modify it so that it ignores the header row (row 1)?
此HideEmptyColumns()
宏隐藏所有没有任何数据的列。如何修改它以忽略标题行(第 1 行)?
Sub HideEmptyColumns()
Dim Col As Range
For Each Col In ActiveSheet.UsedRange.Columns
Col.EntireColumn.Hidden = RangeIsEmpty(Col)
Next Col
End Sub
Function RangeIsEmpty(R As Range) As Boolean
Dim Cell As Range
RangeIsEmpty = True
For Each Cell In R.Cells
If Cell.Value <> "" Then
RangeIsEmpty = False
Exit For
End If
Next Cell
End Function
回答by Sorceri
Just check to see what row its on
只需检查它在哪一行
Sub HideEmptyColumns()
Dim Col As Range
For Each Col In ActiveSheet.UsedRange.Columns
Col.EntireColumn.Hidden = RangeIsEmpty(Col)
Next Col
End Sub
Function RangeIsEmpty(R As Range) As Boolean
Dim Cell As Range
RangeIsEmpty = True
For Each Cell In R.Cells
'are we not on the first row (header row)
If Cell.Row <> 1 Then
If Cell.Value <> "" Then
RangeIsEmpty = False
Exit For
End If
End If
Next Cell
End Function