Copy and Paste based on cell value

Rogerisit

Board Regular
Joined
Oct 20, 2016
Messages
70
Office Version
  1. 2019
Hi

Part 2 of this isn't working (For i = 2 to last row on) I'm trying to copy and paste relevant cells from each row that is the same as MyCell value which is what I have named each new tab.

VBA Code:
  For Each MyCell In MyRange
        Sheets.Add After:=Sheets(Sheets.Count) 'creates a new worksheet
        Sheets(Sheets.Count).Name = MyCell.Value ' renames the new worksheet
    Next MyCell
  
    lastrow = ws.Range("a" & Rows.Count).End(xlUp).Row
  
    For i = 2 To lastrow
        If ws.Range("d2:d") = MyCell.Value And _
        ws.Range("d2:d").Offset(1, 0) <> "Miscellaneous" _
        And ws.Range("d2:d").Offset(5, 0) <> "" Then
        ws.Range("A" & i & ":F" & i & ",G" & i & ":H" & i).Copy
        On Error Resume Next
    Sheets(MyCell.Value).Select
    ActiveSheet.PasteSpecial Paste:=xlPasteValues
        End If
    Next i

Any help welcome thanks.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hello @Rogerisit

It's not clear to me what you're trying to do. You didn't put your entire macro, so we have to assume a few things. :unsure:
You also didn't put examples of what you have in your sheets, what you have in MyRange and what you expect as a result.

Without that information it is very risky to give you a complete solution. But I will take the risk because I am an adventurer. 😅

Replace all your code, at least what you put, with the following:
VBA Code:
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
 
  For Each MyCell In MyRange
    On Error Resume Next: Sheets(MyCell.Value).Delete: On Error GoTo 0
    Sheets.Add After:=Sheets(Sheets.Count) 'creates a new worksheet
    Sheets(Sheets.Count).Name = MyCell.Value ' renames the new worksheet
   
    For i = 2 To ws.Range("A" & Rows.Count).End(xlUp).Row
      If ws.Range("D" & i).Value = MyCell.Value And _
         ws.Range("D" & i).Offset(1, 0) <> "Miscellaneous" And _
         ws.Range("D" & i).Offset(5, 0) <> "" Then

        ws.Range("A" & i & ":H" & i).Copy
        With Sheets(MyCell.Value)
          lr2 = .Range("A" & Rows.Count).End(xlUp).Row + 1
          .Range("A" & lr2).PasteSpecial Paste:=xlPasteValues
        End With

      End If
    Next i
  Next MyCell
 
  Application.ScreenUpdating = True
  Application.DisplayAlerts = True

--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
 
Upvote 0
Solution
If you have a problem with ranges you set not working out, you can put that range into a short macro like so
Code:
Sub Test()
Range("D2:D").Select
End Sub
and run it. You'll see that it errors so fix that and see what happens.
 
Upvote 0
Hello @Rogerisit

It's not clear to me what you're trying to do. You didn't put your entire macro, so we have to assume a few things. :unsure:
You also didn't put examples of what you have in your sheets, what you have in MyRange and what you expect as a result.

Without that information it is very risky to give you a complete solution. But I will take the risk because I am an adventurer. 😅

Replace all your code, at least what you put, with the following:
VBA Code:
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
 
  For Each MyCell In MyRange
    On Error Resume Next: Sheets(MyCell.Value).Delete: On Error GoTo 0
    Sheets.Add After:=Sheets(Sheets.Count) 'creates a new worksheet
    Sheets(Sheets.Count).Name = MyCell.Value ' renames the new worksheet
  
    For i = 2 To ws.Range("A" & Rows.Count).End(xlUp).Row
      If ws.Range("D" & i).Value = MyCell.Value And _
         ws.Range("D" & i).Offset(1, 0) <> "Miscellaneous" And _
         ws.Range("D" & i).Offset(5, 0) <> "" Then

        ws.Range("A" & i & ":H" & i).Copy
        With Sheets(MyCell.Value)
          lr2 = .Range("A" & Rows.Count).End(xlUp).Row + 1
          .Range("A" & lr2).PasteSpecial Paste:=xlPasteValues
        End With

      End If
    Next i
  Next MyCell
 
  Application.ScreenUpdating = True
  Application.DisplayAlerts = True

--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
Sorry for lack of info, but it seems you can read my mind. Thanks very much.
 
Upvote 0

Forum statistics

Threads
1,214,865
Messages
6,121,988
Members
449,060
Latest member
mtsheetz

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