Copying rows from one sheet to 2 other sheets

itzlaforever

New Member
Joined
Jun 1, 2022
Messages
24
Office Version
  1. 2019
Platform
  1. Windows
Hi! Looking for some help with macros. I've never used them before. Basically, I need a list of employees (Index Worksheet) to be split on two separate worksheets (IndexDriver worksheet, IndexLaborer worksheet). I have the code to complete this task, but I need it to not duplicate these rows when the macro is ran again... Sorry if I am being too vague, I am new to this. Here is my code..

Sub CopyRowBasedOnCellValue()

Dim R1 As Range
Dim R2 As Range
Dim I As Long
Dim J As Long
Dim K As Long
I = Worksheets("Index").UsedRange.Rows.Count
J = Worksheets("IndexDriver").UsedRange.Rows.Count
If J = D Then
If Application.WorksheetFunction.CountA(Worksheets("IndexDriver").UsedRange) = 0 Then J = 0
End If
Set R1 = Worksheets("Index").Range("B1:B" & I)
On Error Resume Next
Application.ScreenUpdating = False
For K = 1 To R1.Count
If CStr(R1(K).Value) = "D" Then
R1(K).EntireRow.Copy Destination:=Worksheets("IndexDriver").Range("A" & J + 1)
J = J + 1
End If
Next
Application.ScreenUpdating = True



I = Worksheets("Index").UsedRange.Rows.Count
J = Worksheets("IndexLaborer").UsedRange.Rows.Count
If J = D Then
If Application.WorksheetFunction.CountA(Worksheets("IndexLaborer").UsedRange) = 0 Then J = 0
End If
Set R1 = Worksheets("Index").Range("B1:B" & I)
On Error Resume Next
Application.ScreenUpdating = False
For K = 1 To R1.Count
If CStr(R1(K).Value) = "1" Then
R1(K).EntireRow.Copy Destination:=Worksheets("IndexLaborer").Range("A" & J + 1)
J = J + 1
End If
Next
Application.ScreenUpdating = True
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Copy of Employee Workbook.xlsm
ABCDEFGHIJKLMNOPQRS
1BothBothDriverBothDriverDriverDriverDriverDriverBothDriverDriverDriverDriverBothBoth
2Emp IDDriver(D)/Laborer(1)Last NameFirst NameDivisionDOBDOHCDL or DLDRIVER LIC #STATELIC. EXPHazmat ExpMed CardTateCar InsMemorandumFMCSAE-VerifyHandbook
3242DAlfordPatrick
4115DChanthalaTor
5314DClementeFermin
6125DCustalow-HallMichael
7246DDiazJose
8249DGrayMario
9130DGreenCharles
10290DHigginsRicky
11253DHolderRussell
12266DHopkinsRusty
13254DJamersonTJ
14255DJonesLarry
15256DLopez-NunfioJesus
16150DOwingsChristopher
17225DPerez-RochinEnrique
18261DRaderAndy
19262DRomeroAngel
20185DSimonpietriSean
21284DSlobodienPhillip
22195DWalshReid
23303DYoungJustin
24307DMaloneGeorge
25302DPhonesavanhTimmy
263161AkpoghenoborHarid
272681AldanaEnrique
283011BountulayVanxay
292991Craft-WiseJuJuan
303151Custalow-HallNathan
312501HarveyLonnie
322521HernandezAlexis
333111KimThomas
342571LopezJesus (uncle J)
352691MiddlebrooksAshton
361451NongSouk
372941QuinonesMark
381651SanchezEric
393121SaysanaVanh
401801SimonpietriSamuel
4110001SpruhillVannie
423131WatsonMichael
432401WinesDaniel
Index
 
Upvote 0
Hello Itzlaforever,

Now that you've uploaded your file sample, you'll find that the code amended as follows will do the task for you:-

VBA Code:
Option Explicit
Sub Test()

        Dim wsI As Worksheet, wsD As Worksheet, i As Long
        Dim ar As Variant: ar = [{"IndexDriver","IndexLaborer";"D","1"}]
        Set wsI = Sheets("Index")

Application.ScreenUpdating = False

        For i = 1 To UBound(ar, 2)
                     Set wsD = Sheets(ar(1, i))
                     wsD.UsedRange.Clear
                    
              With wsI.Range("B2", wsI.Range("B" & wsI.Rows.Count).End(xlUp))
                      .AutoFilter 1, ar(2, i)
                      .EntireRow.Copy wsD.[A1]
                      .AutoFilter
                      wsD.Columns.AutoFit
              End With
        Next i

Application.ScreenUpdating = True

End Sub

The row 2 headings are copied over to the destination sheets as well.

Cheerio,
vcoolio.
 
Upvote 0
Unfortunately,

That didn't copy anything but the first row (headers). Should I be adding this to my original code instead of replacing it?
 
Upvote 0
Hello Itzlaforever,

I've attached your sample workbook here with the code implemented. You'll note that the code works just fine. Click on the "RUN" button to see it work.

Just a couple of questions:-
- Is your data formatted as a formal Excel table?
- If so, what is it named?

Cheerio,
vcoolio.
 
Upvote 0
............................if it is a table then the following code will work:-
VBA Code:
Option Explicit
Sub Test()

        Dim wsI As Worksheet, wsD As Worksheet, i As Long
        Dim ar As Variant: ar = [{"IndexDriver","IndexLaborer";"D","1"}]
        Set wsI = Sheets("Index")
        
Application.ScreenUpdating = False

        For i = 1 To UBound(ar, 2)
                     Set wsD = Sheets(ar(1, i))
                     wsD.UsedRange.Clear
                    
              With wsI.ListObjects("Table1").Range
                      .AutoFilter 2, ar(2, i)
                      .EntireRow.Copy wsD.[A1]
                      If wsI.FilterMode = True Then
                            wsI.ShowAllData
                      End If
              End With
              wsD.Columns.AutoFit
        Next i

Application.ScreenUpdating = True

End Sub

If your table has a different name, then you'll need to change"Table1" to suit.
This code is a replacement for the code in your opening post.

Cheerio,
vcoolio.
 
Last edited:
Upvote 0
Hello Itzlaforever,

I've attached your sample workbook here with the code implemented. You'll note that the code works just fine. Click on the "RUN" button to see it work.

Just a couple of questions:-
- Is your data formatted as a formal Excel table?
- If so, what is it named?

Cheerio,
vcoolio.
Yes it is formatted as a formal Excel Table. The data is in the table named "index"
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,173
Members
453,021
Latest member
Justyna P

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