Formula within an IF statement - VBA

lloydie8

New Member
Joined
Sep 5, 2017
Messages
23
Morning guys

A simple one for you experts, but not so simple for myself as a newbie to VBA.

I am trying to write =IF(H2="0",H2,"0"&H2) as part of a VBA string. However, whatever I try is returning #NAME !

Any ideas on how I should write this in VBA please folks?

Many thanks,
Gareth
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
So if H2 = 0 you return 0 otherwise you return H2 with a leading zero?
Try

Code:
Sub k1()
Dim var1 As String
If Range("H2") = "0" Then var1 = "0"
Else: var1 = "0" & Range("H2")
MsgBox var1
End Sub

You may need to replace "0" with 0 depending on the data (a common problem amongst newbies is to mix up 0 with "0")
 
Last edited:
Upvote 0
Yep, that's right.

Seems to work for me, thank you.

So if H2 = 0 you return 0 otherwise you return H2 with a leading zero?
Try

Code:
Sub k1()
Dim var1 As String
If Range("H2") = "0" Then var1 = "0"
Else: var1 = "0" & Range("H2")
MsgBox var1
End Sub

You may need to replace "0" with 0 depending on the data (a common problem amongst newbies is to mix up 0 with "0")
 
Upvote 0

Forum statistics

Threads
1,216,073
Messages
6,128,645
Members
449,461
Latest member
kokoanutt

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