简体   繁体   中英

VBA Excel macro: use Range to act on a different worksheet

I am very much a beginner when it comes to VBA programming.

I have a Macro that hides or shows columns based on the value in one cell:

Sub HideColumnsMacro()
 Range("b8:o8").EntireColumn.Hidden = False
 v1 = Range("b2").Value + 1
 If v1 < 12 Then
  With Range("b8")
   Range(.Offset(0,v1), .Offset(0, 12)).EntireColumn.Hidden = True
  End With
 End If
End Sub

I want to be able to get this same functionality when I change a cell on a different sheet. Is there a way I can tell this Macro to act on this sheet, when it is run from a different one?

在宏中,指定确切的工作表:

Sheets("Sheet1").Range("b8:o8").EntireColumn.Hidden = False

使用工作表的名称来限定Range

Sheet1.Range("b8:o8").EntireColumn.Hidden = False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM