If then else not working

BobSmith2112

New Member
Joined
Mar 8, 2018
Messages
6
I am trying to add what I thought would be an easy addition to my code.
I have two macros that calculate using different formulas.
I want to automatically call the right macro if the cell 2 rows above the active cell is "W"
Although the value is "W" the macro jumps straight to the else statement and calls the wrong macro.
Code:
Sub Wednesday()
Range("graynext").Select
If ActiveCell.Offset(-2, 0) = "W" Then
Call PasteandcalcSat
Else
Call PasteAndDown
End If
End Sub
[\code]
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Changed ActiveCell.Offset(-2, 0) to ActiveCell.Offset(-2, 0).Value

Code:
[COLOR=#333333]Sub Wednesday()[/COLOR]
[COLOR=#333333]Range("graynext").Select[/COLOR]
[COLOR=#333333]If ActiveCell.Offset(-2, 0).Value = "W" Then[/COLOR]
[COLOR=#333333]Call PasteandcalcSat[/COLOR]
[COLOR=#333333]Else[/COLOR]
[COLOR=#333333]Call PasteAndDown[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]End Sub[/COLOR]
 
Upvote 0
Does it work if you don't use ActiveCell/Select?
Code:
Sub Wednesday()

    If Range("graynext").Offset(-2, 0) = "W" Then
        Call PasteandcalcSat
    Else
        Call PasteAndDown
    End If

End Sub
 
Upvote 0
No, still jumps to else.
I have similar code in Pasteanddown and PasteandcalcSat that detects if the cell two above is "S" and moves two columns to the right and that works fine.
 
Upvote 0
Is your value "W" or "w"? There is a difference as the code as written is case sensitive.

Try forcing the check to always be in upper case as follows:
Code:
If UCase(Range("graynext").Offset(-2, 0)) = "W" Then
 
Upvote 0
Oddly if I set it to detect "S" and move the active cell to 2 down from"S" it works! But I need it to work with"W"
 
Upvote 0
Cured it, not sure exactly how and why but I changed the W to an S and it worked then I changed it back to W and it started working properly. Copying the cell to the other instances of W solves the problem. So the code was fine it was something to do with the cell format.
 
Upvote 0

Forum statistics

Threads
1,214,525
Messages
6,120,051
Members
448,940
Latest member
mdusw

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