473,587 Members | 2,489 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

2 IF Statements on the same macro????

12 New Member
I am currently having difficulties trying to get a macro to work. I am designing a system to automatically select some prices, and there are 5 different menus that can be selected. I am using several IF fucntions within the macro, but when I run it only the first IF function runs. Can anyone help????

An example of my code is below:

If Range("G13") = 1 Then
Sheets("Price Sheet").Select
Range("B10") = "Master"
Range("D10") = "M"

If Range("H13") = 1 Then
Sheets("Price Sheet").Select
Range("B11") = "Slave"
Range("D11") = "M"


If Range("I13") = 1 Then
Sheets("Price Sheet").Select
Range("B12") = "Outlet"
Range("D12") = "M"
End If
End If
End If
Dec 5 '06 #1
5 2562
Andrew Thackray
76 New Member
Your problem is that you are trying to nest the if statements without an ELSE statement in the if when what you want is a simple set of if statements.

If you step through the code and ask youself what happens when the first if statement fails you will find that the code will pass to the end if which relates to the first if which is the last end if in the code.

The rwo possible correct constructs for your code are

Expand|Select|Wrap|Line Numbers
  1.  
  2. If Range("G13") = 1 Then
  3.      Sheets("Price Sheet").Select
  4.      Range("B10") = "Master"
  5.      Range("D10") = "M"
  6. End If
  7.  
  8. If Range("H13") = 1 Then
  9.     Sheets("Price Sheet").Select
  10.     Range("B11") = "Slave"
  11.     Range("D11") = "M"
  12. end if
  13.  
  14. If Range("I13") = 1 Then
  15.     Sheets("Price Sheet").Select
  16.     Range("B12") = "Outlet"
  17.     Range("D12") = "M"
  18. end if
  19.  
  20.  
This construct is valid if you are testing for each range independantly and it is OK if more than one range is valid.

Expand|Select|Wrap|Line Numbers
  1.  
  2. If Range("G13") = 1 Then
  3.      Sheets("Price Sheet").Select
  4.      Range("B10") = "Master"
  5.      Range("D10") = "M"
  6. else
  7.      If Range("H13") = 1 Then
  8.           Sheets("Price Sheet").Select
  9.           Range("B11") = "Slave"
  10.           Range("D11") = "M"
  11.      else
  12.          If Range("I13") = 1 Then
  13.               Sheets("Price Sheet").Select
  14.               Range("B12") = "Outlet"
  15.               Range("D12") = "M"
  16.          end if
  17.      end if
  18. end if
  19.  
This construct only allows for one range to be true. If more than one range is true then the code will only execute for the first true range.

Also note the indenting convention used in the code. This ensures that each IF THEN ELSE line that belongs together is lined up at the same indent level and that each block of code that executes is clearly associated with the if or else clause that causes it to execute.

This makes it easier to see to logic in the code and ensure that each of your multi line if statements has corresponding ELSE and END IF lines associated with it.
Dec 5 '06 #2
nickwright
12 New Member
Thank you for your help but that does not seem to work, it only selects the first statement that is true, I need all the statements to be selected if they are true. Can you help with this problem
Dec 5 '06 #3
Andrew Thackray
76 New Member
The problem is that you are referencing values the worksheet that is active when your macro is executed which is not the worksheet "Price Sheet".

Hence the if statements will never be true and the "Price Sheet" never selected

The following modification specifies the ranges on the worksheet "Price Sheet" explicitly and should work

[code]
If Sheets("Price Sheet").Range(" G13") = 1 Then
Sheets("Price Sheet").Select
Range("B10") = "Master"
Range("D10") = "M"
End If

If Sheets("Price Sheet").Range(" H13") = 1 Then
Sheets("Price Sheet").Select
Range("B11") = "Slave"
Range("D11") = "M"
end if

