I am not a VB coder but can "Tweak" some codes so I have a tiny bit of know how, so I tried to use Chat GPT to create a VB code for my purpose (I know I hear groans) it does some of the things I want but isn't working fully.
It doesn't copy all the columns
It doesn't preserve the dropdowns or keep the formulas when copying
It doesn't loop through each name, once the code ends it goes back to the first filtered name on the list instead of moving to the next name and so on, so it creates an error
Can anyone help fix this up so it does what I need it to do please? I wish I were that clever
- I want the code the filter my master sheet in the workbook by column C (unique Names)
- Copy all columns (hidden or not)
- Copy all rows (as Long)
- Paste into a new workbook (must preserve the data validation, formatting and formulas
- Save the workbook as "SPGC Compensation Increase Master" then append the name from column C (in each instance)
- Also needs to copy the second worksheet "Drop Down" into each of the new workbooks (to keep the validation choice list)
It doesn't copy all the columns
It doesn't preserve the dropdowns or keep the formulas when copying
It doesn't loop through each name, once the code ends it goes back to the first filtered name on the list instead of moving to the next name and so on, so it creates an error
VBA Code:
Sub CopyDataAndDropDown()
Dim wsMaster As Worksheet
Dim wsDropDown As Worksheet
Dim newWB As Workbook
Dim filterRange As Range
Dim filterCell As Range
Dim newName As String
Dim savePath As String
Dim lastRow As Long
' Define your master and drop down worksheets
Set wsMaster = ThisWorkbook.Sheets("Master Sheet")
Set wsDropDown = ThisWorkbook.Sheets("Drop Down")
' Find the last row in column C of Master Sheet
lastRow = wsMaster.Cells(wsMaster.Rows.Count, "C").End(xlUp).Row
' Loop through each unique name in column C of Master Sheet
For Each filterCell In wsMaster.Range("C2:C" & lastRow).SpecialCells(xlCellTypeConstants)
' Filter data based on the name in column C
wsMaster.AutoFilterMode = False
wsMaster.Range("A1:Z" & lastRow).AutoFilter Field:=3, Criteria1:=filterCell.Value
' Copy filtered data including formulas, data validation, and formatting
Set filterRange = wsMaster.Range("A1:Z" & lastRow).SpecialCells(xlCellTypeVisible)
' Create a new workbook and paste the filtered data
Set newWB = Workbooks.Add
filterRange.Copy
newWB.Sheets(1).Range("A1").PasteSpecial Paste:=xlPasteAllUsingSourceTheme
' Copy Drop Down sheet into new workbook
wsDropDown.Copy After:=newWB.Sheets(newWB.Sheets.Count)
' Set the new workbook name and save
newName = filterCell.Value & "_SPGC_Compensation_Increase_Master_Worksheet.xlsx"
savePath = ThisWorkbook.Path & "\" & newName
Application.CutCopyMode = False ' Clear clipboard
newWB.SaveAs savePath
newWB.Close SaveChanges:=False
Next filterCell
' Turn off filter
wsMaster.AutoFilterMode = False
End Sub
Can anyone help fix this up so it does what I need it to do please? I wish I were that clever