Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-02-2013, 11:18 PM
Scheuerman1987 Scheuerman1987 is offline How do you write a macro with an IF Function Windows Vista How do you write a macro with an IF Function Office 2013
Novice
How do you write a macro with an IF Function
 
Join Date: May 2013
Location: Corona, CA
Posts: 4
Scheuerman1987 is on a distinguished road
Default How do you write a macro with an IF Function

I need to write a macro that does the following. I'm not very good with macros or VBA and I think my text book words this step really confusing. This is an extra credit assignment but I would still like to complete it. I have attached my workbook below so if anyone can help me write the code for the macro I'd really appreciate it.



Write a macro named Insert_Transaction to copy the transaction data from the Transaction worksheet and insert it at the top of the list of transactions in the Auction Account worksheet. (Hint: You can use the macro recorder to record the steps to copy sample data from the Transaction sheet to the Auction Account sheet.) The macro should include the following:

An If statement that verifies that there is enough money in the auction account to cover the transaction. If there is not enough money, the macro should prevent the transaction from being entered into the auction account and notify the user with a message box, indicating how much money is in the account.

If the transaction is covered by the account, copy the values from the Transaction worksheet into the Auction Account worksheet. After the values have been copied, use a message box to notify the user of a successful transaction along with the current account balance. Replace the values in the Transaction worksheet with blanks.

Test the macro by entering the data shown below in the order shown. Sales are entered as positive values and purchases are entered as negative values.

Date Description Item Amount

4/2/2013 Purchase Bach Trumpet with Mouth Piece - $165
4/4/2013 Sale King 1250 Flugelhorn $210
4/7/2013 Purchase Schiller Lightweight Trumpet - $350

https://docs.google.com/file/d/0B6iC...it?usp=sharing
Reply With Quote
  #2  
Old 06-04-2013, 09:19 AM
BobBridges's Avatar
BobBridges BobBridges is offline How do you write a macro with an IF Function Windows 7 64bit How do you write a macro with an IF Function Office 2010 32bit
Expert
 
Join Date: May 2013
Location: USA
Posts: 700
BobBridges has a spectacular aura aboutBobBridges has a spectacular aura about
Default

I had to look at the worksheet and your copy of the instructions to figure this out, but I see now what they want. So you think you know what you have to do, except the part about the If statement, right?

I don't know whether you meant it literally or you just speaking loosely, but you wrote "IF function" in the subject of this thread while the assignment says "If statement". The assignment said it right. Technically you could use an Excel IF function in VBA, but that's going around your elbow to get to your thumb.

You already know what an IF function looks like in Excel. In VBA, an If statement looks different but does much the same thing. Let's say that you've already got your program to look at the current balance (Ending Balance in row 4 of the Account Auction worksheet), and put that value in a variable named Balance; and that you've put the next transaction amount in a variable named XctAmt. Your program is supposed to create a new row in AccountAuction—but before it does, it needs to make sure that there's enough money in the account. That is, once you add a negative transaction amount to a positive balance, the new value should still be positive. In programming there is always more than one way to do that, but the way I choose to demonstrate here goes like this:

If Balance + XctAmt < 0 Then
MsgBox "Error!"
Exit Sub
End If

Note what happens: The program adds Balance and XctAmt, then compares the result to 0. If it's less than 0, then there isn't enough money in the account to cover the transaction, in which case (so say your instructions), you should generate an error message and stop the program.

My error message said simply "Error!", but of course that's not enough information; you'll have to decide just how to word the message to satisfy the requirements of te assignment. After displaying that message is the Exit Sub statement, which stops execution of the subroutine.

If there is enough money in the account, then the program skips over those instructions and just goes on with whatever you tell it to do next, the rest of the program in other words.

If you're "not very good" with VBA then no doubt you'll have other questions, but this should help with the If statement. If it's worse than that—if you've never written anything in VBA before, nor in any other language—then we should back up and proceed one statement at a time, so that you can see what you're building and how each piece works with the rest. But I'll leave it to you to ask the right questions, at first.
Reply With Quote
  #3  
Old 06-04-2013, 10:03 AM
CGM3 CGM3 is offline How do you write a macro with an IF Function Windows XP How do you write a macro with an IF Function Office 2007
Advanced Beginner
 
Join Date: Oct 2009
Posts: 38
CGM3 is on a distinguished road
Default

Actually, if Balance is the available amount, and XctAmt is the (positive) "cost" that will be deducted, methinks the code should be:

Code:
   If Balance < XctAmt Then
      ' error message, etc
   Else
      ' deduct amount from balance
   End If
Reply With Quote
  #4  
Old 06-04-2013, 10:25 AM
BobBridges's Avatar
BobBridges BobBridges is offline How do you write a macro with an IF Function Windows 7 64bit How do you write a macro with an IF Function Office 2010 32bit
Expert
 
Join Date: May 2013
Location: USA
Posts: 700
BobBridges has a spectacular aura aboutBobBridges has a spectacular aura about
Default

Normally, yes, CGM3. But I looked at the sample workbook; purchases are negative and sales are positive, so it's the other way around.
Reply With Quote
  #5  
Old 06-05-2013, 05:32 AM
CGM3 CGM3 is offline How do you write a macro with an IF Function Windows XP How do you write a macro with an IF Function Office 2007
Advanced Beginner
 
Join Date: Oct 2009
Posts: 38
CGM3 is on a distinguished road
Default

Quote:
Originally Posted by BobBridges View Post
Normally, yes, CGM3. But I looked at the sample workbook; purchases are negative and sales are positive, so it's the other way around.
So use:

If Balance < ABS(XctAmt) Then

(Has the advantage of implying, for the unfamiliar user, that XctAmt might be negative)
Reply With Quote
  #6  
Old 06-05-2013, 06:04 AM
BobBridges's Avatar
BobBridges BobBridges is offline How do you write a macro with an IF Function Windows 7 64bit How do you write a macro with an IF Function Office 2010 32bit
Expert
 
Join Date: May 2013
Location: USA
Posts: 700
BobBridges has a spectacular aura aboutBobBridges has a spectacular aura about
Default

Sure, if the transaction amount was always negative. But sometimes the transaction is positive, so that test would forbid, for example, selling a trumpet for $120 on the grounds that there's only $45 in the account.
Reply With Quote
Reply

Tags
excel 2013, macros, vba excel



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro or user function to Extract row height Catalin.B Excel 12 06-22-2011 09:21 AM
How do you write a macro with an IF Function How to write a macro to find a specified name in a list of data? Jaffa Excel 1 10-23-2010 02:39 PM
automatic start of macro + help on writing helper function vsempoux Word 3 10-09-2009 03:01 AM
How do you write a macro with an IF Function Macro or Function to know wether a string is included in a text Eduardo Word VBA 5 06-15-2009 01:55 AM
How do you write a macro with an IF Function Help! for using an appropriate function/Macro in Excel pawan2paw Excel 1 06-04-2009 12:28 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:33 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft