Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50190

Export specific sheets into 1, 2, or 3 workbooks, depending on cell value

$
0
0
Greetings-
I am attempting to export selected sheets of a master workbook to either 1, 2, or 3 new workbooks, depending on the value of cell C27 (which will be 1, 2, or 3). The filename of each new workbook will also vary depending on C27’s value. I’m a near-total novice at VBA, but this is what I’ve come up with. When I run the code, I get the “else without if” error. I’ve tried many different variations of If, Else, and
ElseIf… so the error is probably occurring because I am going about this task in a fundamentally wrong way. I cannot upload the workbook in question due to the security restrictions at my workplace. I am sure there are much better ways to do what I am attempting, but I am at the limit of my knowledge of VBA. Any assistance in fixing the code would be greatly appreciated.

Code:

Sub Createtemplate2()
MsgBox "This function creates the template."
    Dim relativePath1 As String
    Dim relativePath2 As String
    Dim relativePath3 As String
    relativePath1 = ThisWorkbook.Path & "\" & "S # 1 Template " & Sheets("Welcome").Range("O26").Value & ".xlsm"
    relativePath2 = ThisWorkbook.Path & "\" & "S # 2 Template " & Sheets("Welcome").Range("O28").Value & ".xlsm"
    relativePath3 = ThisWorkbook.Path & "\" & "S # 3 Template " & Sheets("Welcome").Range("O30").Value & ".xlsm"
   
If Range("C27").Value = 1 Then
For Each ws In Worksheets(Array("General_Instructions", "Information", "Instructions", _
"Tasks", "K_Instructions", "Ks", "C_Instructions", "Comps", "Comments"))
    SheetName = ws.Name
    ws.Copy
    With ActiveWorkbook
    .SaveAs Filename:=relativePath1
    .Close SaveChanges:=True

ElseIf Range("C27").Value = 2 Then
For Each ws In Worksheets(Array("General_Instructions", "Information", "Instructions", _
"Tasks", "K_Instructions", "Ks", "C_Instructions", "Comps", "Comments"))
    SheetName = ws.Name
    ws.Copy
    With ActiveWorkbook
    .SaveAs Filename:=relativePath1
    .Close SaveChanges:=True
    .SaveAs Filename:=relativePath2
    .Close SaveChanges:=True

ElseIf Range("C27").Value = 3 Then
For Each ws In Worksheets(Array("General_Instructions", "Information", "Instructions", _
"Tasks", "K_Instructions", "Ks", "C_Instructions", "Comps", "Comments"))
    SheetName = ws.Name
    ws.Copy
    With ActiveWorkbook
    .SaveAs Filename:=relativePath1
    .Close SaveChanges:=True
    .SaveAs Filename:=relativePath2
    .Close SaveChanges:=True
    .SaveAs Filename:=relativePath3
    .Close SaveChanges:=True
    End If
End Sub


Viewing all articles
Browse latest Browse all 50190

Trending Articles