如何为 Excel 保存 VBA 中所有隐藏列的范围
How to save the range of all hidden columns in VBA for Excel
在 VBA for Excel 中,我需要保存 sheet 中隐藏的所有列的范围,不幸的是我不知道如何做这个。
为了提供更多上下文,我的目的是将 sheet 的隐藏列范围保存在一个临时变量中,然后取消隐藏列,保存工作簿并重新隐藏已保存的列,以便始终保存工作簿所有列都可见。
我卡在 “将 sheet 的隐藏列范围保存在临时变量中” 步骤。
谢谢你的帮助。
这实际上是一个非常简单的过程。以后您确实需要与我们分享您为解决问题所做的工作。
我假设您对 vba 还很陌生,所以会问这个问题,请参阅我在下面的代码中留下的评论。
Sub runme()
Dim HiddenColumns(), size As Integer
'Using a seperate counter, loop through your range of data.
'If you find a hidden column redim our hidden columns array and add that row's number to the array
'then increase our seperate counter
size = 0
For i = 1 To 12 'Change this to your range
If Columns(i).Hidden = True Then
ReDim Preserve HiddenColumns(size) 'Redim and preserve the array to our "size" variable (which will always be one more than the current array size
HiddenColumns(size) = i
size = size + 1
End If
Next i
'Now we want to loop through our array and flip all the columns that were hidden to shown
'You can add this to the original array building loop to be more efficent I'm just breaking it up here
'for demonstration purposes
For i = 0 To size - 1
Worksheets("Sheet1").Columns(HiddenColumns(i)).Hidden = False
Next i
'Call your workbook saving here
ThisWorkbook.Save
'Now loop through our array of columns once more to rehide them
For i = 0 To size - 1
Worksheets("sheet1").Columns(HiddenColumns(i)).Hidden = True
Next i
End Sub
可能有更有效的方法来实现您想要的,但一种方法是遍历范围的列,如果该列被隐藏,则使用 [=11= 将其添加到范围变量中].
例如,假设您要将变量 mInitialRange
中的所有隐藏列存储到变量 mHiddenColumns
中。这会给你:
Dim mInitialRange As Range, mHiddenColumns As Range
For Each mcolumn In mInitialRange.Columns
If mcolumn.Hidden Then
If mHiddenColumns Is Nothing Then
Set mHiddenColumns = mcolumn
Else
Set mHiddenColumns = Union(mHiddenColumns, mcolumn)
End If
End If
Next mcolumn
编辑:根据@BigBen 的建议进行了改进
在 VBA for Excel 中,我需要保存 sheet 中隐藏的所有列的范围,不幸的是我不知道如何做这个。 为了提供更多上下文,我的目的是将 sheet 的隐藏列范围保存在一个临时变量中,然后取消隐藏列,保存工作簿并重新隐藏已保存的列,以便始终保存工作簿所有列都可见。 我卡在 “将 sheet 的隐藏列范围保存在临时变量中” 步骤。 谢谢你的帮助。
这实际上是一个非常简单的过程。以后您确实需要与我们分享您为解决问题所做的工作。
我假设您对 vba 还很陌生,所以会问这个问题,请参阅我在下面的代码中留下的评论。
Sub runme()
Dim HiddenColumns(), size As Integer
'Using a seperate counter, loop through your range of data.
'If you find a hidden column redim our hidden columns array and add that row's number to the array
'then increase our seperate counter
size = 0
For i = 1 To 12 'Change this to your range
If Columns(i).Hidden = True Then
ReDim Preserve HiddenColumns(size) 'Redim and preserve the array to our "size" variable (which will always be one more than the current array size
HiddenColumns(size) = i
size = size + 1
End If
Next i
'Now we want to loop through our array and flip all the columns that were hidden to shown
'You can add this to the original array building loop to be more efficent I'm just breaking it up here
'for demonstration purposes
For i = 0 To size - 1
Worksheets("Sheet1").Columns(HiddenColumns(i)).Hidden = False
Next i
'Call your workbook saving here
ThisWorkbook.Save
'Now loop through our array of columns once more to rehide them
For i = 0 To size - 1
Worksheets("sheet1").Columns(HiddenColumns(i)).Hidden = True
Next i
End Sub
可能有更有效的方法来实现您想要的,但一种方法是遍历范围的列,如果该列被隐藏,则使用 [=11= 将其添加到范围变量中].
例如,假设您要将变量 mInitialRange
中的所有隐藏列存储到变量 mHiddenColumns
中。这会给你:
Dim mInitialRange As Range, mHiddenColumns As Range
For Each mcolumn In mInitialRange.Columns
If mcolumn.Hidden Then
If mHiddenColumns Is Nothing Then
Set mHiddenColumns = mcolumn
Else
Set mHiddenColumns = Union(mHiddenColumns, mcolumn)
End If
End If
Next mcolumn
编辑:根据@BigBen 的建议进行了改进