vba 无效或不合格的参考

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13828796/
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:30  来源:igfitidea点击:

Invalid or Unqualified Reference

excel-vbavbaexcel

提问by STANGMMX

Do I need to put in the sheet name? I need to use this macro across multiple workbooks with similar worksheets but the tab names are different.

我需要输入工作表名称吗?我需要在具有相似工作表的多个工作簿中使用此宏,但选项卡名称不同。

Sub pageSetup()
ActiveSheet.pageSetup
.Orientation = xlLandscape
.PaperSize = xlPaperLegal
.FitToPagesWide = 1
.FitToPagesTall = 1
.LeftMargin = Application.InchesToPoints(1#)

End Sub

回答by brettdj

As Tim has not claimed his answer, you could use either of the following two options to either

由于 Tim 没有声明他的答案,您可以使用以下两个选项之一

  • format the ActiveSheet
  • format all WorkSheetsin the ActiveWorkBook
  • 格式化 ActiveSheet
  • 格式都WorkSheetsActiveWorkBook

ActiveSheet

活动表

Sub TimWilliamsPoints()
With ActiveSheet.pageSetup
.Orientation = xlLandscape
.PaperSize = xlPaperLegal
.FitToPagesWide = 1
.FitToPagesTall = 1
.LeftMargin = Application.InchesToPoints(1#)
End With
End Sub

All Sheets

所有表格

Sub TimWilliamsPoints2()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Sheets
With ws.pageSetup
.Orientation = xlLandscape
.PaperSize = xlPaperLegal
.FitToPagesWide = 1
.FitToPagesTall = 1
.LeftMargin = Application.InchesToPoints(1#)
End With
Next ws
End Sub