Macro bugging when using declared variable "Cl.value" to find worksheet name

Status
Not open for further replies.

diygail123

New Member
Joined
Oct 24, 2018
Messages
25
Hi

Can anyone tell me how to stop the code below bugging at the Sheets(Cl.Value).Activate
The filename when I hover over CL.Value is 5837, and this does exist in the worksheet, I have tested by entering "5837" rather than Cl.value and the code runs fine!

Any help much appreciated.


Code:
 Sub CopyFltr()

'DECLARE VARIABLES
   Dim Ws As Worksheet
   Dim Cl As Range
   
   Application.ScreenUpdating = False
   Set Ws = Sheets("Import")
   
'FILTER ON EACH VENDOR ON IMPORT TAB
   If Ws.AutoFilterMode Then Ws.AutoFilterMode = False
   With CreateObject("Scripting.dictionary")
     For Each Cl In Ws.Range("b5:b58")                                 'For Each Cl In Ws.Range("B5", Ws.Range("B" & Rows.Count).End(xlUp))
         If Not .Exists(Cl.Value) Then
            .Add Cl.Value, Nothing
            Ws.Range("A4:O4").AutoFilter 2, Cl.Value
            Ws.AutoFilter.Range.Copy
            Sheets(Cl.Value).Activate                                                            'Sheets(Cl.Value).Activate
            Range("A6").PasteSpecial
                                                        'Ws.AutoFilter.Range.Copy Sheets(Cl.Value).Range("A4")
         End If
      Next Cl
   End With
   Ws.AutoFilterMode = False
   ActiveWindow.View = xlNormalView
   
   
   MsgBox ("Vendor tabs have now been updated")
   Sheets("Macros").Activate
End Sub
 

Some videos you may like

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,096
Office Version
  1. 2019
Platform
  1. Windows
Hi,
try coercing your numeric value to a string

Rich (BB code):
Sheets(CStr(Cl.Value)).Activate

Dave
 

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
8,110
Office Version
  1. 365
Platform
  1. Windows
Heres how you could do it:

Code:
Dim x As String

x = cl.Value
Sheets(x).Activate
 
Status
Not open for further replies.

Watch MrExcel Video

Forum statistics

Threads
1,109,127
Messages
5,526,996
Members
409,733
Latest member
revender17

This Week's Hot Topics

Top