VBA Select from top to end of column, part 2

opislak

Board Regular
Joined
Feb 28, 2017
Messages
68
Hi all,

I try to select a variable range of cells in a column, in order to set a standard lay-out (background, borders, etc.).
This code does the job allright:
VBA Code:
Range("A3:D999,E3:P999,Q3:Q999,R3:T999,U3:U999,V3:V999,W3:AH999").Select
However, if the data in the column doesn't reach up to row 999, why select that deep? Is there a possibility to select only rows with data in them. Something like this:
VBA Code:
Range("D3", Selection.End(xlDown)).Style = "Percent"
This works fine, too. Can both lines be somehow combined?

Thanks for responses!!
Patrick.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
VBA Code:
Dim iLastCell as long
iLastCell = Cells(rows.count,"D").end(xlup).row
Range("D3:D" & iLastCell).Style = "Percent"
 
Upvote 0
VBA Code:
Dim lr&: lr = [A:AH].Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Intersect([A:AH], Rows("3:" & lr)).Style = "Percent"
 
Upvote 0
Many thanks for your efforts, much appreciated.
But... Sorry, not what I'm looking for...
(I copied the "percent"-thing for information from another part of my script to show as a working example).
However, I would like to start another sub - end sub after the selection.
So: select an interrupted range, but instead of the row 999, I would like to make this last row dynamic, according to the length of the longest column (not all columns are edited).
VBA Code:
sub SelectColumns()
    Range("A3:D999,E3:P999,Q3:Q999,R3:T999,U3:U999,V3:V999,W3:AH999").select
    DoSomething 'sub that does something else, e.g. paint background or put borders of a certain color, etc.
end sub
 
Upvote 0
How about like
VBA Code:
Sub opislak()
   Dim UsdRws As Long
   
   UsdRws = Range("A:AH").Find("*", , , , xlByRows, xlPrevious, , , False).Row
   With Intersect(Rows("3:" & UsdRws), Range("A:AH"))
      .Interior.Color = 45678
      .Borders.Weight = xlThin
   End With
End Sub
 
Upvote 0
How about like
VBA Code:
Sub opislak()
   Dim UsdRws As Long
  
   UsdRws = Range("A:AH").Find("*", , , , xlByRows, xlPrevious, , , False).Row
   With Intersect(Rows("3:" & UsdRws), Range("A:AH"))
      .Interior.Color = 45678
      .Borders.Weight = xlThin
   End With
End Sub
Dear Fluff,
Almost forgot to reply, for which my apologies.
Your code works fine, thank you.
Problem resolved. Many thanks.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,376
Messages
6,119,181
Members
448,871
Latest member
hengshankouniuniu

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