VBA Select from top to end of column, part 2

opislak

New Member
Joined
Feb 28, 2017
Messages
45
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

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

Lian

New Member
Joined
Jan 26, 2021
Messages
34
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
VBA Code:
Dim iLastCell as long
iLastCell = Cells(rows.count,"D").end(xlup).row
Range("D3:D" & iLastCell).Style = "Percent"
 

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
3,229
Office Version
  1. 2016
Platform
  1. Windows
VBA Code:
Dim lr&: lr = [A:AH].Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Intersect([A:AH], Rows("3:" & lr)).Style = "Percent"
 

opislak

New Member
Joined
Feb 28, 2017
Messages
45
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,729
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

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
 

opislak

New Member
Joined
Feb 28, 2017
Messages
45
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,729
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,932
Messages
5,639,067
Members
417,067
Latest member
rohitbabshet

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
Top