docteam135
10-15-2014, 06:51 PM
Hey guys,
So I've been working on this particular code for a couple days now, and I keep getting an Overflow error. Basically, this is a user form where the user inputs the number of days they will be requiring the services of specific employees, and when the click okay, the code calculates the total cost, and changes the value of certain doc variables. These variables then show up in various places in the document, so the code also updates all fields.
It's a little complicated, but I've tried this same code on a smaller scale and it worked fine. I've been over it a hundred times, so I know I must be missing something. I did some Googling about the Overflow error, and tried switching some of my variables to Long but it made no difference. I'm still getting stuck in this one place. The line in bright red below is where I'm getting the error. I'm pretty new to macros, so I'm not sure what else to try, or even where to look.
I'm attaching the Word doc with the macros, I only copied the sub that had the line. Thanks.
Private Sub cmdOK_Click()
CostCalcForm1.Hide
'Change variables PM
With ActiveDocument
strPMOnsiteOld = ActiveDocument.Variables("vPMOnsiteDays")
strPMOnsiteNew = CostCalcForm1.txtPMOnsiteDays
If strPMOnsiteOld <> strPMOnsiteNew Then _
.Variables("vPMOnsiteDays").Value = strPMOnsiteNew
strPMRemoteOld = ActiveDocument.Variables("vPMRemoteDays")
strPMRemoteOld = CostCalcForm1.txtPMRemoteDays
If strPMRemoteOld <> strPMRemoteNew Then _
.Variables("vPMRemoteDays").Value = strPMRemoteNew
'Calculate
vPMOnsite = vPMOnsiteDays * 1250
vPMRemote = vPMRemoteDays * 1000
End With
'Change variables BA
With ActiveDocument
strBAOnsiteOld = ActiveDocument.Variables("vBAOnsiteDays")
strBAOnsiteNew = CostCalcForm1.txtBAOnsiteDays
If strBAOnsiteOld <> strBAOnsiteNew Then _
.Variables("vBAOnsiteDays").Value = strBAOnsiteNew
strBARemoteOld = ActiveDocument.Variables("vBARemoteDays")
strBARemoteOld = CostCalcForm1.txtBARemoteDays
If strBARemoteOld <> strBARemoteNew Then _
.Variables("vBARemoteDays").Value = strBARemoteNew
'Calculate
vBAOnsite = vBAOnsiteDays * 1500
vBARemote = vBARemoteDays * 1000
End With
'Change variables PQRS
With ActiveDocumnet
strPQRSrOld = ActiveDocument.Variables("vPQRSrProviders")
strPQRSrNew = CostCalcForm1.txtPQRSr
If strPQRSrOld <> strPQRSrNew Then _
.Variables("vPQRSrProviders").Value = strPQRSrNew
strPQRSrRangeOld = ActiveDocument.Variables("vPQRSrRange")
strPQRSrRangeNew = CostCalcForm1.cmbPQRS
If strPQRSrRangeOld <> strPQRSrRangeNew Then _
.Variables("vPQRSrRange").Value = strPQRSrRangeNew
strPQRScOld = ActiveDocument.Variables("vPQRScRemoteDays")
strPQRScNew = CostCalcForm1.txtPQRSc
If strPQRScOld <> strPQRScNew Then _
.Variables("vPQRScRemoteDays").Value = strPQRScNew
'Calculate
vPQRSr = (vPQRSrProviders * 250) + vPQRSFee
vPQRSc = vPQRScRemoteDays * 750
End With
'Change variables MU
With ActiveDocument
strMUOnsiteOld = ActiveDocument.Variables("vMUOnsiteDays")
strMUOnsiteNew = CostCalcForm1.txtMUOnsite
If strMUOnsiteOld <> strMUOnsiteNew Then _
.Variables("vMUOnsiteDays").Value = strMUOnsiteNew
strMURemoteOld = ActiveDocument.Variables("vMURemoteDays")
strMURemoteNew = CostCalcForm1.txtMURemote
If strMURemoteOld <> strMURemoteNew Then _
.Variables("vMURemoteDays").Value = strMURemoteNew
'Calculate
vMUOnsite = vMUOnsiteDays * 1250
vMURemote = vMURemoteDays * 1000
End With
'Change variables HBI
With ActiveDocument
strHBIOnsiteOld = ActiveDocument.Variables("vHBIOnsiteDays")
strHBIOnsiteNew = CostCalcForm1.txtHBIOnsiteDays
If strHBIOnsiteOld <> strHBIOnsiteNew Then _
.Variables("vHBIOnsiteDays").Value = strHBIOnsiteNew
strHBIRemoteOld = ActiveDocument.Variables("vHBIRemoteDays")
strHBIRemoteNew = CostCalcForm1.txtHBIRemoteDays
If strHBIRemoteOld <> strHBIRemoteNew Then _
.Variables("vHBIRemoteDays").Value = strHBIRemoteNew
'Calculate
vHBIOnsite = vHBIOnsiteDays * 1500
vHBIRemote = vHBIRemoteDays * 1000
End With
'Change variables EIS
With ActiveDocument
strEISOld = ActiveDocument.Variables("vEISOnsiteMo")
strEISNew = CostCalcForm1.cmbEISOnsiteMo
If strEISOld <> strEISNew Then _
.Variables("vEISOnsiteMo").Value = strEISNew
End With
'Change variables IIS
With ActiveDocument
strIISOnsiteOld = ActiveDocument.Variables("vIISOnsiteDays")
strIISOnsiteNew = CostCalcForm1.txtIISOnsiteDays
If strIISOnsiteOld <> strIISOnsiteNew Then _
.Variables("vIISOnsiteDays").Value = strIISOnsiteNew
strIISRemoteOld = ActiveDocument.Variables("vIISRemoteDays")
strIISOnsiteNew = CostCalcForm1.txtIISRemoteDays
If strIISRemoteOld <> strIISRemoteNew Then _
.Variables("vIISRemoteDays").Value = strIISRemoteNew
'Calculate
vIISOnsite = vIISOnsiteDays * 1500
vIISRemote = vIISRemoteDays * 1000
End With
'Change variables Training
With ActiveDocument
strTrainingOnsiteOld = ActiveDocument.Variables("vTrainingOnsiteDays")
strTrainingOnsiteNew = CostCalcForm1.txtTrainingOnsiteDays
If strTrainingOnsiteOld <> strTrainingOnsiteNew Then _
.Variables("vTrainingOnsiteDays").Value = strTrainingOnsiteNew
strTrainingRemoteOld = ActiveDocument.Variables("vTrainingRemoteDays")
strTrainingRemoteNew = CostCalcForm1.txtTrainingRemoteDays
If strTrainingRemoteOld <> strTrainingRemoteNew Then _
.Variables("vTrainingRemoteDays").Value = strTrainingRemoteNew
'Calculate
vTrainingOnsite = vTrainingOnsiteDays * 1000
vTrainingRemote = vTrainingRemoteDays * 750
End With
'Change variables ESS
With ActiveDocument
strESSOnsiteOld = ActiveDocument.Variables("vESSOnsiteDays")
strESSOnsiteNew = CostCalcForm1.txtESSOnsiteDays
If strESSOnsiteOld <> strESSOnsiteNew Then _
.Variables("vESSOnsiteDays").Value = strESSOnsiteNew
'Calculate
vESSOnsite = vESSOnsiteDays * 1500
End With
'Change variables ICD
With ActiveDocument
strICDOld = ActiveDocument.Variables("vICDRemoteDays")
strICDNew = CostCalcForm1.txtICD
If strICDOld <> strICDNew Then _
.Variables("vICDRemoteDays").Value = strICDNew
'Calculate
vICDRemote = vICDRemoteDays * 750
End With
'Change variables RCC
With ActiveDocument
strRCCOnsiteOld = ActiveDocument.Variables("vRCCOnsiteDays")
strRCCOnsiteNew = CostCalcForm1.txtRCCOnsite
If strRCCOnsiteOld <> strRCCOnsiteNew Then _
.Variables("vRCCOnsiteDays").Value = strRCCOnsiteNew
strRCCRemoteOld = ActiveDocument.Variables("vRCCRemoteDays")
strRCCRemoteNew = CostCalcForm1.txtRCCRemote
If strRCCRemoteOld <> strRCCRemoteNew Then _
.Variables("vRCCRemoteDays").Value = strRCCRemoteNew
'Calculate
vRCCOnsite = vRCCOnsiteDays * 1500
vRCCRemote = vRCCRemoteDays * 1000
End With
'Calculate total cost and cost per provider
With ActiveDocument
.Variables("vTotalCost").Value = FormatCurrency(vPMOnsite + vPMRemote + vBAOnsite + vBARemote + vPQRSr + vPQRSc _
+ vRCCOnsite + vRCCRemote + vHBIOnsite + MURemote + MUOnsite + vICDRemote _
+ vHBIRemote + vEISOnsite + vIISOnsite + vIISRemote + vTrainingOnsite + vTrainingRemote _
+ vESSOnsite)
End With
With ActiveDocument
.Variables("vCostPerProvider").Value = FormatCurrency(vTotalCost / vNumberOfProviders)
End With
'Calculate cost with discount
With ActiveDocument
strOldPercent = ActiveDocument.Variables("vDiscount")
strNewPercent = CostCalcForm1.txtDiscount
If strOldPercent <> strNewPercent Then _
.Variables("vDiscount").Value = strNewPercent
strOldDiscount = ActiveDocument.Variables("vCostAfterDiscount")
strNewDiscount = FormatCurrency(vTotalCost * (100 - vDiscount))
If strOldDiscount <> strNewDiscount Then _
.Variables("vCostAfterDiscount").Value = strNewDiscount
End With
'Calculate cost monthly installments
With ActiveDocument
strOldMonthly = ActiveDocument.Variables("vCostMonthlyInstall")
strNewMonthly = FormatCurrency(vTotalCost / 12)
If strOldMonthly <> strNewMonthly Then _
.Variables("vCostMonthlyInstall").Value = strNewMonthly
End With
'Update fields
ActiveDocument.Fields.Update
'
End Sub
So I've been working on this particular code for a couple days now, and I keep getting an Overflow error. Basically, this is a user form where the user inputs the number of days they will be requiring the services of specific employees, and when the click okay, the code calculates the total cost, and changes the value of certain doc variables. These variables then show up in various places in the document, so the code also updates all fields.
It's a little complicated, but I've tried this same code on a smaller scale and it worked fine. I've been over it a hundred times, so I know I must be missing something. I did some Googling about the Overflow error, and tried switching some of my variables to Long but it made no difference. I'm still getting stuck in this one place. The line in bright red below is where I'm getting the error. I'm pretty new to macros, so I'm not sure what else to try, or even where to look.
I'm attaching the Word doc with the macros, I only copied the sub that had the line. Thanks.
Private Sub cmdOK_Click()
CostCalcForm1.Hide
'Change variables PM
With ActiveDocument
strPMOnsiteOld = ActiveDocument.Variables("vPMOnsiteDays")
strPMOnsiteNew = CostCalcForm1.txtPMOnsiteDays
If strPMOnsiteOld <> strPMOnsiteNew Then _
.Variables("vPMOnsiteDays").Value = strPMOnsiteNew
strPMRemoteOld = ActiveDocument.Variables("vPMRemoteDays")
strPMRemoteOld = CostCalcForm1.txtPMRemoteDays
If strPMRemoteOld <> strPMRemoteNew Then _
.Variables("vPMRemoteDays").Value = strPMRemoteNew
'Calculate
vPMOnsite = vPMOnsiteDays * 1250
vPMRemote = vPMRemoteDays * 1000
End With
'Change variables BA
With ActiveDocument
strBAOnsiteOld = ActiveDocument.Variables("vBAOnsiteDays")
strBAOnsiteNew = CostCalcForm1.txtBAOnsiteDays
If strBAOnsiteOld <> strBAOnsiteNew Then _
.Variables("vBAOnsiteDays").Value = strBAOnsiteNew
strBARemoteOld = ActiveDocument.Variables("vBARemoteDays")
strBARemoteOld = CostCalcForm1.txtBARemoteDays
If strBARemoteOld <> strBARemoteNew Then _
.Variables("vBARemoteDays").Value = strBARemoteNew
'Calculate
vBAOnsite = vBAOnsiteDays * 1500
vBARemote = vBARemoteDays * 1000
End With
'Change variables PQRS
With ActiveDocumnet
strPQRSrOld = ActiveDocument.Variables("vPQRSrProviders")
strPQRSrNew = CostCalcForm1.txtPQRSr
If strPQRSrOld <> strPQRSrNew Then _
.Variables("vPQRSrProviders").Value = strPQRSrNew
strPQRSrRangeOld = ActiveDocument.Variables("vPQRSrRange")
strPQRSrRangeNew = CostCalcForm1.cmbPQRS
If strPQRSrRangeOld <> strPQRSrRangeNew Then _
.Variables("vPQRSrRange").Value = strPQRSrRangeNew
strPQRScOld = ActiveDocument.Variables("vPQRScRemoteDays")
strPQRScNew = CostCalcForm1.txtPQRSc
If strPQRScOld <> strPQRScNew Then _
.Variables("vPQRScRemoteDays").Value = strPQRScNew
'Calculate
vPQRSr = (vPQRSrProviders * 250) + vPQRSFee
vPQRSc = vPQRScRemoteDays * 750
End With
'Change variables MU
With ActiveDocument
strMUOnsiteOld = ActiveDocument.Variables("vMUOnsiteDays")
strMUOnsiteNew = CostCalcForm1.txtMUOnsite
If strMUOnsiteOld <> strMUOnsiteNew Then _
.Variables("vMUOnsiteDays").Value = strMUOnsiteNew
strMURemoteOld = ActiveDocument.Variables("vMURemoteDays")
strMURemoteNew = CostCalcForm1.txtMURemote
If strMURemoteOld <> strMURemoteNew Then _
.Variables("vMURemoteDays").Value = strMURemoteNew
'Calculate
vMUOnsite = vMUOnsiteDays * 1250
vMURemote = vMURemoteDays * 1000
End With
'Change variables HBI
With ActiveDocument
strHBIOnsiteOld = ActiveDocument.Variables("vHBIOnsiteDays")
strHBIOnsiteNew = CostCalcForm1.txtHBIOnsiteDays
If strHBIOnsiteOld <> strHBIOnsiteNew Then _
.Variables("vHBIOnsiteDays").Value = strHBIOnsiteNew
strHBIRemoteOld = ActiveDocument.Variables("vHBIRemoteDays")
strHBIRemoteNew = CostCalcForm1.txtHBIRemoteDays
If strHBIRemoteOld <> strHBIRemoteNew Then _
.Variables("vHBIRemoteDays").Value = strHBIRemoteNew
'Calculate
vHBIOnsite = vHBIOnsiteDays * 1500
vHBIRemote = vHBIRemoteDays * 1000
End With
'Change variables EIS
With ActiveDocument
strEISOld = ActiveDocument.Variables("vEISOnsiteMo")
strEISNew = CostCalcForm1.cmbEISOnsiteMo
If strEISOld <> strEISNew Then _
.Variables("vEISOnsiteMo").Value = strEISNew
End With
'Change variables IIS
With ActiveDocument
strIISOnsiteOld = ActiveDocument.Variables("vIISOnsiteDays")
strIISOnsiteNew = CostCalcForm1.txtIISOnsiteDays
If strIISOnsiteOld <> strIISOnsiteNew Then _
.Variables("vIISOnsiteDays").Value = strIISOnsiteNew
strIISRemoteOld = ActiveDocument.Variables("vIISRemoteDays")
strIISOnsiteNew = CostCalcForm1.txtIISRemoteDays
If strIISRemoteOld <> strIISRemoteNew Then _
.Variables("vIISRemoteDays").Value = strIISRemoteNew
'Calculate
vIISOnsite = vIISOnsiteDays * 1500
vIISRemote = vIISRemoteDays * 1000
End With
'Change variables Training
With ActiveDocument
strTrainingOnsiteOld = ActiveDocument.Variables("vTrainingOnsiteDays")
strTrainingOnsiteNew = CostCalcForm1.txtTrainingOnsiteDays
If strTrainingOnsiteOld <> strTrainingOnsiteNew Then _
.Variables("vTrainingOnsiteDays").Value = strTrainingOnsiteNew
strTrainingRemoteOld = ActiveDocument.Variables("vTrainingRemoteDays")
strTrainingRemoteNew = CostCalcForm1.txtTrainingRemoteDays
If strTrainingRemoteOld <> strTrainingRemoteNew Then _
.Variables("vTrainingRemoteDays").Value = strTrainingRemoteNew
'Calculate
vTrainingOnsite = vTrainingOnsiteDays * 1000
vTrainingRemote = vTrainingRemoteDays * 750
End With
'Change variables ESS
With ActiveDocument
strESSOnsiteOld = ActiveDocument.Variables("vESSOnsiteDays")
strESSOnsiteNew = CostCalcForm1.txtESSOnsiteDays
If strESSOnsiteOld <> strESSOnsiteNew Then _
.Variables("vESSOnsiteDays").Value = strESSOnsiteNew
'Calculate
vESSOnsite = vESSOnsiteDays * 1500
End With
'Change variables ICD
With ActiveDocument
strICDOld = ActiveDocument.Variables("vICDRemoteDays")
strICDNew = CostCalcForm1.txtICD
If strICDOld <> strICDNew Then _
.Variables("vICDRemoteDays").Value = strICDNew
'Calculate
vICDRemote = vICDRemoteDays * 750
End With
'Change variables RCC
With ActiveDocument
strRCCOnsiteOld = ActiveDocument.Variables("vRCCOnsiteDays")
strRCCOnsiteNew = CostCalcForm1.txtRCCOnsite
If strRCCOnsiteOld <> strRCCOnsiteNew Then _
.Variables("vRCCOnsiteDays").Value = strRCCOnsiteNew
strRCCRemoteOld = ActiveDocument.Variables("vRCCRemoteDays")
strRCCRemoteNew = CostCalcForm1.txtRCCRemote
If strRCCRemoteOld <> strRCCRemoteNew Then _
.Variables("vRCCRemoteDays").Value = strRCCRemoteNew
'Calculate
vRCCOnsite = vRCCOnsiteDays * 1500
vRCCRemote = vRCCRemoteDays * 1000
End With
'Calculate total cost and cost per provider
With ActiveDocument
.Variables("vTotalCost").Value = FormatCurrency(vPMOnsite + vPMRemote + vBAOnsite + vBARemote + vPQRSr + vPQRSc _
+ vRCCOnsite + vRCCRemote + vHBIOnsite + MURemote + MUOnsite + vICDRemote _
+ vHBIRemote + vEISOnsite + vIISOnsite + vIISRemote + vTrainingOnsite + vTrainingRemote _
+ vESSOnsite)
End With
With ActiveDocument
.Variables("vCostPerProvider").Value = FormatCurrency(vTotalCost / vNumberOfProviders)
End With
'Calculate cost with discount
With ActiveDocument
strOldPercent = ActiveDocument.Variables("vDiscount")
strNewPercent = CostCalcForm1.txtDiscount
If strOldPercent <> strNewPercent Then _
.Variables("vDiscount").Value = strNewPercent
strOldDiscount = ActiveDocument.Variables("vCostAfterDiscount")
strNewDiscount = FormatCurrency(vTotalCost * (100 - vDiscount))
If strOldDiscount <> strNewDiscount Then _
.Variables("vCostAfterDiscount").Value = strNewDiscount
End With
'Calculate cost monthly installments
With ActiveDocument
strOldMonthly = ActiveDocument.Variables("vCostMonthlyInstall")
strNewMonthly = FormatCurrency(vTotalCost / 12)
If strOldMonthly <> strNewMonthly Then _
.Variables("vCostMonthlyInstall").Value = strNewMonthly
End With
'Update fields
ActiveDocument.Fields.Update
'
End Sub