Help with VB Code to copy workbook

kizzie37

Well-known Member
Joined
Oct 23, 2007
Messages
585
Office Version
  1. 365
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.

  1. I want the code the filter my master sheet in the workbook by column C (unique Names)
  2. Copy all columns (hidden or not)
  3. Copy all rows (as Long)
  4. Paste into a new workbook (must preserve the data validation, formatting and formulas
  5. Save the workbook as "SPGC Compensation Increase Master" then append the name from column C (in each instance)
  6. Also needs to copy the second worksheet "Drop Down" into each of the new workbooks (to keep the validation choice list)
Below is the code ChatGPT came back with. a couple of issues:

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 :)
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

Forum statistics

Threads
1,223,952
Messages
6,175,592
Members
452,653
Latest member
craigje92

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