Finding the end of a worksheet

Dan Wilson

Well-known Member
Joined
Feb 5, 2006
Messages
507
Office Version
  1. 365
Platform
  1. Windows
Good day. I am running Excel out of Office365 (updated) on Windows 10 Home. I have a workbook that creates a worksheet to list almost 5,000 songs in my Music folder. I have added some subroutines to the macro to format the new worksheet after the data has been installed. I have accomplished all my goals except one. As I am constantly adding songs to my Music folder. I need a way to determine the last used Row in the worksheet. In the macro is a section copied below that puts Borders around the existing data. Currently, I have to modify the range in the macro every time I create a new database with added songs. Is there a way to modify the Range in the sub below to automatically find the last row with data? Thanks for any help on this.
Dan Wilson...

Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("A2:H4560").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Will there be any column that always has data in every cell?
If so which?
 
Upvote 0
Will there be any column that always has data in every cell?
If so which?
Good day Fluff. I meant to include that, but did not. Column A will have data from A1 to the last row of data.
Thanks, Dan Wilson...
 
Upvote 0
How about
VBA Code:
With Range("A2:H" & .Range("A" & Rows.Count).End(xlUp).Row)
   .Borders(xlDiagonalDown).LineStyle = xlNone
   .Borders(xlDiagonalUp).LineStyle = xlNone
   With .Borders(xlEdgeLeft)
      .LineStyle = xlContinuous
      .ColorIndex = xlAutomatic
      .TintAndShade = 0
      .Weight = xlThin
   End With
End With
 
Upvote 0
You could also use conditional formatting to add the borders instead of using code. You can have a rule that says

Excel Formula:
=A1<>""
Applies to: A:H

and the formatting is the border you want.
 
Upvote 0
How about
VBA Code:
With Range("A2:H" & .Range("A" & Rows.Count).End(xlUp).Row)
   .Borders(xlDiagonalDown).LineStyle = xlNone
   .Borders(xlDiagonalUp).LineStyle = xlNone
   With .Borders(xlEdgeLeft)
      .LineStyle = xlContinuous
      .ColorIndex = xlAutomatic
      .TintAndShade = 0
      .Weight = xlThin
   End With
End With
Good day Fluff. I entered the code and when I run the macro, the Debugger pops up with the word ".Range" highlighted and an error statement of "Compile Error - Invalid or unqualified reference". I checked that I entered everything correctly. I have copied what I entered below.

With Range("A2:H" & .Range("A" & Rows.Count).End(x1Up).Row)
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
With .Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
End With

Thanks, Dan Wilson...
 
Upvote 0
Along with what Rick has said you also have x1Up which should be xlUp (lowercase L not number one)
 
Upvote 0
You could also use conditional formatting to add the borders instead of using code. You can have a rule that says

Excel Formula:
=A1<>""
Applies to: A:H

and the formatting is the border you want.
Good day 6StringJazzer. Thank you for the suggestion. That would make the border the same for all 5,000 rows. I will experiment with it later.
Thanks, Dan Wilson...
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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