Help Updating VBA code to include formulas and condt'l formatting when Pasting Data to Multiple workbooks

futureexcelguru

New Member
Joined
Jan 25, 2022
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
Hi all! I'm needing some help updating an existing code to paste the data into my new sheet hopefully including formulas, data validation, and conditional formatting. Relatively new to VBA and had some help creating the below, so any help is appreciated!

Its set up to create a new sheet based on text in Column B:B on the Q3-Q4 worksheet. For example, Column B has 5 text options (Test 1, Test 2, Test 3, Test 4, Test5) across 5000 rows. The new workbooks created from the below code will create 5 workbooks to a specified folder path per B text with all the data/rows applicable, its just pasting values and i'm losing most of the formatting formulas. *It is pasting the fill/text colors and borders that populated from the conditional formatting entered on the main workbook

Code here:

Option Explicit

Sub Split_Data_in_workbooks()

Application.ScreenUpdating = False

Dim data_sh As Worksheet
Set data_sh = ThisWorkbook.Sheets("Q3-Q4")

Dim setting_Sh As Worksheet
Set setting_Sh = ThisWorkbook.Sheets("Settings")

Dim nwb As Workbook
Dim nsh As Worksheet

setting_Sh.Range("A:A").Clear
data_sh.AutoFilterMode = False
data_sh.Range("B:B").Copy setting_Sh.Range("A1")

setting_Sh.Range("A:A").RemoveDuplicates 1, xlYes

Dim i As Integer

For i = 2 To Application.CountA(setting_Sh.Range("A:A"))

data_sh.UsedRange.AutoFilter 2, setting_Sh.Range("A" & i).Value


Set nwb = Workbooks.Add
Set nsh = nwb.Sheets(1)

data_sh.UsedRange.SpecialCells(xlCellTypeVisible).Copy nsh.Range("A1")
nsh.UsedRange.EntireColumn.ColumnWidth = 15


nwb.SaveAs setting_Sh.Range("H6").Value & "/" & setting_Sh.Range("A" & i).Value & ".xlsx"
nwb.Close False
data_sh.AutoFilterMode = False
Next i

setting_Sh.Range("A:A").Clear

MsgBox "Done"

End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
I had a couple of issue with the code but not copying across formulas & formatting weren't amongst them.
1) I suggest you change to copy to the settings sheet to copying values only.
If there are any formulas in column B they cause and issue when you are only copying column B
VBA Code:
    'data_sh.Range("B:B").Copy setting_Sh.Range("A1")  <--- replaced by the next 2 lines
    data_sh.Range("B:B").Copy
    setting_Sh.Range("A1").PasteSpecial Paste:=xlPasteValues

2) Turn the screen updating back on at the very end of the code
VBA Code:
Application.ScreenUpdating = true

3) The code as it stands seems to copy everything - formulas, formatting, conditional formatting, constants etc.
Copying any references that are not self contained in the UsedRange of the sheet Q3-Q4 is going to be an issue.
eg if your data validation list is on a different sheet or the formulas link to a different sheet.

Also if your usedrange on Q3-Q4 doesn't start at A1 it could upset anything that is formula based since you are copying it to A1 in the new workbook. Can you confirm Q3-Q4 has data in A1 ?

Ideally provide us with an XL2BB of your data and we can see if any of the above come in to play.
 
Upvote 0
I had a couple of issue with the code but not copying across formulas & formatting weren't amongst them.
1) I suggest you change to copy to the settings sheet to copying values only.
If there are any formulas in column B they cause and issue when you are only copying column B
VBA Code:
    'data_sh.Range("B:B").Copy setting_Sh.Range("A1")  <--- replaced by the next 2 lines
    data_sh.Range("B:B").Copy
    setting_Sh.Range("A1").PasteSpecial Paste:=xlPasteValues

2) Turn the screen updating back on at the very end of the code
VBA Code:
Application.ScreenUpdating = true

3) The code as it stands seems to copy everything - formulas, formatting, conditional formatting, constants etc.
Copying any references that are not self contained in the UsedRange of the sheet Q3-Q4 is going to be an issue.
eg if your data validation list is on a different sheet or the formulas link to a different sheet.

Also if your usedrange on Q3-Q4 doesn't start at A1 it could upset anything that is formula based since you are copying it to A1 in the new workbook. Can you confirm Q3-Q4 has data in A1 ?

