if function in VBA

marreco

Well-known Member
Joined
Jan 1, 2011
Messages
609
Office Version
  1. 2010
Platform
  1. Windows
Good day!

Excel 2007.

how do i use the IF Function in VBA?

Example: If (A2<> "";A2+B2;"")

Thank you!!
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
1) Simple IF with a single statement
Code:
   If condition Then x = 1

2) Simple IF with multiple statements
Code:
   If condition Then x = 1: a = ""

3) Block IF..THEN
Code:
   If condition Then
     x = 1
     a = "xxx"
   End If

4) Block IF..THEN..ELSE
Code:
   If condition Then
     x = 1
     a = "xxx"
   Else
     x = 2
     a = "yyy"
   End If

5) Block IF..THEN..ELSEIF
Code:
   If condition1 Then
     x = 1
     a = "xxx"
   ElseIf condition2 Then
     x = 2
     a = "yyy"
   End If

6) Block IF..THEN..ELSEIF..ELSE
Code:
   If condition1 Then
     x = 1
     a = "xxx"
   ElseIf condition2 Then
     x = 2
     a = "yyy"
   ElseIf condition3 Then
     x = 3
     a = "zzz"
   Else
     x = 4
     a = "xyz"
   End If

How to code SELECT statements

1) Single test variable
Code:
   Select Case variable
     Case 1, 3, 6 To 9
       x = 1
       a = "xxx"
     Case Is <= 100
       x = 2
       a = "yyy"
     Case Is <= 1000
       x = 3
       a = "zzz"
     Case Else
       x = 4
       a = "xyz"
   End Select

2) Multiple test variables
Code:
   Select Case True
     Case x < 100
       a = "xxx"
     Case a = "yyy", a = "zzz"
       x = 2
     Case Else
       x = 4
       a = "xyz"
   End Select
 
Upvote 0
Are you looking for the IIf function

Code:
myResult = IIf(myInput<>vbNullString, myInput + 2, vbNullString)
 
Upvote 0
Good afternoon!!

I have a worksheet where the cells of column "A", have values.
in column "B", some cells are empty.
what i want is...the result of cells in column "C"..If (A2<> "";A2+B2;"").
and then drag down.

Column A---------Column B---------ColumnC

456-------------- ---------------""
321--------------753---------------1074
654--------------236---------------890
696-------------- ---------------""
124--------------124---------------248


Thank you!!
 
Upvote 0
Should it not be B2 and NOT A2 like:
=IF(B2<>"",A2+B2,"")
 
Upvote 0
Good afternoon!!

I have a worksheet where the cells of column "A", have values.
in column "B", some cells are empty.
what i want is...the result of cells in column "C"..If (B2<> "";A2+B2;"").
and then drag down.

Column A---------Column B---------ColumnC

456-------------- ---------------""
321--------------753---------------1074
654--------------236---------------890
696-------------- ---------------""
124--------------124---------------248


Thank you!!
 
Upvote 0
Code:
With Sheet1.Range("A:A")
    Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp)).Offset(0, 2).FormulaR1C1 = "=IF(RC1 <> """", RC1+RC2, """")"
End With
 
Upvote 0
hi, thank you...:laugh:

is working perfectly, thanks!
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,750
Members
452,940
Latest member
rootytrip

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