copying cells from sheet 1 to various other sheets

sbrown64

Board Regular
Joined
Aug 23, 2019
Messages
87
Hi

I have a spreadsheet which has over 1000 lines, all for varying companies. What I would like to do is create an option so when you run it, it will copy the relevant lines to their respective sheet. The code listed below is some I have seen on here, which works to a point. It will run for one company, bit if I press run again it adds the same ones' to sheet 2. The other point is how to make it do if for ALL companies and not just the one? I hope you can help.

Regards

Stephen

[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Sub Copy_Bd()
Application.ScreenUpdating = False
Dim i As Integer
Dim Lastrow As Long
Dim Lastrowa As Long[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Sheets(1).Activate
Lastrow = Cells(Rows.Count, "F").End(xlUp).Row
Lastrowa = Sheets(2).Cells(Rows.Count, "F").End(xlUp).Row + 1[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif] For i = 1 To Lastrow
If Cells(i, 4).Value = "Bd" Then
Rows(i).Copy Destination:=Sheets(2).Rows(Lastrowa)
Lastrowa = Lastrowa + 1
End If
Next
Cells(1, 1).Select
Application.ScreenUpdating = True
End Sub[/FONT]
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Ok, how about
Code:
Sub sbrown64()
   Dim Cl As Range
   Dim Ky As Variant
   Dim Ws As Worksheet
   Dim UsdRws As Long
   
   Set Ws = Sheets("sheet1")
   UsdRws = Ws.Range("D" & Rows.Count).End(xlUp).Row
   With CreateObject("Scripting.dictionary")
      .CompareMode = vbTextCompare
      For Each Cl In Ws.Range("D5:D" & UsdRws)
         If Cl.Value <> "" Then .item(Cl.Value) = Empty
      Next Cl
      For Each Ky In .Keys
         Ws.Range("A4:D" & UsdRws).AutoFilter 4, Ky
         If Not Evaluate("isref('" & Ky & "'!A1)") Then
            Sheets.Add(, Sheets(1)).Name = Ky
            Ws.Range("A1:A" & UsdRws).EntireRow.Copy Sheets(Ky).Range("A1")
         Else
            Ws.Range("A5:A" & UsdRws).EntireRow.Copy Sheets(Ky).Range("D" & Rows.Count).End(xlUp).Offset(1, -3)
         End If
      Next Ky
   End With
   Ws.AutoFilterMode = False
End Sub
 
Upvote 0
Just noticed your edit to post#30, so try this instead
Code:
Sub sbrown64()
   Dim Cl As Range
   Dim Ky As Variant
   Dim Ws As Worksheet
   Dim UsdRws As Long
   
   Set Ws = Sheets("sheet1")
   UsdRws = Ws.Range("D" & Rows.Count).End(xlUp).Row
   With CreateObject("Scripting.dictionary")
      .CompareMode = vbTextCompare
      For Each Cl In Ws.Range("D5:D" & UsdRws)
         If Cl.Value <> "" Then .item(Trim(Cl.Value)) = Empty
      Next Cl
      For Each Ky In .Keys
         Ws.Range("A4:D" & UsdRws).AutoFilter 4, Ky
         If Not Evaluate("isref('" & Ky & "'!A1)") Then
            Sheets.Add(, Sheets(1)).Name = Ky
            Ws.Range("A1:A" & UsdRws).EntireRow.Copy Sheets(Ky).Range("A1")
         Else
            Ws.Range("A5:A" & UsdRws).EntireRow.Copy Sheets(Ky).Range("D" & Rows.Count).End(xlUp).Offset(1, -3)
         End If
      Next Ky
   End With
   Ws.AutoFilterMode = False
End Sub
 
Upvote 0
Hi

Just ran the new code and it worked, all the data copied to their own sheets. I then went to the major sheet and tried entering a new line with a new company and ran the code and it created a new page with the data, however I noticed one of the other companies had now double info (duplicated)
 
Upvote 0
Will you be adding new rows to the Master sheet, or will you be replacing it with new data?
If the former, are you happy to delete the info on the company sheets & then copy all the data over again?
 
Upvote 0
the master sheet will be an going sheet. The one we are working on is a one page spreadsheet with 30 entries, the original has approx. 1000 lines with about 60 companies involved.
 
Upvote 0
Can you please post the link to the thread, as per forum rules
Thanks
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,307
Members
452,633
Latest member
DougMo

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