Excel VBA复制时如何判断是否有隐藏列

发布于 2024-07-26 04:19:46 字数 200 浏览 1 评论 0原文

正如标题所解释的,我有一个 Excel 2003 工作簿,并且我正在 VBA 中将一张工作表的许多列复制到另一张工作表中。 我不知道的是,有人隐藏了源工作表上的几列,这扰乱了我处理目标工作表中单元格的方式。

如何以编程方式确定:

  1. 如果存在隐藏列
  2. 哪些列被隐藏?

谢谢! JFV

As the title explains, I have an Excel 2003 workbook and I'm copying a number of columns of one sheet to another in VBA. Unknown to me, someone has hidden a few columns on the source sheet and it has messed up how I process the cells in the destination sheet.

How can I programmically determine:

  1. IF there are hidden columns
  2. WHICH columns are hidden?

Thanks!
JFV

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一场信仰旅途 2024-08-02 04:19:47

对于范围,请检查Range.Hidden 属性。

MSDN 中的以下代码片段是如何隐藏/取消隐藏行/列的一个很好的示例:

 Worksheets("Sheet1").Columns("C").Hidden = True

您还可以使用 If 语句测试该值:

 For ColCounter = 1 To 10
      If Columns("C").Hidden = True Then
           Columns("C").Hidden = False
      End If
 Next

For a Range, check the Range.Hidden property.

The following snippet from MSDN is a good example of how to hide/unhide a row/column:

 Worksheets("Sheet1").Columns("C").Hidden = True

You can also test the value with an If statement:

 For ColCounter = 1 To 10
      If Columns("C").Hidden = True Then
           Columns("C").Hidden = False
      End If
 Next
最后的乘客 2024-08-02 04:19:47

如果您只想复制可见文件,那么一个非常好的选择是仅选择可见列作为范围。

这可以通过

Set visrng =rng.SpecialCells(xlCellTypeVisible)

我不清楚这对您的情况是否有用,但我决定发布它,因为它可能对其他人有用。

If you only want to copy the visible files then one option that is quite nice it to select only the visible columns as a range.

This can be done by

Set visrng =rng.SpecialCells(xlCellTypeVisible)

It wasn't clear to me if this would be useful in your case, but I decided to post it as it could be useful to others.

坐在坟头思考人生 2024-08-02 04:19:47

将可见单元格复制到另一个范围,然后比较每个单元格的数量可能是确定范围中是否存在隐藏单元格的最简单方法,

例如

Selection.SpecialCells(xlCellTypeVisible).Copy Destination:=VisRan

If Not Selection.Cells.Count = VisRan.Cells.Count Then
    MsgBox "Selection contains Hidden Cells"
End If

Copying Visible Cells to another Range and then comparing the number of cells in each is probably the easiest way to determine if there are Hidden Cells in the Range

eg

Selection.SpecialCells(xlCellTypeVisible).Copy Destination:=VisRan

If Not Selection.Cells.Count = VisRan.Cells.Count Then
    MsgBox "Selection contains Hidden Cells"
End If
享受孤独 2024-08-02 04:19:47

您可以使用如下函数进行检查:

Function IsColumnHidden(column As Variant) as Boolean
    IsColumnHidden = False
    If Columns(column).ColumnWidth = 0.0 Then IsColumnHidden = True
End Function

列宽或行高为 0.0 表示该范围是否隐藏。

You can check by using a function like:

Function IsColumnHidden(column As Variant) as Boolean
    IsColumnHidden = False
    If Columns(column).ColumnWidth = 0.0 Then IsColumnHidden = True
End Function

A Column width or Row height of 0.0 is an indicator of whether or not the range is hidden.

不如归去 2024-08-02 04:19:47

这是我已经测试过的一个,如果您想隐藏/取消隐藏列,它效果很好

Sub Macro1_Test_Hidden() ' ' Macro1_Test_Hidden Macro ' ' ' If Columns("BH:XFA").Hidden = False then Columns(" BH:XFA").选择范围("BH:XFA").激活Selection.EntireColumn.Hidden = True Else Columns("BH:XFA").选择范围("BH:XFA").激活Selection.EntireColumn.Hidden = False End If ' ' End Sub

Here is one that I have tested and it works well if you want to hide/unhide columns

Sub Macro1_Test_Hidden() ' ' Macro1_Test_Hidden Macro ' ' ' If Columns("BH:XFA").Hidden = False Then Columns("BH:XFA").Select Range("BH:XFA").Activate Selection.EntireColumn.Hidden = True Else Columns("BH:XFA").Select Range("BH:XFA").Activate Selection.EntireColumn.Hidden = False End If ' ' End Sub

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文