Setting a Sheet based on a cell value in a different sheet

DavyJones90

Board Regular
Joined
Feb 23, 2022
Messages
62
Office Version
  1. 365
Platform
  1. Windows
Hello Everyone, thanks in advance for your help

Here is my code

VBA Code:
Sub AutoArrowsLeft()
   
  
    Dim Cl As Range
    Dim Dic As Object
    Dim sys As Worksheet
    'Dim arrow As Worksheet
    Dim Sarrows As Worksheet
       
   

    Set sys = ThisWorkbook.Sheets("$Auto")
   ' Set arrow = Workbooks("CURRENCIES  W-D System.xlsm").Worksheets("Final ARROWS")
   
       
    If sys.Range("H3:Y3").Text = "Monthly" Then
    Set Sarrows = ThisWorkbook.Sheets("$MW")
    ElseIf sys.Range("H3:Y3").Text = "Weekly" Then
    Set Sarrows = ThisWorkbook.Sheets("$WD")
    ElseIf sys.Range("H3:Y3").Text = "Daily" Then
    Set Sarrows = ThisWorkbook.Sheets("$DH")
    Else
    End If
      
       
   
    Set Dic = CreateObject("scripting.dictionary")
   
   
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.AskToUpdateLinks = False
    Application.DisplayAlerts = False

'Dow Patern
   
    With sys
        For Each Cl In .Range("A5", .Range("A" & Rows.Count).End(xlUp)) 'I am getting an error here stating Object Variable of with variable not set
            Dic(Cl.Value) = Cl.Offset(, 7).Value
        Next Cl
    End With
    With Sarrows
       
        For Each Cl In .Range("F6", .Range("F" & Rows.Count).End(xlUp))

'I am getting an error stating Object Variable of with variable not set, any idea how to restructure so it works?

It must have somehting to do with this part, because it works if I only set one worksheet instead of 3 through an if
VBA Code:
If sys.Range("H3:Y3").Text = "Monthly" Then
    Set Sarrows = ThisWorkbook.Sheets("$MW")
    ElseIf sys.Range("H3:Y3").Text = "Weekly" Then
    Set Sarrows = ThisWorkbook.Sheets("$WD")
    ElseIf sys.Range("H3:Y3").Text = "Daily" Then
    Set Sarrows = ThisWorkbook.Sheets("$DH")
    Else
    End If
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
VBA Code:
If sys.Range("H3:Y3").Text = "Monthly" Then

a range can not be compared like that.

should be:

VBA Code:
If Worksheetfunction.Countif(sys.Range("H3:Y3"), "Monthly") then

Similar to Weekly and Daily.
 
Upvote 0
Solution
Thanks man, works like a charm, can you tell me why that worked and mine didn't?
 
Upvote 0
To test if a value is in a range, not:
VBA Code:
If sys.Range("H3:Y3").Text = "Monthly" Then

but two ways:

1) Loop
VBA Code:
For each cell in sys.Range("H3:Y3")
    If cell.value = "Monthly" then <do some thing>
Next

2) to test if value exist or not:
VBA Code:
If Worksheetfunction.Countif(sys.Range("H3:Y3"), "Monthly") then
 
Upvote 0

Forum statistics

Threads
1,215,223
Messages
6,123,714
Members
449,118
Latest member
MichealRed

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