Macro Error 91, SubAdress usage and selecting first row where column A is blank

skipick

New Member
Joined
Jan 17, 2017
Messages
1
I am putting together a macro that
1) Creates a new tab as a copy of a tab named 'Template'
2) Queries the user to enter a name for the new tab
3) Inserts a new line to a table within a worksheet in a tab named 'Summary'
4) Enters the new tab name into the cell in the first column of that new line
5) Then makes that name a hyperlink to the new tab by that name.

Once the new tab is created named, the hyperlink I need shows up as the name entered with !Print_Area appended (e.g., tabname!Print_Area).

I tried the do that in the code below, but get Run-time error'91' Object variable or With Block variable not set.

I've tried a bunch of different ways to write it, but just can't figure it out. The key is in the last line - the SubAddress part. I think I need to set the wks of wks.Name to the correct value, but am not sure what to do.


Ultimately, I also need to figure out how to select the first row that has nothing in it, and then do my insert. As it is now, once I run the macro once, line 21 will now be line 22 and the macro will insert the new line in the wrong place. I have sorting macros that have a range, but that range doesn't extend when I insert a new row, so I need to change those macros to 'look' for the first blank row.

Sorry if this is too much, but any help would be much appreciated. I am just beginning to use/learn vb/macros and don't know what I am doing!
Sub InsertOpportunity()
'
' InsertOpportunity Macro
'
'
Dim sName As String
Dim wks As Worksheet
Worksheets("Template").Copy after:=Sheets("Active -->")
Set wks = ActiveSheet
Do While sName <> wks.Name
sName = Application.InputBox _
(Prompt:="Enter new worksheet name")
On Error Resume Next
wks.Name = sName
On Error GoTo 0
Loop
Set wks = Nothing
Sheets("Summary").Select
Rows("21:21").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A21").Select
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:="'" & wks.Name & "'!Print_Area", TextToDisplay:=sName
End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Forum statistics

Threads
1,223,236
Messages
6,170,917
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