Changing Worksheet Name to apply formulas based on cell reference entry

jaspalsd

Board Regular
Joined
Feb 3, 2014
Messages
74
Hi,

I haven't touched VBA for a long time and hopefully someone can provide the simplest solution.

I have a dropdown list of worksheet names and whicever option is chosen, I wish to then have formulas applied to that workseet.

Can someone please advise on how to approach this. Thank you

VBA Code:
Sub Commission_Percent()



Dim Lastrow As Long

Lastrow = Range("A" & Rows.Count).End(xlUp).Row


Dim strFormulas(1 To 4) As Variant




With Sheets("JBP") 'I want to dynnamically change this to what is entered in the Sheets("Commission").Range("C4").Select as there is a dropdown menu to select diff names
      
      
      strFormulas(1) = "=SUM(C2:E2)"
      strFormulas(2) = "=XLOOKUP(A2,Lists!C:C,Lists!B:B,0,0)"
      strFormulas(3) = "=J2*I2"
      
     
      
       .Range("I2:K" & Lastrow).Formula = strFormulas
    
      
   End With
   
   
   
With Worksheets("JBP")
    For i = 2 To Lastrow
        If .Range("J" & i).Value < .Range("B" & i).Value Then
            .Range("J" & i).Font.Color = vbRed
        Else
            .Range("J" & i).Font.Color = vbBlack
        End If
    Next i
End With



End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
You should be able to just substitute:
VBA Code:
Sheets("Commission").Range("C4").Value
for all your references of
VBA Code:
"JBP"

If you want to capture in it in a variable for easier referencing, you could so soemthing like:
VBA Code:
Dim ws as String
ws = Sheets("Commission").Range("C4").Value

The you can replace all references of:
VBA Code:
Sheets("JBP")
with
VBA Code:
Sheets(ws)
 
Upvote 0
Solution
You should be able to just substitute:
VBA Code:
Sheets("Commission").Range("C4").Value
for all your references of
VBA Code:
"JBP"

If you want to capture in it in a variable for easier referencing, you could so soemthing like:
VBA Code:
Dim ws as String
ws = Sheets("Commission").Range("C4").Value

The you can replace all references of:
VBA Code:
Sheets("JBP")
with
VBA Code:
Sheets(ws)
thank you, you are a superstar, as I may have multiple worksheets, the last bit of code does the job nicely
 
Upvote 0
One other option, is to use/set a Worksheet object, which makes referencing it even easier, which may be preferrable if you are referencing this worksheet a lot.

You would first create the Worksheet object like this:
VBA Code:
    Dim ws As Worksheet
    Set ws = Sheets(Sheets("Commission").Range("C4").Value)

Then, you could replace all references of this:
VBA Code:
With Sheets(ws)
with this:
VBA Code:
With ws
 
Upvote 0

Forum statistics

Threads
1,224,802
Messages
6,181,054
Members
453,014
Latest member
Chris258

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