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

NDMDRB

Board Regular
Joined
Jun 20, 2016
Messages
164
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
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
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.
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,916
Members
449,093
Latest member
dbomb1414

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