VBA - creating new worksheets with a variable name

Autumnscribe

New Member
Joined
Jul 10, 2021
Messages
8
Office Version
  1. 2016
Hi All

Complete VBA noobie. Other modules are working now need to fix the export module.

Need to split up files based on column B - source system. It has the values AX or COM. Code does the splitting great - creates 2 new workbooks but doesn't name them.

Where I've added the comment (in CAPITALS) inside the IF statement, the variable strColumnValue is AX then COM. Does anyone know how at that point I can create and name the 2 workbooks?

VBA Code:
Sub SplitSheetDataIntoMultipleWorkbooksBasedOnSpecificColumn()
    Dim objWorksheet As Excel.Worksheet
    Dim nLastRow, nRow, nNextRow As Integer
    Dim strColumnValue As String
    Dim objDictionary As Object
    Dim varColumnValues As Variant
    Dim varColumnValue As Variant
    Dim objExcelWorkbook As Excel.Workbook
    Dim objSheet As Excel.Worksheet
 
 'Set CombinedData as the activeworksheet
 Worksheets("CombinedData").Activate
 
    Set objWorksheet = ActiveSheet
    nLastRow = objWorksheet.Range("A" & objWorksheet.Rows.Count).End(xlUp).Row
 
    Set objDictionary = CreateObject("Scripting.Dictionary")
 
    For nRow = 2 To nLastRow
        'Get the specific Column
        'Here source (system) is in column B
        'The column can be changed here
        strColumnValue = objWorksheet.Range("B" & nRow).Value
 
        If objDictionary.Exists(strColumnValue) = False Then
           objDictionary.Add strColumnValue, 1
           MsgBox strColumnValue ' HERE THE  VARIABLE VALUES ARE AX THEN COM - How do I create workbooks with those names at this point?
        End If
    Next
 
    varColumnValues = objDictionary.Keys
 
    For i = LBound(varColumnValues) To UBound(varColumnValues)
        varColumnValue = varColumnValues(i)
 
        'Create a new Excel workbook
        Set objExcelWorkbook = Excel.Application.Workbooks.Add
        Set objSheet = objExcelWorkbook.Sheets(1)
 
        objWorksheet.Rows(1).EntireRow.Copy
        objSheet.Activate
        objSheet.Range("A1").Select
        objSheet.Paste
 
        For nRow = 2 To nLastRow
            If CStr(objWorksheet.Range("B" & nRow).Value) = CStr(varColumnValue) Then
               'Copy data with the same column "B" value to new workbook
               objWorksheet.Rows(nRow).EntireRow.Copy
 
               nNextRow = objSheet.Range("A" & objWorksheet.Rows.Count).End(xlUp).Row + 1
               objSheet.Range("A" & nNextRow).Select
               objSheet.Paste
               objSheet.Columns("A:B").AutoFit

            End If
           
        Next
       
    Next
End Sub


MacroControlFile.xlsm
ABCDEF
1IDSource SystemProductBuy_PriceRRPStatus
2ZN6213AXMitre Box35.248.5Recommend approval
3AB1234AXSkillsaw48.571Recommend approval
4AP7201COMHammer7.7511.3Recommend approval
5CD6543COMScrewdriver3.255.75Recommend approval
CombinedData
 
Last edited by a moderator:

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
This may help you?
VBA Code:
Dim wb As Workbook
    For nRow = 2 To nLastRow
        'Get the specific Column
        'Here source (system) is in column B
        'The column can be changed here
        strColumnValue = objWorksheet.Range("B" & nRow).Value
 
        If objDictionary.Exists(strColumnValue) = False Then
           objDictionary.Add strColumnValue, 1
           MsgBox strColumnValue ' HERE THE  VARIABLE VALUES ARE AX THEN COM - How do I create workbooks with those names at this point?
           Set wb = Workbooks.Add
           wb.SaveAs ThisWorkbook.Path & "\" & strColumnValue
           
        End If
    Next
 
Upvote 0
Solution
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Create new workbooks with a variable name
and Create new workbooks with a variable name - OzGrid Free Excel/VBA Help Forum
not to mention Create new workbooks with a variable name
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,139
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top