Ideally provide us with an XL2BB of your data and we can see if any of the above come in to play.
Thank you Alex for your guidance. Here is an xlbb of a test section - i've made the updates to the code that you suggested above, and i can confirm that Q3-Q4 does start on A1.
To your point, the data validation was linked to another sheet.. I updated the data validation dropdown items to the same sheet, and they copied to the new workbooks. However, it only pulled the list in the items that pulled from Row B:B, attached a screenshot for reference.. any suggestions in how to maintain the data validation? The only other thing that was lost are the formulas in the columns

LIP Bonus Split VBA XLBB Test.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXY
1EE IDWorkbooksCurrent StatusHire DatePrev Term DateRehire DateTerm DateLeave StartLeave EndAnnual $PositionStarting LIP EligiblityEnding LIP EligiblityEligible Days in MonthActual Days in Mo% of Month1/12 SalaryMonthWork LocationLIP Mapped LocationVALIDATION SELECTION REQ'DCorrected Location, or Pooled Location 1Pooled Location 2Pooled Location 3 (If Applicable)Pooled Location 4 (If Applicable)
2EE1Test 1Active06/22/199800/00/000000/00/000000/00/000004/10/202006/16/20210Manager, Operations I08/01/202108/31/20213131100%0.00August74 FL74 FLColumn Z Location is INCORRECT, 1 location needs updatingEnter Updated Location
3EE2Test 1Active03/27/201700/00/000000/00/000000/00/000000/00/000000/00/000050000Manager, Operations II08/01/202108/31/20213131100%4166.67August22 CA22 CAColumn Z Location is CORRECT, only 1 allocation needed
4EE3Test 8Active12/16/201900/00/000000/00/000000/00/000005/23/202005/20/202170000Supervisor, Operations08/01/202108/31/20213131100%5833.33August95 NJ95 NJPooled EmployeeSelect Pooled Location 1Select Pooled Location 2Select Pooled Location 3Select Pooled Location 4
5EE4Test 1Active05/18/201500/00/000000/00/000000/00/000000/00/000000/00/000080000Supervisor, Inventory Control08/01/202108/31/20213131100%6666.67August22 CA22 CAColumn Z Location is CORRECT, only 1 allocation needed
6EE5Test 8Active07/14/201400/00/000000/00/000000/00/000010/01/202004/25/202190000Manager, Warehouse08/01/202108/31/20213131100%7500.00August4 CA4 CAColumn Z Location is CORRECT, only 1 allocation needed
7EE6Test 8Active02/15/200200/00/000000/00/000000/00/000010/12/202001/01/2021100000Manager, Operations II08/01/202108/31/20213131100%8333.33August228 CA228 CAColumn Z Location is CORRECT, only 1 allocation needed
8EE7Test 6Active08/18/201409/18/202002/22/202100/00/000012/06/202101/09/202290000Sr. Manager, Operations12/01/202112/05/202153116%7500.00December126 CA126 CAColumn Z Location is INCORRECT, 1 location needs updatingEnter Updated Location
9EE8Test 8Active10/29/201800/00/000000/00/000000/00/000010/25/202001/10/202160000Supervisor, Operations08/01/202108/31/20213131100%5000.00August6 CALOCATION NOT ON VALIDATION LISTColumn Z Location is INCORRECT, 1 location needs updatingEnter Updated Location
10EE9Test 8Active03/27/201700/00/000000/00/000000/00/000011/05/202006/27/202160000Supervisor, Processing08/01/202108/31/20213131100%5000.00August28 CA28 CAColumn Z Location is CORRECT, only 1 allocation needed
11EE10Test 8Active04/29/201900/00/000000/00/000000/00/000011/10/202002/28/202150000Supervisor, Operations08/01/202108/31/20213131100%4166.67August94 NJ94 NJColumn Z Location is CORRECT, only 1 allocation needed
12EE11Test 5Active05/11/201500/00/000000/00/000000/00/000000/00/000000/00/000060000Sr. Manager, Operations08/01/202108/31/20213131100%5000.00August528 CA528 CAColumn Z Location is CORRECT, only 1 allocation needed
13EE12Test 8Active08/13/201600/00/000000/00/000000/00/000000/00/000000/00/000080000Supervisor, Operations08/01/202108/31/20213131100%6666.67August6 CALOCATION NOT ON VALIDATION LISTColumn Z Location is INCORRECT, 1 location needs updatingEnter Updated Location
14EE13Test 8Active08/10/199800/00/000000/00/000000/00/000000/00/000000/00/000090000Supervisor, Customer Service08/01/202108/31/20213131100%7500.00August6 CALOCATION NOT ON VALIDATION LISTColumn Z Location is INCORRECT, 1 location needs updatingEnter Updated Location
15EE14Test 8Active06/01/201500/00/000000/00/000000/00/000000/00/000000/00/0000100000Director, Operations08/01/202108/31/20213131100%8333.33August112 CA112 CAColumn Z Location is CORRECT, only 1 allocation needed
Q3-Q4
Cell Formulas
RangeFormula
P2:P15P2=N2/O2
Q2:Q15Q2=J2/12
V2,V4:V15V2=IF($U2="Column Z Location is incorrect, 1 Location Needs updating","Enter Updated Location",IF($U2="Pooled Employee","Select Pooled Location 1"," "))
W2:W15W2=IF($U2="Pooled Employee","Select Pooled Location 2"," ")
X2:X15X2=IF($U2="Pooled Employee","Select Pooled Location 3"," ")
Y2:Y15Y2=IF($U2="Pooled Employee","Select Pooled Location 4"," ")
N2:N15N2=M2-L2+1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
V:VCell Valuecontains "Updated"textNO
V:YCell Valuecontains "Select"textNO
T1,T9,T13:T14,T16:T1048576Cell Valuecontains "Validation"textNO
Cells with Data Validation
CellAllowCriteria
U2:U15List=Dropdowns!$A$2:$A$4
V2:V15List=Dropdowns!$E$2:$E$15
W2:Y15List=Dropdowns!$E$2:$E$15
 

