Help In Existing Code In Resize

Haree

Board Regular
Joined
Sep 22, 2019
Messages
146
Office Version
  1. 2016
Hi all, I am attaching my current Code. It works perfectly fine. But due to certain changes I have to modify the code to copy 4 columns instead of 5 . When I change that part, The paste section is looping fully.

VBA Code:
Sub Copy_Rows()
Application.ScreenUpdating = False
Dim R As Range, Cell As Range
Set R = Range("H2:H500")
For Each Cell In R
If Cell.Value = "Error" Then
MsgBox "Kindly Check Errors and try again"
Exit Sub
End If
Next Cell
Dim Drange As Range
Dim psheet As Worksheet
Set Drange = Range("A2:E500")
For Each psheet In Worksheets
psheet.Unprotect Password:="STOCK"
Next psheet
Sheets("Data Sheet").Activate
Dim i As Long
Dim Lastrow As Long
Lastrow = Sheets("Data Sheet").Cells(Rows.Count, "A").End(xlUp).Row
Dim Lastrowa As Long
For i = 2 To Lastrow
Lastrowa = Sheets(Cells(i, 1).Value).Cells(Rows.Count, "A").End(xlUp).Row + 1
Cells(i, 2).Resize(, 5).Copy Sheets(Cells(i, 1).Value).Rows(Lastrowa)
Next
Drange.ClearContents
For Each psheet In Worksheets
If psheet.Name <> "Data Sheet" And psheet.Name <> "Daily Extract" Then
psheet.Protect Password:="STOCK", AllowFormattingCells:=True, DrawingObjects:=False, Scenarios:=True
Else
psheet.Unprotect Password:="STOCK"
End If
Next psheet
MsgBox "Data Updated Successfully"
Application.ScreenUpdating = True
End Sub


The place where I am changing is
Code:
For i = 2 To Lastrow
Lastrowa = Sheets(Cells(i, 1).Value).Cells(Rows.Count, "A").End(xlUp).Row + 1
Cells(i, 2).Resize(, 5).Copy Sheets(Cells(i, 1).Value).Rows(Lastrowa)
Next

I am changing the Resize Portion.
Any help would be greatly appreciated.
Many Thanks

BEFORE EDITING
Gold Stock.xlsm
ABCD
7014-07-2022Sales14.03
7115-07-2022Sales154.63
7221-07-2022Sales112.14
Chains
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B72Cell Valuecontains "Sales"textNO
B72Cell Valuecontains "Purchase"textNO
B71Cell Valuecontains "Sales"textNO
B71Cell Valuecontains "Purchase"textNO
B70Cell Valuecontains "Sales"textNO
B70Cell Valuecontains "Purchase"textNO


AFTER EDITING Resize to 4

Gold Stock.xlsm
ABCDEGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAK
8922-07-2022Sales1122-07-202211#######Sales11#######Sales1122-07-2022Sales1122-07-2022Sales11#######Sales11#######Sales11#######Sales11#######
Rings
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B89,F89,J89,N89,R89,V89,Z89,AD89,AH89,AL89,AP89,AT89,AX89,BB89,BF89,BJ89,BN89,BR89,BV89,BZ89,CD89,CH89,CL89,CP89,CT89,CX89,DB89,DF89,DJ89,DN89,DR89,DV89,DZ89,ED89,EH89,EL89,EP89,ET89,EX89,FB89,FF89,FJ89,FN89,FR89,FV89,FZ89,GD89,GH89,GL89,GP89,GT89,GX89,HB89Cell Valuecontains "Sales"textNO
B89,F89,J89,N89,R89,V89,Z89,AD89,AH89,AL89,AP89,AT89,AX89,BB89,BF89,BJ89,BN89,BR89,BV89,BZ89,CD89,CH89,CL89,CP89,CT89,CX89,DB89,DF89,DJ89,DN89,DR89,DV89,DZ89,ED89,EH89,EL89,EP89,ET89,EX89,FB89,FF89,FJ89,FN89,FR89,FV89,FZ89,GD89,GH89,GL89,GP89,GT89,GX89,HB89Cell Valuecontains "Purchase"textNO
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try
VBA Code:
Cells(i, 2).Resize(, 4).Copy Sheets(Cells(i, 1).Value).Cells(Lastrowa,1)
 
Upvote 0
Solution
Hi @Fluff
Worked perfectly as always.
Many Thanks.

Just for learning can I please know why it was working fine with 5 and not with 4. I understood that the basic syntax was wrong and I was referring to the entire Row.
 
Upvote 0
I'm surprised it worked for 5 columns, so have no idea why it did.
 
Upvote 0

Forum statistics

Threads
1,215,241
Messages
6,123,823
Members
449,127
Latest member
Cyko

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