2013-01-24 24 views
5

एक्सेल फ़ाइल प्रोग्रामेटिक रूप से मैक्रो जोड़ने के लिए कोई तरीका है?
मेरे पास बहुत अधिक एक्सेल फ़ाइलें हैं जो मैं उन्हें एक मैक्रो जोड़ना चाहता हूं। मैन्युअल रूप से (हाथ से) जोड़ना असंभव लगता है। मुझे ऐसा करने के लिए एक टूल बनाने की आवश्यकता है।एक्सेल 2010 मैक्रो प्रोग्रामेटिक रूप से

+0

आप इस प्रश्न के उत्तर का भी पालन कर सकते हैं [विभिन्न एक्सेल फ़ाइलों में एक मैक्रो का उपयोग करें] (http://stackoverflow.com/q/9480936/4519059)। –

उत्तर

5

हां, आप इसे प्रोग्रामिक रूप से कर सकते हैं, आप कोड के माध्यम से वीबी एकीकृत विकास पर्यावरण तक पहुंच सकते हैं। निम्नलिखित वेबसाइट VBIDE के बारे में सीखने के लिए उत्कृष्ट हैं, मैंने इन कोडों को एक साथ रखने के लिए उपयोग किया है। यदि आप WorkbookModuleImport चलाते हैं तो दो खुले संवाद बॉक्स पॉप अप हो जाएंगे, पहले मॉड्यूल को आयात करने के लिए कार्यपुस्तिकाओं के लिए पूछना होगा, दूसरा आयात करने के लिए मॉड्यूल का चयन करने के लिए। http://www.cpearson.com/excel/vbe.aspx और http://www.rondebruin.nl/vbaimportexport.htm:

Sub WorkbookModuleImport() 
    Dim ii As Integer, vFileNames As Variant, vModules As Variant 

    'We'll use the Application.GetOpenFilename to get a list of all the excel  workbooks we want to import into 
    vFileNames = Application.GetOpenFilename(",*.xls;*.xlsx;*.xlsm", , "Select Workbooks to Import Modules To", , True) 
    'If the result is not an array it means the cancel button has been pressed 
    If Not IsArray(vFileNames) Then Exit Sub 

    'Use the same method to get all the modules/classes/forms to input 
    vModules = Application.GetOpenFilename(",*.cls, *.bas, *.frm", , "Select Modules/Forms/Class Modules to Import", , True) 
    If Not IsArray(vModules) Then Exit Sub 

    'Now loop through all the workbooks to import the modules 
    For ii = LBound(vFileNames) To UBound(vFileNames) 
     Call ImportModules(VBA.CStr(vFileNames(ii)), vModules) 
    Next ii 

End Sub 



Public Sub ImportModules(sWorkbookName As String, vModules As Variant) 
    Dim cmpComponents As VBIDE.VBComponents, ii As Integer 
    Dim wkbTarget As Excel.Workbook 

    'We need to open the workbook in order to be able to import the code module 
    Set wkbTarget = Workbooks.Open(sWorkbookName) 

    'If the project is protected with a password we can't import so just set tell us in the immediate window 
    If wkbTarget.VBProject.Protection = 1 Then 
     'Give a message 
     Debug.Print wkbTarget.Name & " has a protected project, cannot import module" 
     GoTo Cancelline 
    End If 

    'This is where we set the reference to the components of the Visual Basic project 
    Set cmpComponents = wkbTarget.VBProject.VBComponents 

    'Loop through all the modules to import 
    For ii = LBound(vModules) To UBound(vModules) 
     cmpComponents.Import vModules(ii) 
    Next ii 


Cancelline: 
    'If it's in Excel 2007+ format but doesn't already have macros, we'll have to save it as a macro workbook 
    If wkbTarget.FileFormat = xlOpenXMLWorkbook Then 
     wkbTarget.SaveAs wkbTarget.Name, xlOpenXMLWorkbookMacroEnabled 
     wkbTarget.Close SaveChanges:=False 
    Else 
     'Otherwise, just save the workbook and close it 
     wkbTarget.Close SaveChanges:=True 
    End If 

    'I don't trust excel, so set the workbook object to nothing 
    Set wkbTarget = Nothing 
End Sub 

ये वेबपृष्ठ महान संदर्भ हैं। मैंने रॉन को शुरुआती बिंदु के रूप में इस्तेमाल किया।