Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88868

Split Excel workbook into multiple files

$
0
0

I am trying to split an excel workbook with multiple sheets into multiple files for them to be uploaded by a third party. Here is what I have and what I am trying to achieve using VBA.

I have an excel workbook entitled 'Procedures Load File - Version 1.0' which has the following sheets in it

- Procedures
- Sections
- StepsAndOptions

I need to split these sheets out into separate files as per below

- Procedures (Needs to create a file entitled Procedures.csv at a specified path)
- Sections (Needs to create a file entitled Procedures.csv at a specified path)
- StepsAndOptions 

This is where im stuck... this one sheet StepsAndOptions needs to create two new files entitled Steps.csv and Options.csv. Steps.csv should contain the values from columns A2:V2 and Options.csv needs to contain the values from columns W2:Z2 the row amount could be any value. Also should be saved in the same specified path as above

I have the following code in VBA which sort of outlines what i'm trying to achieve.. I only want to add the rows where the type is Steps to the Steps.csv file and rows where the type is Option to the Options.csv file

Sub SplitStepsOptions_Click()

    Dim LastRow As Long, c As Range
    Application.EnableEvents = False
    LastRow = Cells(Cells.Rows.Count, "G").End(xlUp).Row

    For Each c In Range("G1:G"& LastRow)
                If c.Value = "Step" Then
                    c.EntireRow.Select
                    Selection.Copy
                    Workbooks.Add
                    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                        :=False, Transpose:=False
                    Application.CutCopyMode = False
                    ActiveWorkbook.SaveAs FileName:= _
                        "Desktop/Test file load/Load Sheets/Steps.csv", FileFormat:= _
                        xlCSVUTF8, CreateBackup:=False
                End If
    Next
    For Each c In Range("G1:G"& LastRow)
                If c.Value = "Option" Then
                    c.EntireRow.Select
                    Selection.Copy
                    Workbooks.Add
                    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                        :=False, Transpose:=False
                    Application.CutCopyMode = False
                    ActiveWorkbook.SaveAs FileName:= _
                        "Desktop/Test file load/Load Sheets/Options.csv", FileFormat:= _
                        xlCSVUTF8, CreateBackup:=False
                 End If
    Next
End Sub

Viewing all articles
Browse latest Browse all 88868


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>