473,406 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

2 IF Statements on the same macro????

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 2557
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
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
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
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("Selection Sheet").Range("G13") = 1 Then
Sheets("Price Sheet").Select
Range("B10") = "Master"
Range("D10") = "M"

End If

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

End If

If Sheets("Selection 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("Selection Sheets").Range("E13") = 1 Then
Sheets("Price Sheet").Select
Range("A10") = "1.5/8 Ash Vessel"

ElseIf Sheets("Selection Sheet").Range("E13") = 2 Then
Sheets("Price Sheet").Select
Range("A10") = "1.5/8 Ash Vessel (Hung)"
Dec 5 '06 #5
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
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...
1
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...
5
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....
2
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...
1
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"...
1
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...
2
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...
9
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...
40
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.