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
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: