Last row problem again

PeterJohns27

New Member
Joined
Jun 17, 2014
Messages
22
After earlier post this week, managed to correct a last row problem with a little help from Alex. This current macro works fine upto the last line, where despite a +1 , it continues to paste into the same row!! what silly mistake / error this time
Rich (BB code):
Sub Ekkie()
Dim WsI As Worksheet
Dim WsT As Worksheet
Dim WsR As Worksheet
Dim WsL As Worksheet
Dim I As Integer
Dim T As Integer
Dim lastRowWsT As Long
Dim lastRowWsL As Long
Dim lastRowWsR As Long

Set WsT = Worksheets("Template")
Set WsI = Worksheets("Interim")
Set WsR = Worksheets("Results")
Set WsL = Worksheets("Latest")

lastRowWsT = WsT.Cells(Rows.Count, "A").End(xlUp).Row
lastRowWsL = WsL.Cells(Rows.Count, "B").End(xlUp).Row
lastRowWsR = WsR.Cells(Rows.Count, "B").End(xlUp).Row

Application.ScreenUpdating = True

For T = 2 To lastRowWsT
'T = Range("A2:A")
  With WsT
'Sheets("Template").Select
    
    .Range("A" & T, "T" & T).Copy
    
    End With
    'Sheets("Interim").Select
    With WsI
    
    .Range("B3:U3").PasteSpecial xlPasteValues
    
    .Range("B3:C3").Copy
    .Range("B4:C4").PasteSpecial xlPasteValues
    .Range("B4:U4").Copy
    
    End With
    
    With WsR
    'Sheets("Results").Select
    .Range("B" & lastRowWsR + 1).PasteSpecial Paste:=xlPasteAll
    
    End With
    
    Application.CutCopyMode = False
         
  Next T
    
 Application.ScreenUpdating = False



End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
If you put a breakpoint on the line
VBA Code:
.Range("B" & lastRowWsR + 1).PasteSpecial Paste:=xlPasteAll
run your code and then hover your mouse over lastRowWsR, does the number that appears match the last last row number?

Btw, not that I think it makes any difference to this issue but your
VBA Code:
 Application.ScreenUpdating = False

and

VBA Code:
Application.ScreenUpdating = True

are the wrong way around
 
Upvote 0
If you put a breakpoint on the line
VBA Code:
.Range("B" & lastRowWsR + 1).PasteSpecial Paste:=xlPasteAll
run your code and then hover your mouse over lastRowWsR, does the number that appears match the last last row number?

Btw, not that I think it makes any difference to this issue but your
VBA Code:
 Application.ScreenUpdating = False

and

VBA Code:
Application.ScreenUpdating = True

are the wrong way around
It Shows the correct row but doesn't change after paste command has completed. stays the same. I tried +2 to see if that made a difference but same result. Correct row selected but no change on subsequent loop around. Baffled to say the least
 
Upvote 0
It Shows the correct row but doesn't change after paste command has completed. stays the same. I tried +2 to see if that made a difference but same result. Correct row selected but no change on subsequent loop around. Baffled to say the least
You need to move the line defining the last row within the loop for it to change on each loop.

VBA Code:
    With WsR
         lastRowWsR = .Cells(Rows.Count, "B").End(xlUp).Row
         .Range("B" & lastRowWsR + 1).PasteSpecial Paste:=xlPasteAll
    
    End With
 
Upvote 0
Solution
You need to move the line defining the last row within the loop for it to change on each loop.

VBA Code:
    With WsR
         lastRowWsR = .Cells(Rows.Count, "B").End(xlUp).Row
         .Range("B" & lastRowWsR + 1).PasteSpecial Paste:=xlPasteAll
   
    End With
Mark, many thanks . worked a treat. simple when you how!!
 
Upvote 0

Forum statistics

Threads
1,224,619
Messages
6,179,924
Members
452,949
Latest member
beartooth91

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