[英]Button with macro in Excel
by programming (not manually) I am importing a macro and a form into a spreadsheet with a button that executes that macro.通过编程(不是手动),我将一个宏和一个表单导入到带有执行该宏的按钮的电子表格中。 When I do it in Excel 2007 I open the spreadsheet and execute the button without problems.
当我在 Excel 2007 中执行此操作时,我打开电子表格并毫无问题地执行按钮。 But in Excel 2010 I focus the button macro but with a malformed path (adds a subfolder to the original path) and can't find the macro.
但是在 Excel 2010 中,我关注按钮宏但路径格式错误(将子文件夹添加到原始路径)并且找不到宏。 I would appreciate if someone can help me with this.
如果有人可以帮助我,我将不胜感激。 Cheers
干杯
ok BigBen, Here I put code for Export and Import macro code from Excel:
Sub ExportModules(FullPathFile As String) Dim bExport As Boolean Dim wkbSource As Excel.Workbook Dim szSourceWorkbook As String Dim szExportPath As String Dim szFileName As String Dim cmpComponent As VBIDE.VBComponent Dim wb As Workbook
Set wb = Workbooks.Open(FullPathFile)
Set wkbSource = Application.Workbooks(wb.Name)
szExportPath = "C:\valq\VBAProjectFiles\"
For Each cmpComponent In wkbSource.VBProject.VBComponents
bExport = True
szFileName = cmpComponent.Name
Select Case cmpComponent.Type
Case vbext_ct_ClassModule
szFileName = szFileName & ".cls"
Case vbext_ct_MSForm
szFileName = szFileName & ".frm"
Case vbext_ct_StdModule
szFileName = szFileName & ".bas"
Case vbext_ct_Document
' This is a worksheet or workbook object.
' Don't try to export.
bExport = False
End Select
If bExport Then
cmpComponent.Export szExportPath & szFileName
End If
Next cmpComponent
wb.Close
End Sub
Sub ImportModules(FullPathFile As String)
Dim wkbTarget As Excel.Workbook
Dim objFSO As Scripting.FileSystemObject
Dim objFile As Scripting.File
Dim szTargetWorkbook As String
Dim szImportPath As String
Dim szFileName As String
Dim cmpComponents As VBIDE.VBComponents
Dim szSourceWorkbook As String
Dim wkbSource As Application
Dim wb As Workbook
Set wb = Workbooks.Open(FullPathFile)
szTargetWorkbook = wb.Name
Set wkbTarget = Application.Workbooks(szTargetWorkbook)
szImportPath = "C:\valq\VBAProjectFiles\"
Set objFSO = New Scripting.FileSystemObject
If objFSO.GetFolder(szImportPath).Files.Count = 0 Then
MsgBox "There are no files to import"
Exit Sub
End If
Set cmpComponents = wkbTarget.VBProject.VBComponents
For Each objFile In objFSO.GetFolder(szImportPath).Files
If (objFSO.GetExtensionName(objFile.Name) = "cls") Or _
(objFSO.GetExtensionName(objFile.Name) = "frm") Or _
(objFSO.GetExtensionName(objFile.Name) = "bas") Then
cmpComponents.Import objFile.Path
End If
Next objFile
wb.Save
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.