Worksheet Variable

Xenocity

New Member
Joined
Jul 6, 2011
Messages
7
HI,

I have tried looking at all the examples of how to create a formula in VBA that has the worksheet defined as a variable that I can easily change for each report I need.

I don't know how to define this at the top of my module.

Range("B486").Select
ActiveCell.FormulaR1C1 = _"=IF('2012master'!R[-483]C[82]="""","""",VLOOKUP(""Y"",'2012master'!R[-483]C[82]:R[-483]C108,24,FALSE))"

I am looking for something at the top similar to:

set ws = '2012master'

so that all the formulas on the vba will use the workshee that I define.

Secondly, I wanted to make the process easier for the user so that they could choose the worksheet from a user form to populate the specific worksheet being used for the variable if this is possible.

Thank you in advance.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Code:
Dim strSheetname As String
 
strSheetname="'[COLOR=red]2012master[/COLOR]'"
 
ActiveCell.FormulaR1C1 = _"=IF('" & [COLOR=red]strSheetname[/COLOR] & "'!R[-483]C[82]="""","""",VLOOKUP(""Y"",'" & [COLOR=red]strSheetname[/COLOR] & "'!R[-483]C[82]:R[-483]C108,24,FALSE))"
 
Upvote 0
And to display a list of worksheets for the user to select one:-
Code:
Application.CommandBars("Workbook Tabs").ShowPopup
 
Upvote 0
Soz - I accidentally copied the apostrophes. Try:-
Code:
strSheetname="[COLOR=red]2012master[/COLOR]"
 
Upvote 0
The first code worked great but I dont think I explained myself well enough for the second.

I would like the end user of my document to be able to select what worksheet becomes the variable without having to go into VBE and changing the strSheetname variable. (The VBA isn't run on the active worksheet, it is pulling information from the variable worksheet)

So esentially I'm wondering if I can create a user form or something to that nature that will automaticall fill the strSheetname variable.
 
Upvote 0
Did you try this?
Code:
Application.CommandBars("Workbook Tabs").ShowPopup
(You can test it from the Immediate window.)

If you place that in a macro, you can call it from a button on your worksheet and that will change the active sheet.

Alternatively you can design a userform with just a single combobox on it, load the combobox with the names of all the worksheets in the workbook, then when the user clicks one of the names, store it in your variable and close the userform. (Or a listbox - whichever seems best.)

Assuming you use the names UserForm1 and ComboBox1, this should do the trick:-
Code:
Private Sub ComboBox1_Click()
 
  strSheetname = UserForm1.ComboBox1.Value

  Unload Me
 
End Sub
 
Private Sub UserForm_Activate()
 
  Dim ws As Worksheet
  
  UserForm1.ComboBox1.Clear
  For Each ws In ThisWorkbook.Worksheets
    UserForm1.ComboBox1.AddItem ws.Name
  Next ws
  
End Sub
That goes in the code module for the userform. Then all you need to do is UserForm1.Show, maybe from a command button on your worksheet.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,726
Members
452,939
Latest member
WCrawford

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