Dynamic Worksheet Name

bostonrudi1993

New Member
Joined
Dec 15, 2017
Messages
5
Hello,

I am trying to create a dynamic worksheet name in my dynamic range borders macro.

I got the range borders to be dynamic but now I am struggling with creating a dynamic worksheet name that I do not have to change for each report I run.

This is the code I have:

Sub DynamicRangeBorders()
'Best used when first column has value on last row and first row has a value in the last column

Dim sht As Worksheet
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range


Set sht = Worksheet("A1")
Set StartCell = Range("A2")


'Find Last Row and Column
LastRow = sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).Row
LastColumn = sht.Cells(StartCell.Row, sht.Columns.Count).End(xlToLeft).Column


'Select Range
sht.Range(StartCell, sht.Cells(LastRow, LastColumn)).Select

'Apply the borders
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With


End Sub

The red text above is the line I cannot figure out. I am new to VBA and trying to teach myself. Any advice is more than welcome!

Thanks in advance!
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Welcome to the forum and goodluck on your VBA learning! It can be a blast.

I'm guessing you need it to be:

Set sht = Worksheets(Range("A1").value)

Your code is setting sht to the worksheet with the name of the string "A1", not setting it to the name of the string that is in cell A1. If that makes sense.
 
Last edited:
Upvote 0
A few things:
- You need to make sure that you include the sheet reference along with cell A1 to make sure you are looking at the correct sheet.
- You need to make sure that a sheet exists by that name.

This should work (assuming you are looking at cell A1 on Sheet1):
Code:
Dim shtName As String
Dim sht As Worksheet


shtName = Sheets("Sheet1").Range("A1")
Set sht = Sheets(shtName)
 
Upvote 0
Works for me if the value of A1 is first put in a variable.

Code:
Dim sht As Worksheet
Dim wsname As String

wsname = Sheets("Sheet1").Range("A1")
Set sht = Worksheets(wsname)
 
Upvote 0
Now I have:

Sub DynamicRangeBorders()

Dim sht As Worksheet
Dim wsname As String
Dim LastRow As Long
Dim LasColumn As Long
Dim StartCell As Range

wsname = Sheets("Sheet1").Range("A1")
Set sht = Worksheets(wsname)
Set StartCell = Range("A2")

Getting an error on the red font. If I set A1 to a variable should it be a string?
 
Upvote 0
Exactly what is the error messaging you are getting?
Do you have a sheet named "Sheet1"?
What exactly is in cell A1 on Sheet1?
 
Upvote 0
Would it not need to be:

wsname = Sheets("Sheet1").Range("A1").value
 
Last edited:
Upvote 0
Doesn't it need to be:

wsname = Sheets("Sheet1").Range("A1").value
No, the ".value" is implied, if left out.
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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