Vba code needed to create workbooks based on column data

pcloukas

New Member
Joined
Jul 12, 2022
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Good evening everyone,

I'm new to VBA and i need a simple VBA code to split a sheet into new workbooks based on data in column F (Date), so i can create several workbooks based on date.



TYPELocCodeAreaSourceDate
ZUST12332778843333320230216
ZUST12332778863333320230217
ZUST12332778872333320230216
ZUST12332778881333320230218
ZUST12332779003333320230217




The new workbooks would be helpful to store in the folder where the main workbook is saved



Can anyone help?
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hello PcLoukas,

See if this helps (untested):-

VBA Code:
Option Explicit
Sub TestWbks()

        Dim i As Long, mypath As String, ar As Variant

Application.ScreenUpdating = False
Application.DisplayAlerts = False

        mypath = ThisWorkbook.Path & "\"
        ar = sh.Range("F2", sh.Range("F" & sh.Rows.Count).End(xlUp))
        
        For i = 1 To UBound(ar)
                If Not Evaluate("ISREF('" & ar(i, 1) & "'!A1)") Then
                        Workbooks.Add
                        ActiveWorkbook.SaveAs mypath & ar(i, 1) & ".xlsx"
                        ActiveWorkbook.Close
                End If
        Next i
                       
Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub

The code should save all the new workbooks to the same folder as the main workbook.

I hope that this helps,

Cheerio,
vcoolio.
 
Upvote 0
Hello PcLoukas,

See if this helps (untested):-

VBA Code:
Option Explicit
Sub TestWbks()

        Dim i As Long, mypath As String, ar As Variant

Application.ScreenUpdating = False
Application.DisplayAlerts = False

        mypath = ThisWorkbook.Path & "\"
        ar = sh.Range("F2", sh.Range("F" & sh.Rows.Count).End(xlUp))
       
        For i = 1 To UBound(ar)
                If Not Evaluate("ISREF('" & ar(i, 1) & "'!A1)") Then
                        Workbooks.Add
                        ActiveWorkbook.SaveAs mypath & ar(i, 1) & ".xlsx"
                        ActiveWorkbook.Close
                End If
        Next i
                      
Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub

The code should save all the new workbooks to the same folder as the main workbook.

I hope that this helps,

Cheerio,
vcoolio.


Hello my friend and thank you for the reply, it stops here

1676449041740.png



1676449064544.png
 
Upvote 0
Hello PcLoukas,

Perhaps I should have tested it!
Apologies! I've left out the sheet variable, sh, in the declared variables part. Hence, after ar As Variant, place sh As Worksheet. Make sure that there is a comma between the two variable declarations as follows:

Dim i As Long, mypath As String, ar As Variant, sh As Worksheet

Then set a value to the variable:
Set sh = ThisWorkbook.Sheets("Sheet1")
then place this directly after:
mypath = ThisWorkbook.Path & "\"

You can change "Sheet1" to the relevant sheet name from your main workbook.

The variable should now be defined within the code.

Cheerio,
vcoolio.
 
Upvote 0
Hello PcLoukas,

Perhaps I should have tested it!
Apologies! I've left out the sheet variable, sh, in the declared variables part. Hence, after ar As Variant, place sh As Worksheet. Make sure that there is a comma between the two variable declarations as follows:

Dim i As Long, mypath As String, ar As Variant, sh As Worksheet

Then set a value to the variable:
Set sh = ThisWorkbook.Sheets("Sheet1")
then place this directly after:
mypath = ThisWorkbook.Path & "\"

You can change "Sheet1" to the relevant sheet name from your main workbook.

The variable should now be defined within the code.

Cheerio,
vcoolio.

like this?

1676451242413.png




Option Explicit
Sub TestWbks()

Dim i As Long, mypath As String, ar As Variant sh As Worksheet
Set sh = ThisWorkbook.Sheets("Sheet1")
mypath = ThisWorkbook.Path & "\"

Application.ScreenUpdating = False
Application.DisplayAlerts = False

mypath = ThisWorkbook.Path & "\"
ar = sh.Range("F2", sh.Range("F" & sh.Rows.Count).End(xlUp))

For i = 1 To UBound(ar)
If Not Evaluate("ISREF('" & ar(i, 1) & "'!A1)") Then
Workbooks.Add
ActiveWorkbook.SaveAs mypath & ar(i, 1) & ".xlsx"
ActiveWorkbook.Close
End If
Next i

Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub
 
Upvote 0
You've left out the comma after Variant
So it should look like this:-

VBA Code:
Option Explicit
Sub TestWbks()

        Dim i As Long, mypath As String, ar As Variant, sh As Worksheet

Application.ScreenUpdating = False
Application.DisplayAlerts = False

        mypath = ThisWorkbook.Path & "\"
        Set sh = ThisWorkbook.Sheets("Sheet1")
        ar = sh.Range("F2", sh.Range("F" & sh.Rows.Count).End(xlUp))
        
        For i = 1 To UBound(ar)
                If Not Evaluate("ISREF('" & ar(i, 1) & "'!A1)") Then
                        Workbooks.Add
                        ActiveWorkbook.SaveAs mypath & ar(i, 1) & ".xlsx"
                        ActiveWorkbook.Close
                End If
        Next i
                       
Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub


Cheerio,
vcoolio.
 
Upvote 0
You've left out the comma after Variant
So it should look like this:-

VBA Code:
Option Explicit
Sub TestWbks()

        Dim i As Long, mypath As String, ar As Variant, sh As Worksheet

Application.ScreenUpdating = False
Application.DisplayAlerts = False

        mypath = ThisWorkbook.Path & "\"
        Set sh = ThisWorkbook.Sheets("Sheet1")
        ar = sh.Range("F2", sh.Range("F" & sh.Rows.Count).End(xlUp))
       
        For i = 1 To UBound(ar)
                If Not Evaluate("ISREF('" & ar(i, 1) & "'!A1)") Then
                        Workbooks.Add
                        ActiveWorkbook.SaveAs mypath & ar(i, 1) & ".xlsx"
                        ActiveWorkbook.Close
                End If
        Next i
                      
Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub


Cheerio,
vcoolio.

Yes it seems to work but i cant find the files, their are not in the foldes of the main file
 
Upvote 0
I actually did test it this time and it works just fine on my end. Perhaps upload a sample of the worksheet that you are working from using the XL2BB uploader noted at the top of any reply box.

Cheerio,
vcoolio.
 
Upvote 0
I actually did test it this time and it works just fine on my end. Perhaps upload a sample of the worksheet that you are working from using the XL2BB uploader noted at the top of any reply box.

Cheerio,
vcoolio.
 
Upvote 0
There isn't a reply associated with your last post!

Cheerio,
vcoolio.
 
Upvote 0

Forum statistics

Threads
1,223,630
Messages
6,173,454
Members
452,514
Latest member
cjkelly15

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