多个工作簿使用同一个表单时保持打开状态

Keep same form open when multiple workbook use it

enter image description here我正在寻找解决方案、选项或更好地理解它:

我正在为每个客户的工作簿捕获数据。为了防止出现不同数量的 sheet 并保持每个客户端的数据完整性,我从默认工作簿创建新工作簿,其中包含所有相同的表单和工具。我将一个新客户端添加到连续的客户端列表中,然后保存旧的默认工作簿,使其具有最新的客户端列表,然后将新创建的工作簿保存到我生成的文件夹中的静态位置。

在我工作簿的表单上,我有一个客户字段绑定到填充组合框的客户列表范围。当用户更改客户端并触发更改事件。我正在尝试尽可能无缝地将工作簿切换到新选择的客户端的工作簿。

用户窗体为当前工作簿和用户选择的工作簿分配变量和目录。 VBA 打开所需位置的工作簿,然后打开两个工作簿。现在问题来了。

  1. 我激活了用户选择的工作簿并尝试从该工作簿打开相同的表单,然后关闭当前工作簿,让新选择的工作簿保持打开状态,工作sheets 中有正确的客户数据,所以用户可以继续。我收到关于 运行 time 1004 can not 运行 macro it was not found or doesn't exist 的错误。由于当我关闭工作簿时,表单是在当前工作簿中打开的,因此整个过程都会关闭。我的新工作簿在那里,但表格不是 运行ning。

有更好的方法吗?我阅读了一些有关使用户窗体模块化的内容,但不确定这是否是正确的思路。

Sub SwitchClient(clientname As String)


Dim directory As String
Dim fileName As String
Dim wrk As Workbook
Dim switchwrk As Workbook
Dim sheet As Worksheet

Dim routine As String
Dim passclient As String

Dim wbname As String
Dim filenamep0 As String
Dim filenamep1 As String
Dim filenamep2 As String

Dim sh As String
Dim shname


wbname = ThisWorkbook.Name
filenamep0 = "BA - Briefcase"
filenamep1 = clientname
filenamep2 = "BA - Tools"


Set wrk = ThisWorkbook

Application.ScreenUpdating = False
Application.DisplayAlerts = False


directory = CreateObject("wscript.shell").specialfolders("Desktop") & "\" & 
(filenamep0) & "\" & (filenamep1) & "\" & (filenamep2) & "\"


fileName = Dir(directory & "EB_Analyst_Tool - " & filenamep1 & ".xlsm") 


Workbooks.Open (directory & fileName)

Set switchwrk = Workbooks("EB_Analyst_Tool - " & filenamep1 & ".xlsm")

'Sub I'm trying to run from the new workbook before closing the current workbook - Meeting Minutes sub is userform.show
routine = "MeetingMinutes"

switchwrk.Activate

'Can't get the right syntax to run the macro from the newly opened workbox
Run "'" & switchwrk.Name & "'!" & routine(passclient)
Application.Run fileName & "!MeetingMinutes"

'Save existing workbook with updated clientlist

wrk.Save

'Close current workbook - Everything closes except new opened workbook so i 
need to start the form again.

wrk.Close

还有一些关于添加“解决方案转换器”之类的东西 就像宏错误一样,并使其可信。我不知道那是不是 正确的轨道。

提前致谢

更新:2/1/21 尝试添加更多代码和图像,以确保我没有遗漏任何有用的重要信息。

'Immediately after changeing the Combobox to a different client.

Private Sub cbmmclient_Change()

Dim mmclientname As String

Application.ScreenUpdating = False
Application.DisplayAlerts = False

mmclientname = cbmmclient

SwitchClient


Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

' SwitchClient is is a subroutine in the Module - FormActions
'With Example1 and 2  i get : Run- Time Error '1004' Cannot run the 
macro'EB_Analyst_Tool - Client.xlsm!MeetingMinutes'. The macro may not be 
available in this workbook or all macros maybe disabled

Sub SwitchClient()


Dim directory As String
Dim fileName As String
Dim wrk As Workbook
Dim switchwrk As Workbook
Dim sheet As Worksheet
Dim total As Integer
Dim routine As String
Dim passclient As String

Dim wbname As String
Dim filenamep0 As String
Dim filenamep1 As String
Dim filenamep2 As String
Dim filenamep3 As String
Dim filenamep4 As String
Dim sh As String
Dim shname
Dim openrng As Range

wbname = ThisWorkbook.Name
filenamep0 = "BA - Briefcase"
filenamep1 = MeetingMinutesForm1.cbmmclient
filenamep2 = "BA - Tools"

Dim prffilename

Set wrk = ThisWorkbook


directory = CreateObject("wscript.shell").specialfolders("Desktop") & "\" & 
(filenamep0) & "\" & (filenamep1) & "\" & (filenamep2) & "\"


'Add the Process List document name here and be able to generate an error if 
the list isn't here and prevent accidental xlm files being loaded
fileName = Dir(directory & "EB_Analyst_Tool - " & filenamep1 & ".xlsm") 
'Requires a Static name to be found and Excel sheets must be name accordingly

'需要打开文件获取进程名和sheetImport 过程数据将是的一部分 Workbooks.Open (directory & fileName) ' - 这有效并打开正确的 切换客户端的工作簿

Set switchwrk = Workbooks("EB_Analyst_Tool - " & filenamep1 & ".xlsm")

'routine = "MeetingMinutes" '- Tried this because I got an error that it 
expected a varible
'switchwrk.Activate ' Tried to make sure the correct workbook was active when 
the code ran.
'wrk.Activate  ' Tried to make sure the correct workbook was active when teh 
code ran.


'Application.Run ("EB_Analyst_Tool - Hawaii - DOT.xlsm! MeetingMinutes") 
'Example 1 I tried to use.

Run switchwrk.Name & "!" & routine, (filenamep1) 'Example 2 I tried to use


'Determine if you want to save the current workbook before closing

wrk.Save

'Close current workbook

 wrk.Close


End Sub

'附图显示了打开的工作簿和两个工作簿中相同的 MeetingMinutesForm1。但由于某种原因,它无法打开第一个工作簿和表单中的工作簿和表单。

试试这一行:

Run switchwrk.Name & "!" & routine, passclient

Application.Run 命令将宏的名称作为第一个参数;以下参数用于宏的参数。


编辑

采用的解决方案:

Application.Run("'" & YourStringVariable & "'!GetUserform")