VBA Last Row Question

ummjay

Board Regular
Joined
Oct 1, 2010
Messages
193
Hi!

I have some logic which will copy and paste into a range using last row. that works fine:
VBA Code:
lastrow = ActiveSheet.Range("AY7:BB" & Rows.Count).Find("").Row - 1
Set CopyRng = Range("AY7:BB" & lastrow)
CopyRng.Copy
Range("AQ7:AT" & lastrow).PasteSpecial xlPasteValues

I then want to re-copy that and paste it below the paste values above. this also works fine:
VBA Code:
'last row of actual data
lastrowadj = ActiveSheet.Range("AQ7:AT" & Rows.Count).Find("").Row
'copy original range/paste orignal range below 1st paste
CopyRng.Copy
Range("AQ" & lastrowadj & ":AT" & lastrowadj).PasteSpecial xlPasteValues

The issue I have is, then I want to replace cells on the 2nd data set I pasted, and that doesnt seem to be working properly. it only replaces the 1st line of that range, and not the ones beneath it:
VBA Code:
'final last row data
lastrowfinal = ActiveSheet.Range("AQ7:AT" & Rows.Count).Find("").Row
range("AQ" & lastrowadj & ":AQ" & lastrowfinal).Replace What:="S", Replacement:="Sam", LookAt:=xlWhole
Range("AQ" & lastrowadj & ":AQ" & lastrowfinal).Replace What:="B", Replacement:="Boy", LookAt:=xlWhole

Any ideas?
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Check the values of "lastrowadj" and "lastrowfinal" when you get to those replacement lines to confirm they are what you think they are.
There are numerous ways to check those values:
- use a MsgBox to return their values
- go through the code line-by-line and hover over those variables when you get to that line to see what the values are
- use the Immediate Window
 
Upvote 0
VBA Code:
lastrowfinal = ActiveSheet.Range("AQ7:AT" & Rows.Count).Find("").Row
That doesn't look like a very reliable way of finding the last row.

You would normally find the last row with data then offset it by one row

For instance see the results of the Message boxes with the data below and the 2 code lines (please note you still need to carry out the testing of your variables proposed by Joe4 in post 2)

Book1
AQARASAT
7Order IDOrder DateSiteSP Name
8108-4-N7004/02/2014On SiteSteven Buchanan
9108-13-F8613/02/2014Off SiteNancy Davolio
10108-11-N8211/02/2014On SiteMargaret Peacock
11107-22-N1422/10/2013On SiteSteven Buchanan
12108-14-N3214/01/2014On SiteAndrew Fuller
13108-15-N3315/01/2014On SiteMichael Suyama
14106-1-F1801/08/2013Off SiteNancy Davolio
15110-8-F0908/04/2014Off SiteAndrew Fuller
16110-6-N0106/04/2014On SiteAndrew Fuller
17107-29-F2229/10/2013Off SiteLaura Callahan
18102-23-N6323/07/2012On SiteAnne Dodsworth
19106-19-N7519/09/2013On SiteSteven Buchanan
20109-31-F8931/03/2014Off SiteAndrew Fuller
2101/04/2014
2202/04/2014
Sheet1


VBA Code:
Sub xxxx()
MsgBox ActiveSheet.Range("AQ7:AT" & Rows.Count).Find("").Row
MsgBox Range("AQ7:AT" & Rows.Count).Find("*", , xlValues, , xlByRows, xlPrevious).Row
End Sub

and even worse if there are any blank cells in the block of data

Book1
AQARASAT
7Order IDOrder DateSiteSP Name
8108-4-N7004/02/2014On SiteSteven Buchanan
9108-13-F8613/02/2014Off SiteNancy Davolio
10108-11-N8211/02/2014On SiteMargaret Peacock
11107-22-N1422/10/2013On SiteSteven Buchanan
12108-14-N3214/01/2014On SiteAndrew Fuller
13108-15-N3315/01/2014On SiteMichael Suyama
14106-1-F18Off SiteNancy Davolio
15110-8-F0908/04/2014Off SiteAndrew Fuller
16110-6-N0106/04/2014On SiteAndrew Fuller
17107-29-F2229/10/2013Off SiteLaura Callahan
18102-23-N6323/07/2012On SiteAnne Dodsworth
19106-19-N7519/09/2013On SiteSteven Buchanan
20109-31-F8931/03/2014Off SiteAndrew Fuller
2101/04/2014
2202/04/2014
Sheet1
 
Last edited:
Upvote 0
thanks, yea, so I think the problem I'm having specifically, is being able to determine the 2nd range I made/pasted, and doing a find/replace on certain values within that range, and NOT the 1st range I pasted. I was trying to say lastrowfinal , is end of both data ranges combined to determine the end, and using lastrowadj as the START of the 2nd data range (or technically end of the 1st).

hope that's not confusing, is there a better way to do that?
 
Upvote 0
and NOT the 1st range I pasted
You are using the same method in each piece of code that you posted. Have you checked what your variables are returning as Joe4 suggested?
 
Upvote 0
I get a value of 13 in the msg box, but not sure what that actually relates to:

VBA Code:
'last row of actual data
lastrowadj = ActiveSheet.Range("AQ6:AT" & Rows.Count).Find("").Row
MsgBox ActiveSheet.Range("AQ6:AT" & Rows.Count).Find("").Row

the data it pasted is correct, which should be 6, AQ7:A12, unless it counts every cell in the entire range? from AQ7:AT12? In that case, should the number be count 24?
 
Upvote 0
but not sure what that actually relates to:
The first blank cell in the range "AQ6:AT" & Rows.Count going in a horizontal zig-zag starting from cell AQ6

Not knowing what your data looks like, what happens with the code below (Please note the code below uses AQ7 whereas you have changed it to AQ6 in your last code, adjust if necessary)

VBA Code:
Sub ummjay()
    Dim lastrow As Long, lastrowadj As Long, lastrowfinal As Long
    Dim CopyRng As Range

    lastrow = Range("AY7:BB" & Rows.Count).Find("*", , xlValues, , xlByRows, xlPrevious).Row
    Set CopyRng = Range("AY7:BB" & lastrow)
    CopyRng.Copy
    Range("AQ7").PasteSpecial xlPasteValues

    lastrowadj = Range("AQ7:AT" & Rows.Count).Find("*", , xlValues, , xlByRows, xlPrevious).Row + 1
    Range("AQ" & lastrowadj).PasteSpecial xlPasteValues

    Application.CutCopyMode = False

    lastrowfinal = Range("AQ7:AT" & Rows.Count).Find("*", , xlValues, , xlByRows, xlPrevious).Row
    Range("AQ" & lastrowadj & ":AQ" & lastrowfinal).Replace "S", "Sam", xlWhole
    Range("AQ" & lastrowadj & ":AQ" & lastrowfinal).Replace "B", "Boy", xlWhole
End Sub
 
Last edited:
Upvote 1
Solution

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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