#1
|
|||
|
|||
Macro to Skip Calcuation if Field is Blank?!
Hello, I have the following macro in my document to calculate a payment amount, based on the times per year the contract is invoiced. My macro is repeated for Year3 - Year5... I wanted to know if there is something that I can add to this so that if any years beyond the Year1 are not filled in (form field left blank) the macro won't try to calculate. Right now, if some of the form fields aren't filled in, I get an error message when I exit the fields that are empty. Code:
Sub CalcPayable() Last edited by macropod; 04-05-2013 at 02:13 PM. Reason: Added code tags & formatting |
#2
|
|||
|
|||
1. use formfield objects (docFF in code below)
2. use Select Case (for the cycle type) and then a test of Year (not = 0) Option Explicit Code:
Sub CalcPayable() ' ' CalcPayable Macro Dim docFF As FormFields Set docFF = docFF() Select Case docFF("Cycle").Result Case "Annually" If docFF("Year1").Result <> 0 Then docFF("Payable1").Result = docFF("Year1").Result If docFF("Year2").Result <> 0 Then docFF("Payable2").Result = docFF("Year2").Result If docFF("Year3").Result <> 0 Then docFF("Payable3").Result = docFF("Year3").Result ' same for year 4 and 5 Case "Semiannually" If docFF("Year1").Result <> 0 Then docFF("Payable1").Result = docFF("Year1").Result / 2 If docFF("Year2").Result <> 0 Then docFF("Payable2").Result = docFF("Year2").Result / 2 If docFF("Year3").Result <> 0 Then docFF("Payable3").Result = docFF("Year3").Result / 2 ' same for year 4 and 5 Case "Quarterly" If docFF("Year1").Result <> 0 Then docFF("Payable1").Result = docFF("Year1").Result / 4 If docFF("Year2").Result <> 0 Then docFF("Payable2").Result = docFF("Year2").Result / 4 If docFF("Year3").Result <> 0 Then docFF("Payable3").Result = docFF("Year3").Result / 4 ' same for year 4 and 5 Case "Bimonthly" If docFF("Year1").Result <> 0 Then docFF("Payable1").Result = docFF("Year1").Result / 6 If docFF("Year2").Result <> 0 Then docFF("Payable2").Result = docFF("Year2").Result / 6 If docFF("Year3").Result <> 0 Then docFF("Payable3").Result = docFF("Year3").Result / 6 ' same for year 4 and 5 Case "Monthly" If docFF("Year1").Result <> 0 Then docFF("Payable1").Result = docFF("Year1").Result / 12 If docFF("Year2").Result <> 0 Then docFF("Payable2").Result = docFF("Year2").Result / 12 If docFF("Year3").Result <> 0 Then docFF("Payable3").Result = docFF("Year3").Result / 12 ' same for year 4 and 5 Case "Triannually" If docFF("Year1").Result <> 0 Then docFF("Payable1").Result = docFF("Year1").Result / 3 If docFF("Year2").Result <> 0 Then docFF("Payable2").Result = docFF("Year2").Result / 3 If docFF("Year3").Result <> 0 Then docFF("Payable3").Result = docFF("Year3").Result / 3 ' same for year 4 and 5 End Select End Sub |
#3
|
||||
|
||||
Personally, I'd forego the macro and use a series of formula fields.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#4
|
|||
|
|||
Fumei - Thank you for the detailed response. The "docFF" is confusing me though. What am I referencing there?
Macropod - Are you suggesting using an IF formula in those fields, similar to the idea of my original macro? |
#5
|
||||
|
||||
yes, that kind of thing is possible, but it makes little difference (the main one being whether macros are allowed to run) if your document already has other macros in it.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
My document does have other macros in it. Does that mean I can't use the calculation feature in the fields? I created the following calculation (made sure it worked in Excel before transferring over to Word).
=IF(Cycle="Annually", "Year1"/1, IF(Cycle ="Semiannually", "Year1"/2, IF(Cycle ="Quarterly", "Year1"/4, IF(Cycle ="Monthly", "Year1"/12, IF(Cycle ="Bimonthly", "Year1"/6, IF(Cycle ="Triannually", "Year1"/3,0)))))) When I protected the document and tabbed through the fields, it skipped over the field with the formula and didn't calculate anything. Also, the default of the form field with the calculation was showing "$t:00" Which is another issue... I can't have anything showing as a default because if there isn't anything entered in the fields, I want the printed document to look like those fields don't exist. Going back to the macro by Fumei - does "docFF" need to be changed to ActiveDocument.FormFields? since that is what I am referencing? |
#7
|
||||
|
||||
Quote:
Quote:
http://windowssecrets.com/forums/sho...l=1#post320143 or http://www.gmayor.com/downloads.htm#Third_party
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
Ooops! My bad. Yes docFF should refers to all the formfields in the document.
Code:
Dim docFF as FormFields ' declares docFF as the formfield collection Set docFF = ActiveDocument.Formfields() ' sets the object If you do go the field calculation route you are strongly advised to to listen to Paul and check out his Word Field Maths Tutorial. It is a few years old, but is still relevant and is still the very best learning source out there. |
#9
|
||||
|
||||
I didn't call you on that because I haven't been monitoring this thread closely. Given your level of competence, I'm not inclined to check your work. Of course, as you know from our discussion at VBAX, I too make mistakes sometimes ...
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#10
|
|||
|
|||
That is OK. My tongue was firmly planted in my cheek. Ummmm, hopefully that expression works well (as intended) in Oz.
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Macro to remove blank column | netchie | Word VBA | 4 | 03-14-2013 02:22 PM |
Email macro to read range for .To field | MattMurdock | Excel Programming | 2 | 08-07-2012 06:46 AM |
Assign macro but hide the button/field | trlear | Word VBA | 5 | 02-23-2012 04:35 PM |
Form Fields - Create blank form text field with if statement? | LAssist2011 | Word | 5 | 12-14-2011 03:02 PM |
How to turn all blank lines into non-blank for legal forms | sieler | Word | 3 | 04-12-2009 01:38 PM |