if else function won't work

ChanL

Board Regular
Joined
Apr 8, 2021
Messages
65
Office Version
  1. 2019
Platform
  1. Windows
Hi, currently writing a VBA where if the value of range I10 in sheet name Main is equal to 1H, then the value of range ("B20") in sheet name Part 1 would be January

VBA Code:
sub working()

Dim reportprd as string

reportprd = thisworkbook.sheets("Main").Range("I10").value

if reportprd="1H" then
thisworkbook.sheets("Part 1").range("B20").value = "January"
else 
thisworkbook.sheets("Part 1").range("B20").value="February"
End if

end sub

i use some basic function to return the value of reportprd , it shows "1H". Bt i don't know why the value of the of B20 just won't change to January. in fact it showing february, which is not correct
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try running the below and see what the message box states.

VBA Code:
Sub working()
    Dim reportprd As String
    
    reportprd = ThisWorkbook.Sheets("Main").Range("I10").Value
    
    MsgBox Len(reportprd)
    
    If reportprd = "1H" Then
    ThisWorkbook.Sheets("Part 1").Range("B20").Value = "January"
    Else
    ThisWorkbook.Sheets("Part 1").Range("B20").Value = "February"
    End If

End Sub
 
Upvote 0
i think i know why, i think have an extra space after my 1H that why my if else function won't work, thanks for the reminder!
 
Upvote 0
i removed the extra space for 1H but somehow, the value still return as february? can advice?
 
Upvote 0
Maybe if we tidy up the string in the code:
VBA Code:
Sub working()
    Dim reportprd As String
    
    reportprd = UCase(Trim(Sheets("Main").Range("I10").Value))
    
    If reportprd = "1H" Then
        Sheets("Part 1").Range("B20").Value = "January"
    Else
        Sheets("Part 1").Range("B20").Value = "February"
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,226,730
Messages
6,192,699
Members
453,747
Latest member
tylerhyatt04

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