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]
 
Hi

Thank you for that, I will go and have a good read of that, just the last point, how do we know all data is copied across? would it tell us if any were missed?
 
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
As long as there is a value in col D then it should get copied.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
Hi again

Just been playing around with the workbook. So we ran a few scenarios and this is what happened. We added a new record with a new company name, we clicked the macro and it worked ok, then we changed the name of the entry to an existing company and re ran the macro, it didn't update the record to anywhere and left the old entry on the new sheet?

We also edited an existing record using Find and replace to change a company name to a new name, it created a new sheet with no records and left the existing sheet in place, it also created a blank sheet called company. The other thing is the macro I have on the master sheet was copied to all the new pages it created, but didn't assign the macro (i.e. didn't want the button copying across).

Regards

Stephen
 
Upvote 0
Hi also noted that when you run the macro a date is shown in column M and if i try to place a filter on say line 4 and try to filter what is column G, it doesn't work?
 
Upvote 0
The code needs to go in a standard module, not the sheet module.
then we changed the name of the entry to an existing company and re ran the macro, it didn't update the record to anywhere and left the old entry on the new sheet?
That's what I would expect to happen.

If you are editing existing entries & want lines added & deleted accordingly, including deleting sheets, then you will need completely different code.
In which case you will need to start a new thread & explain from the outset, EXACTLY what you need to happen & when.

I cannot replicate a scenario where the shape used to run a macro is copied across to each new sheet.
This will correct some of the other problems
Code:
Sub sbrown64()
   Dim Cl As Range
   Dim Ky As Variant
   Dim Ws As Worksheet
   Dim UsdRws As Long
   
   On Error GoTo Xit
   Application.ScreenUpdating = False
   Set Ws = Sheets(1)
   If Ws.AutoFilterMode Then Ws.AutoFilterMode = False
   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:M" & UsdRws).AutoFilter 4, Ky
         Ws.Range("A4:M" & UsdRws).AutoFilter 13, ""
         If Not Evaluate("isref('" & Left(Ky, 30) & "'!A1)") Then
            Sheets.Add(, Sheets(1)).Name = Left(Ky, 30)
            Ws.Range("A1:A" & UsdRws).SpecialCells(xlVisible).EntireRow.Copy Sheets(Left(Ky, 30)).Range("A1")
            Ws.Range("M5:M" & UsdRws).SpecialCells(xlVisible).Value = Date
         Else
            On Error Resume Next
            Ws.Range("A5:A" & UsdRws).SpecialCells(xlVisible).EntireRow.Copy Sheets(Left(Ky, 30)).Range("D" & Rows.Count).End(xlUp).Offset(1, -3)
            Ws.Range("M5:M" & UsdRws).SpecialCells(xlVisible).Value = Date
            On Error GoTo Xit
         End If
      Next Ky
   End With
   Ws.AutoFilterMode = False
   Exit Sub
Xit:
   Ws.AutoFilterMode = False
   MsgBox "The macro encountered an error" & vbLf & "Error " & Err.Number & " " & Err.Description
End Sub
 
Upvote 0
Hi

I have just pasted the new code and it works, with regards to editing and deleting lines/tabs i will start a new thread.

Thank you for all your work.

Regards

Stephen
 
Upvote 0
Hi again

Just a quick question about this post
I also notice that one of the company names is longer than the max allowed length for a sheet name, so I have capped it 30 characters.

Can you point out where that code is? as is may be useful going forward? i.e. is it a case of changing the number?

Regards

Stephen
 
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