VBA Code to Find Last Row

Justinian

Well-known Member
Joined
Aug 9, 2009
Messages
1,557
Office Version
  1. 365
Platform
  1. Windows
I have a list of apartment codes (e.g., 87-8703A) in range A2:A8. In cell J2, I used =RIGHT(A2,LEN(A2)-FIND("-",A2,1)) to extract the second part of the apartment code after the dash, giving me 8703A. I then copied the formula down to J8 and did paste special (values) into range A2:A8 and then centered the result horizontally and vertically. Everything works perfectly assuming my data is only in range A2:A8 but today, my data was in range A2:A13, so my macro only goes down to A8. How do I change the following code so that the macro finds the las row of data for both column J and column A?

Sub Extract()
Range("J2").Select
Selection.FormulaR1C1 = "=RIGHT(RC[-9],LEN(RC[-9])-FIND(""-"",RC[-9],1))"
Selection.AutoFill Destination:=Range("J2:J8")
Range("J2:J8").Select
Selection.Copy
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Columns("J:J").Select
Selection.ClearContents
Range("A1").Select
End Sub
 
Last edited:

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Not sure why you are clearing column J at the end after doing all of that, but try this (which I think should do everything except clear column J):
Code:
Sub Extract()

    Dim lr As Long
    
'   Find last row with data in column A
    lr = Cells(Rows.Count, "A").End(xlUp).Row

'   Apply the following to all data rows in column J
    With Range("J2:J" & lr)
'       Formula
        .FormulaR1C1 = "=RIGHT(RC[-9],LEN(RC[-9])-FIND(""-"",RC[-9],1))"
'       Convert to values
        .Value = .Value
'       Formatting
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
    End With
    
End Sub
 
Last edited:
Upvote 0
I am clearing column J because the result is being copied and pasted (values) into column A so J is redundant. When I apply your code, changing J to A, I get a value error.
 
Upvote 0
OK, I think I see what you are doing. Try this:
Code:
Sub Extract()

    Dim lr As Long
    
'   Find last row with data in column A
    lr = Cells(Rows.Count, "A").End(xlUp).Row

'   Apply the following to all data rows in column J
    With Range("J2:J" & lr)
'       Formula
        .FormulaR1C1 = "=RIGHT(RC[-9],LEN(RC[-9])-FIND(""-"",RC[-9],1))"
'       Convert to values
        .Value = .Value
'       Formatting
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
    End With
    
'   Copy to column A and remove
    Range("J2:J" & lr).Copy Range("A2:A" & lr)
    Range("J2:J" & lr).ClearContents
    
End Sub
 
Upvote 0
Sorry if i was not clear.

That is working now - thank you!
 
Upvote 0
The following macro will do what you asked for directly without involving Column J or any copy/pasting...
Code:
Sub Extract()
  With Range("A2", Cells(Rows.Count, "A").End(xlUp))
    .Value = Evaluate(Replace("IF({1},REPLACE(@,1,FIND(""-"",@),""""))", "@", .Address))
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,338
Messages
6,124,354
Members
449,155
Latest member
ravioli44

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