to many 'with this workbook'?

earp_

Active Member
Joined
Apr 30, 2008
Messages
305
hi have this structure

With myTime
If .Value >= TimeSerial(cHour, cMinute, 0) And .Value < TimeSerial(nHour, nMinute, 0) Then
...
Worksheets("Sheet2").range(colSheet2).End(xlUp).Offset(1, 0).PasteSpecial xlValues

'####################
'Count previous column
If (IsEqual = False) Then

If WorksheetFunction.CountA(Sheets("Sheet2").Columns(pIndex)) > 0 Then

Lastrow = Sheets("Sheet2").range(pIndex & Sheets("Sheet2").Rows.Count).End(xlUp).Row


With Worksheets("Sheet2")
'close
'Worksheets("sheet3").range("E65536").End(xlUp).Offset(1, 0) = Worksheets("Sheet2").range(colPrevSheet2).End(xlUp)
...
where
With ThisWorkbook
Set mytime = Worksheets("Sheet1").range("J2")
End With
Could this structure give me any error if I'm instead on the active sheet5?
I mean instead of using
With myTime
If .Value >= TimeSerial(cHour, cMinute, 0) And .Value < TimeSerial(nHour, nMinute, 0) Then
is it better to write something like
If myTime.Value >= TimeSerial(cHour, cMinute, 0) And myTime.Value < TimeSerial(nHour, nMinute, 0) Then
so maybe i have less errors.
Somehow my code crash...and maybe is for this kind of errors
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
If you want to refer to Sheet1 in ThisWorkbook, be sure to include the dot qualifier in your With clause:

Code:
With ThisWorkbook
    Set mytime = .Worksheets("Sheet1").range("J2")
End With
 
Upvote 0
in this case it gives me an error because my structure is
Sub closeFun
Application.OnTime mdNextTime1, "myFirstSub", , False
Ed Sub

Sub myFirstSub
..
With thisWorkbook
Set myTime = Worksheets("Sheet1").range("B2")
End With
...
mdNextTime1 = Now + TimeValue("00:00:01")
End Sub
if I put the '.' it gives me an error on
Application.OnTime mdNextTime1...
 
Upvote 0
You shouldn't really use With End with for one line of code.
Code:
Set myTime = ThisWorkbook.Worksheets("Sheet1").Range("B2")
If that still errors we need to see the rest of the code.
 
Upvote 0
it seems to work without the With.
The Last question is that if i'm seeing the chart sheet (called chartSheet)
it gives me an error on this line
If WorksheetFunction.CountA(Sheets("Sheet2").Columns(pIndex)) > 0 Then
it works instead if i'm seeing other sheets :(
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,040
Members
449,063
Latest member
ak94

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