Hello,
I'm quite new to VBA and I just recently wrote this code to extract data from one main worksheet to copy/paste into each respective client worksheet. Contained in the workbook is a main worksheet with currently over 5000 entries and then the 14 separate client worksheets.
Two questions:
1. It looks like each time I run the macro, it duplicates the data in the client worksheets. What I'd like it to do is just overwrite the data each time I run it for an update.
2. Are there any unnecessary steps that I've included? I'd like it to run as fast as possible.
Thank you in advance for any help you can provide.
I'm quite new to VBA and I just recently wrote this code to extract data from one main worksheet to copy/paste into each respective client worksheet. Contained in the workbook is a main worksheet with currently over 5000 entries and then the 14 separate client worksheets.
Two questions:
1. It looks like each time I run the macro, it duplicates the data in the client worksheets. What I'd like it to do is just overwrite the data each time I run it for an update.
2. Are there any unnecessary steps that I've included? I'd like it to run as fast as possible.
Thank you in advance for any help you can provide.
Code:
Private Sub CommandButton1_Click()
a = Worksheets("main").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
If Worksheets("Main").Cells(i, 3).Value = "BA" Then
Worksheets("Main").Rows(i).Copy
Worksheets("BA").Activate
b = Worksheets("BA").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("BA").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Main").Activate
ElseIf Worksheets("Main").Cells(i, 3).Value = "CS" Then
Worksheets("Main").Rows(i).Copy
Worksheets("CS").Activate
b = Worksheets("CS").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("CS").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Main").Activate
ElseIf Worksheets("Main").Cells(i, 3).Value = "CT" Then
Worksheets("Main").Rows(i).Copy
Worksheets("CT").Activate
b = Worksheets("CT").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("CT").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("main").Activate
ElseIf Worksheets("Main").Cells(i, 3).Value = "DE" Then
Worksheets("Main").Rows(i).Copy
Worksheets("DE").Activate
b = Worksheets("DE").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("DE").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Main").Activate
ElseIf Worksheets("Main").Cells(i, 3).Value = "DM" Then
Worksheets("Main").Rows(i).Copy
Worksheets("DM").Activate
b = Worksheets("DM").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("DM").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Main").Activate
ElseIf Worksheets("Main").Cells(i, 3).Value = "GM" Then
Worksheets("Main").Rows(i).Copy
Worksheets("GM").Activate
b = Worksheets("GM").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("GM").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Main").Activate
ElseIf Worksheets("Main").Cells(i, 3).Value = "JB" Then
Worksheets("Main").Rows(i).Copy
Worksheets("JB").Activate
b = Worksheets("JB").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("JB").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Main").Activate
ElseIf Worksheets("Main").Cells(i, 3).Value = "KJ" Then
Worksheets("Main").Rows(i).Copy
Worksheets("KJ").Activate
b = Worksheets("KJ").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("KJ").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Main").Activate
ElseIf Worksheets("Main").Cells(i, 3).Value = "KO" Then
Worksheets("Main").Rows(i).Copy
Worksheets("KO").Activate
b = Worksheets("KO").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("KO").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Main").Activate
ElseIf Worksheets("Main").Cells(i, 3).Value = "KP" Then
Worksheets("Main").Rows(i).Copy
Worksheets("KP").Activate
b = Worksheets("KP").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("KP").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Main").Activate
ElseIf Worksheets("Main").Cells(i, 3).Value = "ML" Then
Worksheets("Main").Rows(i).Copy
Worksheets("ML").Activate
b = Worksheets("ML").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("ML").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Main").Activate
ElseIf Worksheets("Main").Cells(i, 3).Value = "RD" Then
Worksheets("Main").Rows(i).Copy
Worksheets("RD").Activate
b = Worksheets("RD").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("RD").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Main").Activate
ElseIf Worksheets("Main").Cells(i, 3).Value = "SS" Then
Worksheets("Main").Rows(i).Copy
Worksheets("SS").Activate
b = Worksheets("SS").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("SS").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Main").Activate
ElseIf Worksheets("Main").Cells(i, 3).Value = "ST" Then
Worksheets("Main").Rows(i).Copy
Worksheets("ST").Activate
b = Worksheets("ST").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("ST").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Main").Activate
End If
Next
Application.CutCopyMode = False
ThisWorkbook.Worksheets("Main").Cells(1, 1).Select
End Sub