gpt4 book ai didi

vba - 为什么我的 VBA 代码抛出 "Invalid outside procedure"错误?

转载 作者:行者123 更新时间:2023-12-02 09:42:56 29 4
gpt4 key购买 nike

我一生都无法弄清楚为什么以下代码会抛出编译错误并显示消息“无效的外部过程”。它突出显示下面带星号的行上的错误。

Option Explicit

Dim shtThisSheet As Worksheets

**Set shtThisSheet = Application.Workbook("Formatting2.xlsm").Worksheets("Sheet1")**

Sub Formatting()

With shtThisSheet

With Range("A1")
.Font.Bold = True
.Font.Size = 14
.HorizontalAlignment = xlLeft
End With

With Range("A3:A6")
.Font.Bold = True
.Font.Italic = True
.Font.ColorIndex = 5
.InsertIndent 1
End With

With Range("B2:D2")
.Font.Bold = True
.Font.Italic = True
.Font.ColorIndex = 5
.HorizontalAlignment = xlRight
End With

With Range("B3:D6")
.Font.ColorIndex = 3
.NumberFormat = "$#,##0"
End With

End With

End Sub

最佳答案

外部过程不允许

Set 语句。将 Set 语句移至 Formatting 过程中:

Sub Formatting()
Set shtThisSheet = Application.Workbook("Formatting2.xlsm").Worksheets("Sheet1")
...

(我还将 Dim 语句移至过程中。我更愿意尽可能避免全局变量。)

关于vba - 为什么我的 VBA 代码抛出 "Invalid outside procedure"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35319309/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com