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

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
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,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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