2017-12-21 4 views
0

Ich versuche, etwas CBA-Code zu schreiben, um meine Arbeit zu erleichtern. Eigentlich möchte ich zwei Arbeitsmappen öffnen, Arbeitsmappe 1 und Arbeitsmappe 2.VBA-Schleife Arbeit mit zwei Arbeitsmappe öffnen und als neue Arbeitsmappe speichern

Dann brauche ich bestimmte Zellen workbook2 (C103:C107 zum Beispiel) bilden kopieren in workbook 1 (E41:E45) und speichern Sie die workbook1 als neue Arbeitsmappe genannt X1.xlsm.

Copy (D103:D107) aus workbook2 und kopieren Sie in (E41:E45) workbook1 und wie neu genannt X2.xlsm speichern.

(E103:E107) (workbook 2) ---(E41:E45) (Workbook1), gespeichert als x3.xlsm .......

Machen Sie die gleiche Sache Schleife durch Spalten von Worbook 2.

Aber das folgende Makro funktioniert nicht:

Sub TADDEnter() 
Dim wbk1 As Workbook 
Dim wbk2 As Workbook 
Dim activeWB As Workbook 
Dim FilePath1 As String 
Dim FilePath2 As String 

FilePath1 = "T:\L'Oreal\83113035 - Project Beauty\TOM\Deliverables\Tables\TADD\Copy of TADD Uploads (002).xlsx" 
FilePath2 = "T:\L'Oreal\83113035 - Project Beauty\TOM\Deliverables\Tables\TADD\TADD CSV template.xlsm" 
Set wbk1 = Application.Workbooks.Open(FilePath2) 
Set wbk2 = Application.Workbooks.Open(FilePath1) 
Set activeWB = Application.ActiveWorkbook 
For icol = 3 To 33 
    wbk1.Sheets("DATA MEASURES FORM").Copy 
    Workbooks.Add 
    Range("A1").PasteSpecial 
    wbk2.Sheets("LOreal").Range(wbk2.Sheets("LOreal").Cells(103, icol), wbk2.Sheets("LOreal").Cells(107, icol)).Copy Destination:=activeWB.Sheets("DATA MEASURES FORM").Range("E41:E45") 
    activeWB.SaveAs Filename:= _ 
     "T:\L'Oreal\83113035 - Project Beauty\TOM\Deliverables\Tables\TADD\TADD_CSV_" & wbk2.Sheets("LOreal").Cells(147, icol).Value & ".xlsm" 
    activeWB.Close 
    Application.CutCopyMode = False 
    Next icol 
End Sub 
+2

Inwiefern funktioniert es nicht? Gib uns einen Hinweis. Die Maus läuft weg? Monitor explodiert? –

+0

Welchen Fehler bekommen Sie und in welcher Zeile wird er hervorgehoben, wenn Sie auf debug klicken? – braX

+0

Versuchen Sie, ActiveWB zu setzen, nachdem Sie die neue Arbeitsmappe hinzugefügt haben. Momentan ist Ihr activeWB wbk2. – Maki

Antwort

0

Probieren Sie die neue Arbeitsmappe als activeWB ein, nachdem Sie schaffen es haben.

Sub TADDEnter() 

Dim wbk1 As Workbook 'source_A 
Dim wbk2 As Workbook 'source_B 
Dim activeWB As Workbook 'target 
Dim FilePath1 As String 
Dim FilePath2 As String 

FilePath1 = "T:\L'Oreal\83113035 - Project Beauty\TOM\Deliverables\Tables\TADD\Copy of TADD Uploads (002).xlsx" 
FilePath2 = "T:\L'Oreal\83113035 - Project Beauty\TOM\Deliverables\Tables\TADD\TADD CSV template.xlsm" 
Set wbk1 = Application.Workbooks.Open(FilePath1) 
Set wbk2 = Application.Workbooks.Open(FilePath2) 

For icol = 3 To 33 
    wbk1.Sheets("DATA MEASURES FORM").Copy 'copy sheet and create a new workbook 

    Set activeWB = Application.ActiveWorkbook 'set the new workbook as activeWB 
    wbk2.Sheets("LOreal").Range(wbk2.Sheets("LOreal").Cells(103, icol), wbk2.Sheets("LOreal").Cells(107, icol)).Copy 
    activeWB.Sheets("DATA MEASURES FORM").Range("E41:E45").PasteSpecial 

    activeWB.SaveAs Filename:="X" & icol - 2, FileFormat:=52 '52 = xlsm 
    activeWB.Close 

    Application.CutCopyMode = False 
Next icol 

End Sub 
Verwandte Themen