If and constant logical problem

earp_

Active Member
Joined
Apr 30, 2008
Messages
305
Hi i have problem with a long code.
So I divided that code in 4 parts.

So I have
module1 -> Sub Macro1()
module2 -> Sub Macro2()
module3 -> Sub Macro3()
module4 -> Sub Macro4()

I have declared
Option Explicit

Dim mdNextTime1 As Double
Dim myEnter As Integer
'Dim myK1 As Integer
'Dim myK2 As Integer
'Dim myK3 As Integer
Dim myK4 As Integer
Const myC As Integer = 82
Dim Lastrow As Integer
Dim myActualRow As Integer

all my sub have the following structure:
Sub Macro1()

'If (myK1 = 0) Then
' myEnter = myC
' myK1 = 1
'End If

With Sheets("Sheet1").Range("J2")
'7.40
If .Value >= TimeSerial(7, 0, 0) And .Value < TimeSerial(7, 40, 0) Then
With ThisWorkbook
...
End With
If (myEnter = 0) Then
...
myEnter = 1
End If
End If

'8.00
If .Value >= TimeSerial(7, 40, 0) And .Value < TimeSerial(8, 0, 0) Then
With ThisWorkbook
...
End With
If (myEnter = 1) Then
...
myEnter = 2
End If
End If

'8.00
If .Value >= TimeSerial(8, 0, 0) And .Value < TimeSerial(8, 40, 0) Then
With ThisWorkbook
...
End With
If (myEnter = 2) Then
...
myEnter = 3
End If
End If
...
and so on

myEnter is declared as Integer and it's initial value is 0
Dim myEnter As Integer
The question is that if I start the macro at 7.00 everything run ok because myEnter Updates itself
but if I run it at 7.41 I will miss the update and it will not go inside
If (myEnter = 2) Then
...
myEnter = 3
So i thought...well let's use this
If (myK1 = 0) Then
myEnter = myC
myK1 = 1
End If
and if I declare myC as
Const myC As Integer = 1
I should revolve this.
The problem is when for example in macro4 I have
If (myK4 = 0) Then
myEnter = myC
myK4 = 1
'MsgBox myEnter
End If
myEnter doesn't have that value, but it's empty :(

any suggests?
 
It depends on how you decide to start from the third.

If it's the result of a process, maybe you can also have a flag variable like public my_flag as boolean and set its value to false when you don't want the third process to reset to the default 22. Then, you'd start the third process with If my_flag then myK4 = 22.

If it's a manual option, maybe you want to use an input box that, if filled in, will use the new value for myK4 and, if blank, will use whatever value is currently assigned...
 
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
I still don't get why if I put
Public myK1 As Integer
Dim myEnter As Integer
sub macro1
if(myK1 = 0)
myEnter = 22
Msgbox myEnter
end IF

Call macro2

end sub

sub macro2
....
end Sub
works.
but if I put
Public myK1 As Integer
Dim myEnter As Integer
sub macro1
...

Call macro2

end sub

sub macro2
if(myK1 = 0)
myEnter = 22
Msgbox myEnter
end IF
....
end Sub
the Msgbox appears empty.
 
Upvote 0

Forum statistics

Threads
1,214,926
Messages
6,122,306
Members
449,079
Latest member
juggernaut24

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