LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 683
Default 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








 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Single line of code with line separator KarenF Excel Programming 4 August 19th 08 01:43 AM
how do you execute single line of code? honestlylion Excel Discussion (Misc queries) 2 February 24th 06 03:35 PM
VBA Array Population with a Single Line of Code James B Excel Programming 7 February 6th 04 09:37 AM
Way to change a single line of vb code in several hundred excel files? bball887 Excel Programming 2 December 16th 03 03:24 PM
Way to change a single line of vb code in several hundred excel files? Alex[_13_] Excel Programming 0 December 15th 03 06:13 PM


All times are GMT +1. The time now is 10:20 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"