Attachments

  • Screenshot Comparison.PNG
    Screenshot Comparison.PNG
    38 KB · Views: 7
Upvote 0
To get the best result requires a bit of updating on your main spreadsheet.
I suggest that you keep the validation lists on the Sheet "Dropdowns".

Make each of the Dropdown lists an official Excel Table. Give the table a name, I suggest prefixing them all with "tbl".
Now create a Named Range for each table and point the Named Range at the table. I suggest prefixing the range names with "dd".
Now change all you validations in the main spreadsheet to use the Range Names eg List > ddLocation

This will have the added benefit that whenever you add additional items to the list the table will automatically expand and this will flow through to the validation options.

1643259805780.png


If you do the above then the code below should copy both the formulas and validation.
Note: It will copy the sheet Dropdowns to the new workbook, we can hide the sheet if that is what you want.

VBA Code:
Sub Split_Data_in_workbooks()

    Application.ScreenUpdating = False
    
    Dim data_sh As Worksheet
    Set data_sh = ThisWorkbook.Sheets("Q3-Q4")
    
    Dim setting_Sh As Worksheet
    Set setting_Sh = ThisWorkbook.Sheets("Settings")
    
    Dim nwb As Workbook
    Dim nsh As Worksheet
    
    setting_Sh.Range("A:A").Clear
    data_sh.AutoFilterMode = False
    data_sh.Range("B:B").Copy
    setting_Sh.Range("A1").PasteSpecial Paste:=xlPasteValues
    
    setting_Sh.Range("A:A").RemoveDuplicates 1, xlYes
    
    Dim i As Integer
    
    For i = 2 To Application.CountA(setting_Sh.Range("A:A"))
    
        data_sh.UsedRange.AutoFilter 2, setting_Sh.Range("A" & i).Value
        
        Set nwb = Workbooks.Add
        Set nsh = nwb.Sheets(1)
        nsh.Name = data_sh.Name
        ThisWorkbook.Worksheets("Dropdowns").Copy after:=nwb.Sheets(1)

        Application.DisplayAlerts = False
            data_sh.UsedRange.SpecialCells(xlCellTypeVisible).Copy
            nsh.Range("A1").PasteSpecial Paste:=xlPasteAll
        Application.DisplayAlerts = True
        nsh.UsedRange.EntireColumn.ColumnWidth = 15
    
        Application.CutCopyMode = False
        nsh.Activate
        nsh.Range("A1").Select

        nwb.SaveAs setting_Sh.Range("H6").Value & "/" & setting_Sh.Range("A" & i).Value & ".xlsx"
        nwb.Close False
        data_sh.AutoFilterMode = False
    Next i
    
    setting_Sh.Range("A:A").Clear
    
    MsgBox "Done"
    
    Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,874
Members
449,056
Latest member
ruhulaminappu

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