如何将输入表单复制到VBA中新创建的工作簿

时间:2017-04-21 07:48:47

标签: excel vba excel-vba

我意识到由于复制了宏的一部分而不清楚。在以新WB开始的宏下面,给出了所需的错误424对象。在手动模式下检查ProjectWB和SourceWB是否正确加载。

Sub Copy_Form_to_new_WB()
   ProjectWB = ActiveWorkbook.Name
   SourceWB = "Interpretation Analysis 2.0.xlsm"
   SourceWB.VBProject.VBComponents("Input_Analysis_Form").Export_ 
       "Input_Analysis_Form.frm"
   ProjectWB.VBProject.VBComponents.Import "Input_Analysis_Form.frm"
   Kill "Input_Analysis_Form.frm"
   Kill "Input_Analysis_Form.frx"
End Sub

在这种情况下,我调暗ProjectWB和SourceWB我得到错误编译错误:无效的限定符。并突出显示第8行中的ProjectWB

Sub Copy_Form_to_new_WB()
  Dim SourceWB As Integer
  Dim ProjectWB As Integer
  ProjectWB = ActiveWorkbook.Name
  SourceWB = "Interpretation Analysis 2.0.xlsm"
  SourceWB.VBProject.VBComponents("Input_Analysis_Form").Export_ 
     "Input_Analysis_Form.frm"
  ProjectWB.VBProject.VBComponents.Import "Input_Analysis_Form.frm"
 Kill "Input_Analysis_Form.frm"
 Kill "Input_Analysis_Form.frx"
End Sub

出了什么问题?

原始问题:

在宏观中,我创建了一个可以正常工作的新工作簿。现在我想将输入表单从现有工作簿复制到新工作簿。检查互联网我发现了一些零件。为了清楚起见,我复制了宏的两个部分。

Dim SourceWB As Workbook
Dim ProjectWB As Workbook
SourceWB = ActiveWorkbook.Name
'now the part to create the new workbook when this is active
ProjectWB = ActiveWorkbook.Name
SourceWB.VBProject.VBComponents("Input_Analysis_Form").Export_ 
"Input_Analysis_Form.frm"
DestinationWB.VBProject.VBComponents.Import "Input_Analysis_Form.frm"
Kill "Input_Analysis_Form.frm"
Kill "Input_Analysis_Form.frx"

现在我得到运行时错误91"对象变量或没有设置块" 什么是正确的方法。

2 个答案:

答案 0 :(得分:1)

在代码的第一个版本中,您声明(通过不明确声明)ProjectWBSourceWBVariant/String,但后来尝试将它们用作{{1}对象。

在代码的第二个版本中,您明确声明WorkbookProjectWBSourceWB,但在尝试之前尝试为其分配Integer值将它们用作String个对象。

根据您修改的代码示例,我相信您正在尝试:

Workbook

原始回答:

您已将Dim SourceWB As Workbook Dim ProjectWB As Workbook 'This line is the only difference to my previous answer Set SourceWB = Workbooks("Interpretation Analysis 2.0.xlsm") Set ProjectWB = ActiveWorkbook 'Actually, here's a second difference - I hadn't noticed the missing space before ' the line continuation character earlier SourceWB.VBProject.VBComponents("Input_Analysis_Form").Export _ "Input_Analysis_Form.frm" DestinationWB.VBProject.VBComponents.Import "Input_Analysis_Form.frm" Kill "Input_Analysis_Form.frm" Kill "Input_Analysis_Form.frx" SourceWB声明为ProjectWB个对象,但只需为其分配Workbook(某些工作簿的名称)。

String

答案 1 :(得分:0)

我意识到由于复制了宏的一部分而不清楚。 在以新WB开始的宏下面,给出了所需的错误424对象。 在手动模式下检查ProjectWB和SourceWB是否正确加载。

Sub Copy_Form_to_new_WB()
   ProjectWB = ActiveWorkbook.Name
   SourceWB = "Interpretation Analysis 2.0.xlsm"
   SourceWB.VBProject.VBComponents("Input_Analysis_Form").Export_ 
       "Input_Analysis_Form.frm"
   ProjectWB.VBProject.VBComponents.Import "Input_Analysis_Form.frm"
   Kill "Input_Analysis_Form.frm"
   Kill "Input_Analysis_Form.frx"
End Sub

在这种情况下,我调暗ProjectWB和SourceWB我得到错误编译错误:无效的限定符。并突出显示第8行中的ProjectWB

Sub Copy_Form_to_new_WB()
  Dim SourceWB As Integer
  Dim ProjectWB As Integer
  ProjectWB = ActiveWorkbook.Name
  SourceWB = "Interpretation Analysis 2.0.xlsm"
  SourceWB.VBProject.VBComponents("Input_Analysis_Form").Export_ 
     "Input_Analysis_Form.frm"
  ProjectWB.VBProject.VBComponents.Import "Input_Analysis_Form.frm"
 Kill "Input_Analysis_Form.frm"
 Kill "Input_Analysis_Form.frx"
End Sub

出了什么问题?