If/Then statements in Visual Basic

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm trying to create an If/Then statement in Visual Basic, but I cannot get it to work. What I want is to have the test in a cell in BOLD depending on the condition (whether a number equals 1,2, or 3. "P13" is the cell the condition is located and Q16, Q18, and Q20 are the cells I want bolded. Here is my attempt that's not working. Any replys are appreciated.

If "P13" = 1 Then
Range("Q16").Select
Selection.Font.Bold = True
ElseIf "P13" = 2 Then
Range("Q18").Select
Selection.Font.Bold = True
Else
Range("Q20").Select
Selection.Font.Bold = True
End If
 
Woody,

If Range("P13").Value = 1 Then
Range("Q16").Font.Bold = True
ElseIf Range("P13").Value = 2 Then
Range("Q18").Font.Bold = True
Else
Range("Q20").Font.Bold = True
End

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

Woody said:
I'm trying to create an If/Then statement in Visual Basic, but I cannot
get it to work. What I want is to have the test in a cell in BOLD
depending on the condition (whether a number equals 1,2, or 3. "P13" is the
cell the condition is located and Q16, Q18, and Q20 are the cells I want
bolded. Here is my attempt that's not working. Any replys are appreciated.
 
Hi Woody

One way is to use Select Case

Sub test()
Select Case Range("P13").Value
Case Is = 1
Range("Q16,Q18,q20").Font.Bold = False
Range("Q16").Font.Bold = True
Case Is = 2
Range("Q16,Q18,q20").Font.Bold = False
Range("Q18").Font.Bold = True
Case Is = 3
Range("Q16,Q18,q20").Font.Bold = False
Range("Q20").Font.Bold = True
End Select
End Sub


--
Regards Ron de Bruin
(Win XP Pro SP-1 XL2000-2003)




Woody said:
I'm trying to create an If/Then statement in Visual Basic, but I cannot get it to work. What I want is to have the test in a
cell in BOLD depending on the condition (whether a number equals 1,2, or 3. "P13" is the cell the condition is located and Q16,
Q18, and Q20 are the cells I want bolded. Here is my attempt that's not working. Any replys are appreciated.
 
I'm trying to create an If/Then statement in Visual Basic, but I cannot get it to work. What I want is to have the test in a cell in BOLD depending on the condition (whether a number equals 1,2, or 3. "P13" is the cell the condition is located and Q16, Q18, and Q20 are the cells I want bolded. Here is my attempt that's not working. Any replys are appreciated.

If "P13" = 1 Then
Range("Q16").Select
Selection.Font.Bold = True
ElseIf "P13" = 2 Then
Range("Q18").Select
Selection.Font.Bold = True
Else
Range("Q20").Select
Selection.Font.Bold = True
End If


IT's helpful if you tell us what happens when you run your code.

But your "Conditions" will never be true. The string "P13" will never equal 1,
2 or 3. If you want the contents of a cell at address P13, then you need to
address that. e.g. [P13]; Range("P13").value; etc.

Some random thoughts/questions:

1. What kind of procedure contains this code?
Functions return values. Sub's can alter cells.

2. Selecting cells is unnecessary. [Q16].Font.Bold = True

3. My bias would be to use the Select Case construct

e.g. Select Case Range("P13").Value
Case 1
[Q16].Font.Bold = True
Case 2
[Q16].Font.Bold = True

etc.

4. Each time you run this procedure, it will set on of your target cells to
Bold depending on the contents of P13. Those cells will stay bold forever <g>,
unless you explicitly reset that property.

5. If the behavior in comment 4 is not what you want, I would probably use
Conditional Formatting.


--ron
 
Woody

Hit ALT + F11 to get to Visual Basic Editor. Make sure you are in Project
Explorer(View>Project Explorer)

Find your workbook/project and select it with a mouse-click.

Insert>Module. Paste the code in there. Delete it from your worksheet.

Hit Alt + Q to return to Excel Window.

Tools>Macro>Macros. Select the macro by name and "Run".

Gord Dibben Excel MVP
 
Just another idea similar to Ron's.

Sub Demo()
Select Case [P13]
Case 1 To 3
[Q16,Q18,Q20].Font.Bold = False
[Q16].Offset(2 * [P13] - 2).Font.Bold = True
End Select
End Sub

--
Dana DeLouis
Using Windows XP & Office XP
= = = = = = = = = = = = = = = = =


Woody said:
I'm trying to create an If/Then statement in Visual Basic, but I cannot
get it to work. What I want is to have the test in a cell in BOLD
depending on the condition (whether a number equals 1,2, or 3. "P13" is the
cell the condition is located and Q16, Q18, and Q20 are the cells I want
bolded. Here is my attempt that's not working. Any replys are appreciated.
 
Back
Top