Sign in to follow this  
Followers 0
pdixon724

Error writing a formula to Excel

10 posts in this topic

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

Share this post


Link to post
Share on other sites



Maybe just try:

_ExcelWriteCell($oExcel, $add1, $iNextRow, 7)


Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

$sRangeOrRow = 3

$iColumn = 7

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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!!

Share this post


Link to post
Share on other sites

#10 ·  Posted

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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
Sign in to follow this  
Followers 0

  • Similar Content

    • VeryGut
      By VeryGut
      I'm trying to insert the following formula in cell A2 using my script:
      =if(A1=""; "YES"; "NO")
      To my understanding, the line of code should be similar to this:
      _Excel_RangeWrite($MasterFile, Default, "=if(A1=""; "YES"; "NO")", "A2")
      However, it does not work, probably due to the multiple quotation marks that confuse the script :C
      How do I avoid this problem?
    • LoneWolf_2106
      By LoneWolf_2106
      Hi everybody,
      i have a question about Excel, i have to create several charts one below the other dynamically.
      I have thought to use:
       
      $oRangeLast = .UsedRange.SpecialCells($xlCellTypeLastCell) $iRowCount = .Range(.Cells(1, 1), .Cells($oRangeLast.Row, $oRangeLast.Column)).Rows.Count  
      And then to use it in this way:
      $Graph_position = "=Test1!A"&$iRowCount+2&":K"&$iRowCount+24 But it doesn't work with charts.
      Does anyone have a suggestion?
       
    • LoneWolf_2106
      By LoneWolf_2106
      Hi all,
      i have an empty csv file, i have a non formatted text file.
      What do i want to do?
      I want to automate the process "get external data" in Excel, i want to import the data from the text file and basically create a csv file with a specific character encoding.
      Is it possible with AutoIT?
       
    • breakbadsp
      By breakbadsp
      I  want to create a excel file from my script if it does not exist.
      _ExcelBookOpen throws error=2 if file does not exist, after this error i want to create new file at this point.
      can i use _FileCreate()?
      _Logger($sLogPath, "{INFO}------: Opening Excel File: " & $sExcelPath& "") While 1 Local $oExcelTestResult = _ExcelBookOpen($sExcelPath) If @error = 2 Then If not _FileCreate($sResExcelPath) Then MsgBox(0, "Error", "Error In Opening REsult Excel File: Error: " & String(@error)) _Logger($sLogPath, "{ERROR}------: Result Excel File does not exist.. tried to create new but :ERROR : " & String(@error) & "") ExitLoop Else _Logger($sLogPath, "{INFO}------: Result Excel File does not exist.. **Created New**: ") EndIf Else ExitLoop EndIf WEnd  
    • LoneWolf_2106
      By LoneWolf_2106
      Hi everybody,
      i have to store an entire row of a Excel workbook into an array.  The row index is stored in a variable.
      How can i do it?
      Thanks in advance for your support.