Jump to content

Error writing a formula to Excel


Recommended Posts

Hello folks,

Fairly new to AutoIt, but I've done a few scripts. However, this is my first dealing with Excel. I'm getting an AutoIt error when writing a formula ($add1) to Excel via _ExcelWriteFormula. As for the cell references in this formula; the column number would be static, but the row number is stored in a variable ($iNextRow). I write an array just before the formula and it works fine. Also, MsgBox will display the correct formula stored in $add1. Any thoughts? Thanks in advance, guys!

#include <Excel.au3>
#include <EditConstants.au3>
#include <Misc.au3>
#include <Date.au3>
#include <GUIConstantsEx.au3>
#include <Array.au3>
Global $sFilePath = @ScriptDir & "\Shipment Log.xlsx"
Global $oExcel = _ExcelBookOpen($sFilePath, 0)
#region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Shipment Log", 300, 300)
$Input1 = GUICtrlCreateInput(_NOWDATE(), 10, 30, 75, 21)
$Input2 = GUICtrlCreateInput("", 10, 80, 270, 21)
$Input3 = GUICtrlCreateInput("", 10, 130, 270, 21)
$Input4 = GUICtrlCreateInput("", 10, 180, 270, 21)
$Input5 = GUICtrlCreateInput("", 10, 230, 270, 21)
$Label1 = GUICtrlCreateLabel("Today's Date:", 10, 10, 100, 17)
$Label2 = GUICtrlCreateLabel("Location:", 10, 60, 55, 17)
$Label3 = GUICtrlCreateLabel("Contact(s):", 10, 110, 55, 17)
$Label4 = GUICtrlCreateLabel("Item Description(s):", 10, 160, 100, 17)
$Label5 = GUICtrlCreateLabel("Item QTY(s):", 10, 210, 75, 17)
$Button1 = GUICtrlCreateButton("Submit", 10, 260, 120, 30)
GUISetState()
#endregion ### END Koda GUI section ###
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
   _ExcelBookClose($oExcel, 0)
            Exit
     Case $Button1
   Local $fVisible
            Local $avData = _ExcelReadArray($oExcel, 1, 1, 1000, 1, 1)
            Local $iLastUsed = 0, $iNextRow = 0
   _WriteOrderID()
   Local $add1 = "=VLOOKUP($C" & $iNextRow & ",LocationAddresses!$A$2:$G$65,2,FALSE)" ;Variable with Excel formula I'm trying to write
   Local $aArray[6] = [$iNextRow, GUICtrlRead($Input1), GUICtrlRead($Input2), GUICtrlRead($Input3), GUICtrlRead($Input4), GUICtrlRead($Input5)]
            _ExcelWriteArray($oExcel, $iNextRow, 1, $aArray)
   MsgBox(0,"",$add1)
   MsgBox(0,"",$iNextRow)
   _ExcelWriteFormula($oExcel, $add1, $iNextRow, 7) ;ERROR LINE
   _ExcelBookClose($oExcel, 1)
   MsgBox(0, @ScriptName, "Record Saved.")
   GUICtrlSetData($Input2, "")
   GUICtrlSetData($Input3, "")
   GUICtrlSetData($Input4, "")
   GUICtrlSetData($Input5, "")
    EndSwitch
WEnd
Func _WriteOrderID()
   For $n = UBound($avData) - 1 To 1 Step -1
    If StringStripWS($avData[$n], 8) <> "" Then
        $iLastUsed = $n
        ExitLoop
    EndIf
   Next
    If $iLastUsed Then
    $iNextRow = $iLastUsed + 1
    Else
    $iNextRow = 1     
    EndIf
   $sData = _ExcelReadCell($oExcel, $iLastUsed) + 1
EndFunc
Link to comment
Share on other sites

What do you mean by "I'm getting an AutoIt error when writing a formula"?

Please post the error message so we can try to help you.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I apologize. Here is the AutoIt error:

Line 485 (File "C:\Program Files\AutoIt3\Include\Excel.au3"):

$oExcel.Activesheet.Cells($sRangeOrRow,

$iColumn).FormulaR1C1 = $aFormula

$oExcel.Activesheet.Cells($aRangeOrRow,

$iColumn).FormulaR1C1 = $sFormula^ ERROR

Error: The requested action with this object has failed.

Link to comment
Share on other sites

Can you post the values of $sRangeOrRow and $iColumn please?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I think your issues are on this line:

Local $add1 = "=VLOOKUP($C" & $iNextRow & ",LocationAddresses!$A$2:$G$65,2,FALSE)"

You have not declared the variable $C and "LocationAddresses!" is not needed.

You may want to just try putting the formula into the _ExcelWriteFormula function.

EDIT: Just realized your $C is for a location. If that is the case, you will get an error for trying to find the value of $C0, since you set the value of $iNextRow to 0.

Edited by Reg2Post
Link to comment
Share on other sites

Does it work if you use

$oExcel.Activesheet.Cells($sRangeOrRow, $iColumn).Formula = $aFormula
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Does it work if you use

$oExcel.Activesheet.Cells($sRangeOrRow, $iColumn).Formula = $aFormula

Indeed it does. Well, I assume you meant to say "$sFormula" at the end there. That works like a champ. Thanks!!
Link to comment
Share on other sites

I took $aFormula from your post #4. Looks like a typo. I'm glad it's working now!

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...