If Sheets("Price Sheet").Range(" I13") = 1 Then
Sheets("Price Sheet").Select
Range("B12") = "Outlet"
Range("D12") = "M"
end if
Dec 5 '06 #4
nickwright
12 New Member
Thank you very much this now works as I want it to but I have come across another problem with code that follows on from this. I would really appreciate it if you could have a look and giude me (oh and I am sorry about all the confusion)

Sub GetPrices()

If Sheets("Selecti on Sheet").Range(" G13") = 1 Then
Sheets("Price Sheet").Select
Range("B10") = "Master"
Range("D10") = "M"

End If

If Sheets("Selecti on Sheet").Range(" H13") = 1 Then
Sheets("Price Sheet").Select
Range("B11") = "Slave"
Range("D11") = "M"

End If

If Sheets("Selecti on Sheet").Range(" I13") = 1 Then
Sheets("Price Sheet").Select
Range("B12") = "Outlet"
Range("D12") = "M"

End If

[B]I want the macro to continue running after the above and I get a Run Time Error '9' Message (Subscript out of range) and the first line below is highlighted yellow[/b]

If Sheets("Selecti on Sheets").Range( "E13") = 1 Then
Sheets("Price Sheet").Select
Range("A10") = "1.5/8 Ash Vessel"

ElseIf Sheets("Selecti on Sheet").Range(" E13") = 2 Then
Sheets("Price Sheet").Select
Range("A10") = "1.5/8 Ash Vessel (Hung)"
Dec 5 '06 #5
Andrew Thackray
76 New Member
The error vis that you have names your sheet "Selection Sheets" instead of "Selection Sheet"

"Selection Sheets" does not exist & therefore any reference is out of range

A
Dec 5 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

699
33662
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it...
1
2027
by: Erik Haugen | last post by:
This item in the C++ faq: http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-38.5 discusses macros with multiple statements. The problem is that a macro such as #define MYMACRO(a,b) stmt1; stmt2; will cause unwanted astonishment if you say:
5
1436
by: wongjoekmeu | last post by:
Hello all, I have a C++ program written as a CWinApp which compiles but somewhere has an error. I use the visual studio .NET compiler by the way. I am use to programs written in unix environment. Because the program has an error I want to use print out statements using cout to check values of my variables. But the problem is that I do not...
2
3475
by: Pete | last post by:
In Access 95/97 I used to be able to create pull down menus (File,Edit ...) from a macro. It seems there used to be some wizard for that. However in Access 2000 it seems you have to build your menus by customizing a toolbar. With this method you have to create a separate macro for every single menu and sub menu. The old method would allow...
1
1918
by: Brian | last post by:
Is there a way to change the default "using" statements that are created by Visual Studio when you create a new class? Do I have to create a new file type? So, instead of choosing "New -> Class" I would have to choose "New -> MyClass". Basically, I want to add "Using System.IO;" and "Using System.Diagnostics" so that when I create a new...
1
912
by: Anthony Nystrom | last post by:
In vb this is fine... I am moving some code over to c# for a client. I have a few dataclass's that have set statements within the type defined. c# requires this... Anyway to easily (Auto :)) write those set statements to the data type for the property? Or do I need to go through them manually? Anyone have a macro, or something.... Thanks, ...
2
1833
by: WØCBF | last post by:
I am trying to copy the information from a form into a table. I have tried running the sql code from a macro by using the command : This works and writes the work 'test' into the field name 'name' in the 'temptable' table: insert into temptable (name) values (test) But I don't know the correct syntax for writing the field on the form...
9
4286
by: vivek | last post by:
Hi i have used some debug macro and called several times(hundreds) in the code. The purpose of the macro was to print the values at that time to the screen Now that i have finished debugging, i do not want to execute the macro.
40
1746
by: Bill Cunningham | last post by:
I have been thinking about hiding headers from my compiler's preprocessor and allowing others to be shown. I want to use the most used and add others is necessary. Would this be how it is properly done. I want to ask ahead of time because what I do might work but it might not be the "correct" or standard way. #include <stdio.h> #include...
0
7923
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7852
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5719
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1192
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.