-1

I am looking at a macro which would hide columns for a particular range every time I run the query.

For example: For the first time when I run the query, I would need Columns A to D to be hidden, like wise the next time I run the query, I would need columns E to H to be hidden.

Its basically a query that would generalize the column hiding pattern.

I request your expertise in this regard.

4

1 回答 1

0

如果传递的 RunCount 值为 1,下面的过程将隐藏列 A 到 D,如果传递值 2,则隐藏列 E 到 H,如果传递值 3,则隐藏列 I 到 L,依此类推。 .

Public Sub HideColumns(RunCount As Integer, TargetSheet As Worksheet)
  Dim rngHide As Range
  Dim ColOffset As Integer

' Initialise rngHide to columns A to D of the target worksheet.
  Set rngHide = TargetSheet.[A:D]

' Calculate the offset from column A to the first column _
  that needs to be hidden, update rngHide if required.
  ColOffset = (RunCount - 1) * 4
  If ColOffset > 0 Then
    Set rngHide = rngHide.Offset(0, ColOffset)
  End If

' Hide the columns.
  rngHide.ColumnWidth = 0
End Sub

如果您决定只想隐藏某些列,只需更改该行:

Set rngHide = TargetSheet.[A:D]

例如,仅隐藏第一列和最后一列(A 列D 列、E 列H 列等):

Set rngHide = TargetSheet.[A:A, D:D]

仅隐藏前两列:

Set rngHide = TargetSheet.[A:B]
于 2011-06-14T11:49:53.527 回答