Thread
:
Can VBA If...Then execute a block of code instead of a single line
View Single Post
#
1
Posted to microsoft.public.excel.programming
Brian
external usenet poster
Posts: 683
Can VBA If...Then execute a block of code instead of a single
Much simpler. Thanks
--
Brian
"Don Guillett" wrote:
WithOUT selections
If MsgBox("continue?", vbYesNo + vbQuestion) = vbYes Then
Range("A5")= "Hello World!"
Range("C3")= "How are ya!"
Else
Range("C3")= "No Comment"
Range("A5")= "Choose another planet!"
End If
--
Don Guillett
Microsoft MVP Excel
SalesAid Software
"Brian" wrote in message
...
Hi Don,
I previously thought that the IF...THEN did not allow me to execute more
than one statement line, but the following test code seems to be working.
The "Yes" in B2 yields the expected formula results in A5 and C3 when the
code executes.
Range("B2").Select
If ActiveCell.FormulaR1C1 = "Yes" = True Then
Range("A5").Select
ActiveCell.FormulaR1C1 = "Hello World!"
Range("C3").Select
ActiveCell.FormulaR1C1 = "How are ya!"
Else
Range("C3").Select
ActiveCell.FormulaR1C1 = "No Comment"
End If
Range("A6").Select
ActiveCell.FormulaR1C1 = "Finished"
End Sub
I then thought perhaps my trouble was with a Message Box Boolean code
condition that would only execute one line of code per result, but the
following test code works as well.
If MsgBox("Do you wish to continue?", vbYesNo + vbQuestion) = vbYes _
Then
Range("A5").Select
ActiveCell.FormulaR1C1 = "Hello World!"
Range("C3").Select
ActiveCell.FormulaR1C1 = "How are ya!"
Else
Range("C3").Select
ActiveCell.FormulaR1C1 = "No Comment"
Range("A5").Select
ActiveCell.FormulaR1C1 = "Choose another planet!"
End If
End Sub
There must have been some other reason my original code did not perform as
expected. I will have to dig deeper.
Thanks for all of your kind efforts!
--
Brian
"Don Guillett" wrote:
Unless I am not understanding your question the simple if will take care
of
it.
IF var = True THEN do this
if false do this will not happen
if var1=2 then
range("a9").copy range("b12")
end if
next thing to do
--
Don Guillett
Microsoft MVP Excel
SalesAid Software
"Brian" wrote in message
...
Thanks Don & Chip,
Your posts answered some other questions I had and gave me some
formating
guidance, however, my particular question (perhaps not phrased
properly)
was
can an "If ... Then" sequence handle more that a single line of code
per
result.
Back in the old days, before
VB
, we used a GOSUB ... RETURN sequence
that
would run a multi-line block of code and RETURN to the next line in
sequence
once completed, e.g.
IF var = True THEN GOSUB SubRoutine1
SubRoutine1:
1 Line of code
2 Line of code
3 Lines of code
RETURN
In the above case, if the var = False the GOSUB would be ignored and
the
next line of code following the IF...THEN line would be executed. My
question is simply this: Using the above example, how do I get code
lines
1-3 to execute only if var= True. Do I simply create a separate macro
subroutine and use this format?
IF var = True THEN RUN("MacroSubRoutine1")
Since the VBA indentation format in the If block does not seem to work
for
multiple lines.
Thanks for all of your help.
--
Brian
"Don Guillett" wrote:
Your problem was a continuation after the first then _
I would write it like this
With ActiveSheet' you could substitute the sheet name & NOT select
If .Range("i6") = "HCP Added" Then
.Unprotect
.Range("I6").ClearContents
.Protect DrawingObjects:=True, Contents:=True, _
Scenarios:=True, AllowInsertingHyperlinks:=True
End If
Calculate
With Sheets("Data Entry")
If .Visible = True Then .Visible = False
End With
With Sheets("Main Menu")
If .Visible = True Then Application.Goto .Range("F4:H4")
End With
--
Don Guillett
Microsoft MVP Excel
SalesAid Software
"Brian" wrote in message
...
Hello All,
Is there some way in Excel 2007 VBA macro code to have a block of
code
perform if a condition is true and have the code block skipped if
the
condition is false? So far I have only been able to get an
If...Then
condition to work with a single line of code following the Then
statement.
Do I use GoSub...Return?
See the following code:
Range("I6").Select
If ActiveCell.FormulaR1C1 = "HCP Added" Then _
ActiveSheet.Unprotect
Range("I6").Select
Selection.ClearContents
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, _
Scenarios:=True, AllowInsertingHyperlinks:=True
End If
Calculate
If Sheets("Data Entry").Visible = True Then
Sheets("Data Entry").Select
ActiveWindow.SelectedSheets.Visible = False
End If
If Sheets("Main Menu").Visible = True Then
Sheets("Main Menu").Select
Range("F4:H4").Select
End If
End Sub
Can you assist and tell me what I am doing wrong?
--
Brian
Reply With Quote
Brian
View Public Profile
Find all posts by Brian