Runs on one but not another

tourless

Board Regular
Joined
Feb 8, 2007
Messages
144
Office Version
  1. 365
Platform
  1. Windows
Hi Folks,

I've got this code...
Code:
Option Explicit

Sub TransferToMain()
    Dim sourceWB As Workbook, destWB As Workbook
    Dim fName As String, strName As String
    Dim weDate As Date
    Dim iWeekDay As Integer
    Dim LastRow As Long
    
    'Set Source & Copy Sheet To Production File
    Set sourceWB = ThisWorkbook
    'Set date
    weDate = Date
    fName = "myfile"
    Set destWB = Workbooks.Open("myotherfile")
    
    'Call ReportOpen
    
    'Copy the template
    sourceWB.Sheets("Template").Copy After:=destWB.Sheets(destWB.Sheets.Count)
    
    iWeekDay = Weekday(Now(), vbSunday)
    weDate = Format(Now - (iWeekDay - 1), "mm-dd-yyyy")
    
    For Each ws In Worksheets
        If ws.Name = weDate Then
            Application.DisplayAlerts = False
            ws.Delete
            Application.DisplayAlerts = True
        Else
        End If
    Next
 
    ActiveSheet.Name = Format(weDate, "mm-dd-yyyy")   
End Sub
The first thing you might notice is that ws is not actually declared. Curiously it runs on my machine. If I try to run it on another machine, it tells me it's not declared so I declare it as 'Dim ws as Worksheet'. No problem. Then I get a type mismatch on 'If ws.Name = weDate Then', which I understand.

My question is why would the code run successfully on one machine and not another?
 
Last edited by a moderator:

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
It shouldn't run on your machine either since you have Option Explicit at the top of the code module. I can't see any good reason for a type mismatch there since a date should be coercible to a string without an explicit CStr call.
 
Upvote 0
Odd.

Anyway I changed the weDate declaration to a string, declared ws, and changed my For Each from In Worksheet to In destWB.Worksheets and it's working.
 
Upvote 0

Forum statistics

Threads
1,223,702
Messages
6,173,961
Members
452,539
Latest member
delvey

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