Using IIf function instead of "If then" in Excel VBA

NDMDRB

Board Regular
Joined
Jun 20, 2016
Messages
158
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have a peace of code below using "If Then" and wondering how can I make it smaller using IIf function
VBA Code:
If Not Me.Plate_TXT = "" Then
        ws.Cells(nr, "D") = CDbl(Me.Plate_TXT)
End If

In TextBox "Plate_TXT" I'm using number, so I need to use CDbl() format
The above code is running correctly, but I tried the one below, but I got an error, also tried CDate for another textbox and got an error
VBA Code:
ws.Cells(nr, "D") = IIf(Me.Plate_TXT = "", "", CDbl(Me.Plate_TXT))

Can you please help me fixing it?

Many thanks in advanced
 

Some videos you may like

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
55,974
Office Version
  1. 365
Platform
  1. Windows
Note that you can make it a one line statement using IF, like this:
VBA Code:
If Not Me.Plate_TXT = "" Then ws.Cells(nr, "D") = CDbl(Me.Plate_TXT)
You can do that anytime you only have one line of code between the IF and END IF statements.
 

NDMDRB

Board Regular
Joined
Jun 20, 2016
Messages
158
Office Version
  1. 2016
Platform
  1. Windows
Note that you can make it a one line statement using IF, like this:
VBA Code:
If Not Me.Plate_TXT = "" Then ws.Cells(nr, "D") = CDbl(Me.Plate_TXT)
You can do that anytime you only have one line of code between the IF and END IF statements.
Thank you Joe4
Yes you are right, but some situations I need to use If and Else,
Ex:
ws.Cells(nr, "E") = Iif(Me.Type_CMB = "", "Follow Up", Me.Type_CMB)
The above works correctly since there is no date or number format used, but if I need to use it as CDate(Me.Type_CMB) or CDbl(Me.Type_CMB) I got an error

I need a shortcut because I have a lot of textboxes and need to make it simple
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,166
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Shorter code does not necessarily mean better code. The problem with IIf is that it always evaluates both the True and False expressions. So if your textbox is empty, it still tries to evaluate CDbl("") and you get an error. Stick with If...Then.
 

Watch MrExcel Video

Forum statistics

Threads
1,123,489
Messages
5,601,986
Members
414,489
Latest member
Xlambda

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
Top