cherry_pie
New Member
- Joined
- Aug 15, 2006
- Messages
- 29
- Office Version
- 365
- Platform
- Windows
I'm really a VBA novice, but I can play around with existing codes and have a basic grasp of what they are doing but I'm quite rusty as I haven't had to do much VBA for several years and I'm a bit stumped on this one. I've been playing around for several hours and haven't been successful and get regular errors, some of which I seem to fix but then lead to other errors.
I've been using the following code to split a large dataset into individual sheets myself for a while, but I have now been asked if I can make this available to other people. I change the VBA code myself to change the filepath and date that goes on the end of the defined filename (see lines marked with ***).
But others won't necessarily know how to do this so I want to make a few amendments to the VBA so that it can be used more widely.
The first change I want to make is to make the file path and the file name extensions to be input by the user via an input box (or alternatively I'm happy to add in an additional sheet where they could input the details into cells on that sheet).
I would would like to set "Target_Folder" using input box as well as well as the date bit on the end of the file path (*** and underlined) - others may want to use a different end, no end, etc.
The second change I want to make is that prior to running the macro I manually use a "=mid" formula in the last column of the workbook, to pull out the variable used to split the data from a longer string (which is within column A of the "transaction_report" worksheet). I do this and then copy and paste the column as values prior to running the macro. I would like to add this to the code so that other users wouldn't have to worry about doing this manually.
I'm not knowledgeable enough on how all the privatesubs interact to know how to add this in. I could write the code standalone (albeit probably not paritcularly 'proper', but it would suffice), but would struggle to integrate it into the wider module.
As a further bonus, this column would be excluded when the data is copied into the individual sheets and saved. But this isn't essential.
Help would be most welcomed and appreciated as always!!
I've been using the following code to split a large dataset into individual sheets myself for a while, but I have now been asked if I can make this available to other people. I change the VBA code myself to change the filepath and date that goes on the end of the defined filename (see lines marked with ***).
But others won't necessarily know how to do this so I want to make a few amendments to the VBA so that it can be used more widely.
The first change I want to make is to make the file path and the file name extensions to be input by the user via an input box (or alternatively I'm happy to add in an additional sheet where they could input the details into cells on that sheet).
I would would like to set "Target_Folder" using input box as well as well as the date bit on the end of the file path (*** and underlined) - others may want to use a different end, no end, etc.
The second change I want to make is that prior to running the macro I manually use a "=mid" formula in the last column of the workbook, to pull out the variable used to split the data from a longer string (which is within column A of the "transaction_report" worksheet). I do this and then copy and paste the column as values prior to running the macro. I would like to add this to the code so that other users wouldn't have to worry about doing this manually.
I'm not knowledgeable enough on how all the privatesubs interact to know how to add this in. I could write the code standalone (albeit probably not paritcularly 'proper', but it would suffice), but would struggle to integrate it into the wider module.
As a further bonus, this column would be excluded when the data is copied into the individual sheets and saved. But this isn't essential.
Help would be most welcomed and appreciated as always!!
VBA Code:
Option Explicit
**** Const Target_Folder As String = "A:\ABC\DEF\Reports"
Dim wsSource As Worksheet, wsHelper As Worksheet
Dim LastRow As Long, LastColumn As Long
Sub SplitDataset()
Dim collectionUniqueList As Collection
'store the unique list of categories that will be used to split the file
Dim i As Long
Set collectionUniqueList = New Collection
Set wsSource = ThisWorkbook.Worksheets("transaction_report")
Set wsHelper = ThisWorkbook.Worksheets("Helper")
' Clear Helper Worksheet
wsHelper.Cells.ClearContents
With wsSource
.AutoFilterMode = False
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
LastColumn = .Cells(1, Columns.Count).End(xlToLeft).Column
If .Range("A2").Value = "" Then
GoTo Cleanup
End If
Call Init_Unique_List_Collection(collectionUniqueList, LastRow)
Application.DisplayAlerts = False
For i = 1 To collectionUniqueList.Count
SplitWorksheet (collectionUniqueList.Item(i))
Next i
ActiveSheet.AutoFilterMode = False
End With
Cleanup:
Application.DisplayAlerts = True
Set collectionUniqueList = Nothing
Set wsSource = Nothing
Set wsHelper = Nothing
End Sub
Private Sub Init_Unique_List_Collection(ByRef col As Collection, ByVal SourceWS_LastRow As Long)
Dim LastRow As Long, RowNumber As Long
' Unique List Column and copy to helper sheet
wsSource.Range("K2:K" & SourceWS_LastRow).Copy wsHelper.Range("A1")
With wsHelper
'validation checks to find out last row of list on helper sheet and remove duplicates
If Len(Trim(.Range("A1").Value)) > 0 Then
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
.Range("A1:A" & LastRow).RemoveDuplicates 1, xlNo
'resets the variable last row after the duplicates have been removed
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
.Range("A1:A" & LastRow).Sort .Range("A1"), Header:=xlNo
'reset again in case there were empty cells before the data was sorted
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
On Error Resume Next
For RowNumber = 1 To LastRow
col.Add .Cells(RowNumber, "A").Value, CStr(.Cells(RowNumber, "A").Value)
Next RowNumber
End If
End With
End Sub
Private Sub SplitWorksheet(ByVal Category_Name As Variant)
Dim wbTarget As Workbook
Set wbTarget = Workbooks.Add
With wsSource
With .Range(.Cells(1, 1), .Cells(LastRow, LastColumn))
.AutoFilter .Range("K1").Column, Category_Name
.Copy
'wbTarget.Worksheets(1).PasteSpecial xlValues
wbTarget.Worksheets(1).Paste
wbTarget.Worksheets(1).Name = Category_Name
Call Retain_Formula(wbTarget)
*** wbTarget.SaveAs Target_Folder & Category_Name & "[U] v 13 Apr 2022.xlsx[/U]", 51
wbTarget.Close False
End With
End With
Set wbTarget = Nothing
End Sub
Private Sub Retain_Formula(ByVal wb_object As Workbook)
'// assuming dataset always starts at row 2
Dim col_index As Long, target_ws_lastrow As Long
For col_index = 1 To LastColumn
If wsSource.Cells(2, col_index).HasFormula Then
'// transport formula
wb_object.Worksheets(1).Cells(2, col_index).Formula = wsSource.Cells(2, col_index).Formula
'// autofill formula to the last row
target_ws_lastrow = wb_object.Worksheets(1).Cells(Rows.Count, 1).End(xlUp).Row
With wb_object.Worksheets(1)
.Range(.Cells(2, col_index), .Cells(target_ws_lastrow, col_index)).Formula = .Cells(2, col_index).Formula
End With
End If
Next col_index
End Sub