【发布时间】:2026-01-11 20:00:02
【问题描述】:
我有一个包含大量 VBA 代码的 Excel 工作簿。 VBA 代码由许多子例程、函数和用户表单组成。超过 200 名员工将使用此工作簿。
目前我的 VBA 代码位于分布式 Excel 工作簿中。我担心我会面临的问题是如果需要更新每个 Workbooks VBA 代码。
最好将我的所有 VBA 代码编写为插件的一部分,将插件的新版本上传到站点并让员工从那里下载?如果是这样,我会遇到任何限制或限制吗?这样的功能甚至可能吗? VB.Net 是更好的解决方案吗?
我从我的原始工作簿文件创建了一个 XLAM 文件。原始工作簿文件包含我所有的子例程、函数和用户窗体。直接调用 UserForm 时遇到错误,即使我引用了包含 UserForm1 的 XLAM 文件。
从分布式工作簿副本运行以下方案。 WorkBook 正在引用 XLAM 文件。
场景 1:从分配给形状的 Sub 调用 UserForm
下面的 Sub 返回一个Runtime Error 424 Object Required
Sub RectangleRoundedCorners1_Click()
UserForm1.Show 'highlights this line on the error, XLAM reference houses UserForm1
End Sub
场景 2:从调用 UserForm 的形状调用子过程 这个方法不返回错误,为什么?我们不能从引用的加载项中引用用户窗体对象吗?
Sub RectangleRoundedCorners1_Click()
showUserForm
End Sub
Sub showUserForm()
UserForm1.Show
End Sub
场景 3:使用 UserForms 将值输入工作表单元格
我是否必须在每个用户窗体中引用ActiveWorkbook?
Private Sub CommandButton1_Click()
Set wb = ActiveWorkbook
Set ws = wb.Sheets("clientmenu")
forceLogOut
'clear filter so that we dont mix new customers up
If ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
With ws.Shapes("priorities")
.Fill.ForeColor.RGB = RGB(64, 64, 64)
End With
End If
If contact.value <> "" And result.value = vbNullString Then
MsgBox "Please enter a result"
result.BorderColor = vbRed
result.BackColor = vbYellow
result.DropDown
Exit Sub
ElseIf contact.value = vbNullString And result.value <> "" Then
MsgBox "Please enter a date"
contact.BorderColor = vbRed
contact.BackColor = vbYellow
Exit Sub
Else: With ws
callDate
callResult
End With
End If
With ws
lastrow = .Range("A" & Rows.Count).End(xlUp).Row + 1
If Me.priority_ = vbNullString Then
ws.Range("A" & lastrow).Interior.Color = vbWhite
ws.Range("A" & lastrow).Font.Color = RGB(0, 0, 0)
ElseIf Me.priority_ = "None" Then
ws.Range("A" & lastrow).Interior.Color = vbWhite
ws.Range("A" & lastrow).Font.Color = RGB(0, 0, 0)
ws.Range("B" & lastrow).value = vbNullString
ElseIf Me.priority_ = "High" Then
'.Cells(x, 1).Interior.Color = RGB(0, 176, 80)
ws.Range("A" & lastrow).Font.Color = RGB(0, 176, 80)
ws.Range("B" & lastrow).value = addnewClient.priority_.Text
ElseIf Me.priority_ = "Medium" Then
'.Cells(x, 1).Interior.Color = RGB(255, 207, 55)
ws.Range("A" & lastrow).Font.Color = RGB(255, 207, 55)
ws.Range("B" & lastrow).value = addnewClient.priority_.Text
ElseIf Me.priority_ = "Low" Then
'.Cells(x, 1).Interior.Color = RGB(241, 59, 59)
ws.Range("A" & lastrow).Font.Color = RGB(241, 59, 59)
ws.Range("B" & lastrow).value = addnewClient.priority_.Text
End If
If Me.client = vbNullString Then
MsgBox "Must enter Clients name in order to proceed"
Exit Sub
ElseIf Me.client <> vbNullString Then
ws.Range("L" & lastrow).value = Format(Now(), "mm/dd/yyyy")
ws.Range("A" & lastrow).value = addnewClient.client.Text
ws.Range("A" & lastrow).Font.Name = "Arial"
ws.Range("A" & lastrow).Font.Size = 18
ws.Range("A" & lastrow).Font.Bold = True
ws.Range("B" & lastrow).Font.Name = "Arial"
ws.Range("B" & lastrow).Font.Size = 14
ws.Range("B" & lastrow).HorizontalAlignment = xlCenter
ws.Range("C" & lastrow).value = addnewClient.priority.Text
ws.Range("C" & lastrow).Font.Name = "Arial"
ws.Range("C" & lastrow).Font.Size = 14
ws.Range("C" & lastrow).HorizontalAlignment = xlCenter
ws.Range("E" & lastrow).value = addnewClient.contact.value
ws.Range("E" & lastrow).Font.Name = "Arial"
ws.Range("E" & lastrow).Font.Size = 14
ws.Range("E" & lastrow).HorizontalAlignment = xlCenter
ws.Range("G" & lastrow).value = addnewClient.result.Text
ws.Range("G" & lastrow).Font.Name = "Arial"
ws.Range("G" & lastrow).Font.Size = 14
ws.Range("G" & lastrow).HorizontalAlignment = xlCenter
ws.Range("I" & lastrow).value = addnewClient.segmentType.Text
ws.Range("I" & lastrow).Font.Name = "Arial"
ws.Range("I" & lastrow).Font.Size = 14
ws.Range("I" & lastrow).HorizontalAlignment = xlCenter
ws.Range("K" & lastrow).value = addnewClient.notes.Text
If Me.contact = vbNullString Then
ElseIf Me.contact <> vbNullString Then
ws.Range("J" & lastrow) = Sheet3.Range("J" & lastrow).value + 1
ws.Range("J" & lastrow).Font.Name = "Arial"
ws.Range("J" & lastrow).Font.Size = 14
ws.Range("J" & lastrow).Font.Bold = True
ws.Range("J" & lastrow).HorizontalAlignment = xlCenter
End If
End If
End With
'With Sheet3
'Sheet3.Range("A" & lastrow & ":K" & lastrow).Interior.Color = vbWhite
Application.GoTo Range("A" & lastrow), True
'End With
wb.Sheets(2).Range("C4") = Format(Now, "mm/dd/yyyy")
Unload Me
End Sub
【问题讨论】:
-
看看下面的问题:*.com/q/50473365/8597922.
-
@Victor K 这种方法如何用于用户表单或工作表模块级别的代码?它如何触发从形状调用的宏?
-
您绝对可以在加载项中包含用户表单。也可以处理工作表事件,但这可能取决于您的具体情况。
-
@Victor K 为澄清起见,我是否可以使用
Application.Run(ModuleName.SubName)调用 VBA 代码并将所有内容引用到ActiveWorkBook? -
完全依赖于代码的组织。您可以拥有一个 .xlsm,其中包含一个作为引用的加载项。你可以使用
ModuleName.SubName甚至SubName。或者,您可以每次都打开一个加载项并让其引用ActiveWorkbook,或者相反,让工作簿使用Application.Run。