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?
 
There isn't a reply associated with your last post!

Cheerio,
vcoolio.

Good morning my friend, i'm trying to setup the add in in order to see the file correct, but although i did it i cant find it in the ribbon
 
Upvote 0

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
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.
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.
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.
ZUST RIVER.xlsx
ABCDEF
1Doc typeTargetArticleQtySourceDate
2ZUST12332778843909920230216
3ZUST12332778863909920230216
4ZUST12332778872909920230216
5ZUST12332778881909920230216
6ZUST12332779003909920230216
7ZUST12332779023909920230216
8ZUST12332779052909920230216
9ZUST12332779062909920230216
10ZUST12332779092909920230216
11ZUST12332779102909920230216
12ZUST12332779142909920230216
13ZUST12332779153909920230216
14ZUST12332779183909920230216
Sheet1
 
Upvote 0
Hello PcLoukas,

Your dates are all the same.
In your workbook, are they actual dates or are they formatted as text?

Cheerio,
vcoolio.
 
Upvote 0
Hello PcLoukas,

Your dates are all the same.
In your workbook, are they actual dates or are they formatted as text?

Cheerio,
vcoolio.
You are correct, you can use if you like 2 different dates for the test. The date is custom format yyyymmdd
 
Upvote 0
Hello PcLoukas,

Try the following amended code:-
VBA Code:
Option Explicit
Sub TestWbks()

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

Application.ScreenUpdating = False
Application.DisplayAlerts = False

        mypath = ThisWorkbook.Path & "\"
        Set sh = ThisWorkbook.Sheets("Sheet1")
        lr = sh.Range("A" & Rows.Count).End(xlUp).Row
        sh.Range("F1:F" & lr).AdvancedFilter 2, , sh.[X1], 1  'Unique values moved temporarily to Column X.
        sh.Range("X2", sh.Range("X" & sh.Rows.Count).End(xlUp)).Sort [X2], 1 'Unique values sorted.
        ar = sh.Range("X2", sh.Range("X" & sh.Rows.Count).End(xlUp))
        
        For i = 1 To UBound(ar)
                If Not Evaluate("ISREF('" & CStr(ar(i, 1)) & "'!A1)") Then
                        Workbooks.Add
                        ActiveWorkbook.SaveAs mypath & CStr(ar(i, 1)) & ".xlsx"
                        ActiveWorkbook.Close
                End If
        Next i
        sh.Columns(24).Clear
        
Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub

I've added a couple of more lines of code. You'll see them with comments after them (green font). They basically extract the unique values from Column F, using the Advanced Filter and temporarily move them to Column X where they are sorted then placed into an array from where the code will create the new workbooks. This should help to speed things up for you. Column X is then cleared.

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
Hello PcLoukas,

Try the following amended code:-
VBA Code:
Option Explicit
Sub TestWbks()

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

Application.ScreenUpdating = False
Application.DisplayAlerts = False

        mypath = ThisWorkbook.Path & "\"
        Set sh = ThisWorkbook.Sheets("Sheet1")
        lr = sh.Range("A" & Rows.Count).End(xlUp).Row
        sh.Range("F1:F" & lr).AdvancedFilter 2, , sh.[X1], 1  'Unique values moved temporarily to Column X.
        sh.Range("X2", sh.Range("X" & sh.Rows.Count).End(xlUp)).Sort [X2], 1 'Unique values sorted.
        ar = sh.Range("X2", sh.Range("X" & sh.Rows.Count).End(xlUp))
       
        For i = 1 To UBound(ar)
                If Not Evaluate("ISREF('" & CStr(ar(i, 1)) & "'!A1)") Then
                        Workbooks.Add
                        ActiveWorkbook.SaveAs mypath & CStr(ar(i, 1)) & ".xlsx"
                        ActiveWorkbook.Close
                End If
        Next i
        sh.Columns(24).Clear
       
Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub

I've added a couple of more lines of code. You'll see them with comments after them (green font). They basically extract the unique values from Column F, using the Advanced Filter and temporarily move them to Column X where they are sorted then placed into an array from where the code will create the new workbooks. This should help to speed things up for you. Column X is then cleared.

I hope that this helps.

Cheerio,
vcoolio.

Thank you for your effort coolio, it stops here

1676541756882.png
 
Upvote 0
Hello PcLoukas,

Dates can be somewhat painful to work with but try the following amended code:-

VBA Code:
Option Explicit
Sub TestWbks()

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

Application.ScreenUpdating = False

        mypath = ThisWorkbook.Path & "\"
        Set sh = ThisWorkbook.Sheets("Sheet1")
        sh.Range("W1") = "Unique"
        lr = sh.Range("A" & Rows.Count).End(xlUp).Row
        sh.Range("W2:W" & lr) = "=CONCAT(YEAR(F2),MONTH(F2),DAY(F2))"
        sh.Range("W1:W" & lr).AdvancedFilter 2, , sh.[X1], 1
        sh.Range("X2", sh.Range("X" & sh.Rows.Count).End(xlUp)).Sort [X2], 1
        ar = sh.Range("X2", sh.Range("X" & 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
        sh.Columns("W:X").Clear
        
Application.ScreenUpdating = True

End Sub

Now we're using two helper columns temporarily, W and X. In Column W, a formula extracts the year, month and day from your custom format in Column F. Column W is then automatically formatted as "General", the default. From this column, the Advanced Filter will extract the unique values and place them into Column X and sort them. The values are then placed into an array as per my previous code. From there the code does the rest and creates the new workbooks.

The sample that you posted in post #12 only has one date so I added a few more to test with and the code worked without any problems. I'd assume that your data has more than one date because, if it hasn't, the code will error due to the fact that an array will not work in such a situation with only one element included.

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0

Forum statistics

Threads
1,215,766
Messages
6,126,762
Members
449,336
Latest member
p17tootie

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