How to Copy Filtered Excel Data to Day and Night Shift Sheets Using VBA
Question details
The user needs a VBA macro to distribute filtered names from a main sheet (POSTO A) into specific Day Shift, Night Shift, or both sections based on criteria.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating shift schedules where filtered staff records must be distributed to appropriate day or night shift sheets according to their shift codes (SD, SN, PL).
- Observed behavior
- The user wants a script that clears existing data, processes only visible filtered rows, and accurately places records into the next available rows of the destination sheets without duplicating hidden rows.
Before running a VBA macro that clears destination ranges and copies data, ensure you have saved a backup copy of your Excel workbook to prevent accidental data loss.
Automate Data Distribution with a VBA Macro
Use a VBA script to loop through visible filtered rows and conditionally copy data to different destination sheets based on SD, SN, and PL criteria.
A structured VBA approach ensures that only visible rows are processed, keeping hidden or filtered-out data intact. By looping through these rows, you can apply conditional statements to send SD values to the Day Shift, SN values to the Night Shift, and PL values to both.
It is crucial to fully qualify your source and destination ranges with their specific worksheet names and to clear the destination areas before pasting to avoid overlapping old shift records.
Press 'Alt + F11' on your keyboard to launch the Visual Basic for Applications (VBA) editor in Excel.
In the top menu, click 'Insert' and select 'Module'. This will create a blank script window where you can write your macro.
Write your code using 'Range.SpecialCells(xlCellTypeVisible)' to only loop through filtered data. Use 'If criterion = "SD" Then' to assign rows to the Day Shift sheet, 'ElseIf criterion = "SN" Then' for the Night Shift, and handle "PL" by copying to both.
Inside your loop, use a formula like 'Cells(Rows.Count, "A").End(xlUp).Row + 1' to identify the next empty row on your destination sheets, ensuring new data is appended correctly.
Close the VBA editor and return to Excel. Press 'Alt + F8', select your newly created macro, and click 'Run' to distribute the filtered POSTO A data to your shift sheets.

Distribute Shift Data Automatically with WPS Spreadsheet
WPS Spreadsheet fully supports running VBA macros and opening .xlsm files, allowing you to execute complex data-sorting scripts like shift distributions effortlessly.
- 1. Open your File in WPS: Launch WPS Spreadsheet and open your macro-enabled shift schedule file (.xlsm).
- 2. Enable the Developer Tab: Navigate to the 'Developer' tab on the top ribbon. If it is hidden, you can enable it via Menu > Options > Customize Ribbon.
- 3. Access the Macros Menu: Click the 'Macros' button in the Developer ribbon or press 'Alt + F8' to view your available VBA scripts.
- 4. Execute the Shift Macro: Select your shift-sorting macro from the list and click 'Run' to automatically distribute the SD, SN, and PL records to the correct sheets.

Frequently Asked Questions
Why is my macro copying hidden or filtered-out rows?
This happens if your macro iterates over the entire range instead of just the visible cells. You must append .SpecialCells(xlCellTypeVisible) to your source range in the VBA code to ensure only the currently filtered records are processed.
How do I find the next available empty row in VBA?
You can find the next empty row by navigating from the bottom of the worksheet upwards. Use the VBA syntax Cells(Rows.Count, "A").End(xlUp).Row + 1 to locate the first empty cell in column A of your target sheet.
What does it mean to fully qualify a range in VBA?
Fully qualifying a range means explicitly stating which workbook and worksheet the range belongs to (e.g., ThisWorkbook.Worksheets("POSTO A").Range("A1")). Without this, VBA might apply the command to the active sheet by mistake, causing data to copy incorrectly.
How do I ensure my destination ranges are cleared before copying new data?
Add a line of code at the very beginning of your macro execution that clears the target area, such as Worksheets("Day Shift").Range("A2:D500").ClearContents. This wipes old records before the looping process pastes the new shift assignments.




