Select last row with data in

berty2000

Board Regular
Joined
Mar 29, 2011
Messages
71
Need to search down rows in range B5:M6000 to find the last row with data in then select the next row down from column B to M and colour yellow.

Seems simple but cant seem to get result

Code:
Sub test()


'   Select the range: Last row - Last Column
    Range("B5:M6000").Select
Cells(Application.Rows.Count, 1).End(xlUp).Offset(1, 0).Select
    
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try

Code:
Sub test()
Range("B" & Rows.Count).End(xlUp).Offset(1).Resize(, 12).Interior.ColorIndex = 6
End Sub
 
Upvote 0
Another method.

Code:
[color=darkblue]Sub[/color] test()


[color=darkblue]Dim[/color] Lastrow [color=darkblue]As[/color] [color=darkblue]Long[/color]


Lastrow = Range("B5:M6000").Find(What:="*", After:=Range("B5"), LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row


Range("B" & Lastrow + 1).Resize(, 12).Interior.Color = vbYellow


[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0
That is nearly perfect for what I need except sometimes there is no data in the B column but there is in say the H column so I need it to search across the range for the last row thats my stumbling block VoG
 
Upvote 0
Similar to AlphaFrog's

Code:
Sub test()
Dim LR As Long
LR = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
Range("B" & LR + 1).Resize(, 12).Interior.ColorIndex = 6
End Sub
 
Upvote 0
Another method.

Code:
[COLOR=darkblue]Sub[/COLOR] test()


[COLOR=darkblue]Dim[/COLOR] Lastrow [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]


Lastrow = Range("B5:M6000").Find(What:="*", After:=Range("B5"), LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row


Range("B" & Lastrow + 1).Resize(, 12).Interior.Color = vbYellow


[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

That works perfectly, thank you very much wish I ask early spent last 2 hour trying to figure this out
 
Upvote 0
Similar to AlphaFrog's

Code:
Sub test()
Dim LR As Long
LR = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
Range("B" & LR + 1).Resize(, 12).Interior.ColorIndex = 6
End Sub

This too is perfect thank you both guys
 
Upvote 0
I have added

Code:
 LR = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).row
Range("B" & LR + 1).Resize(, 12).Interior.ColorIndex = 6      
Range("B" & LR + 1).Resize(, 12).Cells.MergeCells = True

So it merges the yellow cells into one cell but how do i make that the active cell or selection so i can resize it pull text into it etc I have tried

Code:
'Format cell for Title Placement 
   Range("B" & ActiveCell.row).Select
    ActiveCell.HorizontalAlignment = xlCenter
    ActiveCell.Value = Department
 
Upvote 0
Try

Code:
Sub test()
Dim LR As Long
LR = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
With Range("B" & LR + 1).Resize(, 12)
    .Interior.ColorIndex = 6
    .HorizontalAlignment = xlCenterAcrossSelection
    .Resize(, 1).Value = "Department"
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,924
Members
452,366
Latest member
TePunaBloke

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