Jump to content
Sign in to follow this  
smellyfingers

Excel, change values based on

Recommended Posts

smellyfingers

Hi, what is the fastest way to do this:

Read a column in excel all the way down and change every cell value that isn't an ip adress number into a unique number.

I did a small script but took almost 15 -20  minuts to complete on a 8000 rows sheet and i had 0 sleeps in it :/

Thanx in advance

-smelly-

Share this post


Link to post
Share on other sites
water

If you modify the sheet cell by cell it will take forever.

Use _Excel_RangeRead from my rewrite of the Excel UDF to read the whole column in one go into an array. Then modify the array to your needs and rewrite the column using function _Excel_RangeWrite.

For download of the UDF please check my signature.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - 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
smellyfingers

Kewl, ill try that, thanx water

Share this post


Link to post
Share on other sites
smellyfingers

Seems like i can't get the Excel_RangeRead to actually read a range it just reads 1 cell or no cells at all, how would i write a range in your function?, i can create an array with the old Excel.au3 and display it, but i'm not good with manipulating arrays and not sure how to make the changes in the array before writeback, im guessing i need the opposite of regexReplace or something :/ ?

#Include <Excel.au3>
#include <Array.au3>
#include <misc.au3>
#include <string.au3>

Global $compare
Global $boo
$oExcel = _ExcelBookOpen(@ScriptDir & "\ips.xlsx",0)
$myArray = $oExcel.Activesheet.Range("C1:C600").Value

_ArrayDisplay($myArray,"","",1)

;Find all entries in array that's not an ip-adress and change to an unique value


;Write the changes to excel

_ExcelBookClose($oExcel)

Im kinda lost here, i need a simple example on how to get started, and a bible book about arrays :)

Share this post


Link to post
Share on other sites
water

To use my rewrite of the Excel UDF (ExcelEX) you need to download the file and include it in your script. You can't mix the Excel UDF that comes with AutoIt and my rewrite.

And you need to run the latest AutoIt beta version.

To manipulate arrays I suggest to have a look at the array tutorial in the wiki.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - 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
smellyfingers

I did download and included in another script with beta run, and i can read 1 cell with rangeread like C1 but not C1:C600, wich works in the old udf that's why i got scared of the new udf and went back to mommy, ill try the array tutorial and see what i can get, thanx :)

Share this post


Link to post
Share on other sites
water

Please post the code you used with my new UDF and I will have a look.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - 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
smellyfingers

#include <Excel Rewrite.au3>
#include <Array.au3>
#include <Constants.au3>

Global $oAppl = _Excel_Open()
If @error <> 0 Then Exit MsgBox($MB_SYSTEMMODAL, "Trying to Read Range", "Error creating the Excel application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
Global $oWorkbook = _Excel_BookOpen($oAppl, @ScriptDir & "\ips.xlsx")
If @error <> 0 Then
    MsgBox($MB_SYSTEMMODAL, "Trying to Open workbook", "Error opening workbook" & $oWorkbook & " " & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    _Excel_Close($oAppl)
    Exit
EndIf

read($oWorkbook)

Func read($oWorkbook)

    Local $aResult = _Excel_RangeRead($oWorkbook, Default, "C1:C600", 2)
    If @error <> 0 Then Return MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeRead Example 3", "Error reading from workbook." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeRead Example 3", "Data successfully read." & @CRLF & "Please press enter to display the formulas of cells C1:C600.")
    _ArrayDisplay($aResult, "Excel UDF: _Excel_RangeRead Example 3 - Cells C1:C600")

EndFunc   ;read

Actually i did something very wrong with my old script, this works fine :), sorry for bothering , anyways im back to learning arrays now, and what if i don't know how big the range is? isn't there a way to just read the whole column C ?

Share this post


Link to post
Share on other sites
water

To read the whole column "C" replace "C1:C600" with "C:C".


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - 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
smellyfingers

And that's when i get this error "Error reading from workbook. @error = 5, @extended= -2147352571", as long as i select exact range it works :/

Share this post


Link to post
Share on other sites
water

I just tested with "A:A" and it worked just fine.

-2147352571 (dec) = 0x80020005 (hex) means: DISP_E_TYPEMISMATCH.

Which Excel version do you run?

I'm running Excel 2010 32 bit on Windows 7 64 bit.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - 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
smellyfingers

Im on Excel 2007 32 bit win7 64 bit at home, but im going to be using this on same specs as you at work but it still should be compatible backwards from 2010?

Share this post


Link to post
Share on other sites
water

Can you please add the following lines at the top of your script so we get detailed error information?

#include <Debug.au3>
_DebugSetup()
_DebugCOMError()

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - 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
smellyfingers

Debuginfo:

@@ DEBUG COM Error encountered in testarrayer.au3 (84) :
    Number          = 0x80020006 (-2147352570)
    WinDescription  = Okänt namn.
    Description     = 
    Source          = 
    HelpFile        = 
    HelpContext     = 
    LastDllError    = 0
    Retcode         = 0x00000000
@@ DEBUG COM Error encountered in testarrayer.au3 (918) :
    Number          = 0x80020005 (-2147352571)
    WinDescription  = Typmatchningsfel.
    Description     = 
    Source          = 
    HelpFile        = 
    HelpContext     = 
    LastDllError    = 0
    Retcode         = 0x00000000
>>>>>> Please close the "Report Log Window" to exit <<<<<<<

Share this post


Link to post
Share on other sites
water

Might be a problem with the transpose method.

Could you please give

Local $aResult = _Excel_RangeRead($oWorkbook, Default, "C1:C600", 2, True)

a try?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - 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
smellyfingers

Sure thing, gives no errors and this bug report:

@@ DEBUG COM Error encountered in testarrayer.au3 (84) :
    Number          = 0x80020006 (-2147352570)
    WinDescription  = Okänt namn.
    Description     = 
    Source          = 
    HelpFile        = 
    HelpContext     = 
    LastDllError    = 0
    Retcode         = 0x00000000
>>>>>> Please close the "Report Log Window" to exit <<<<<<<

Share this post


Link to post
Share on other sites
water

That's fine.

This "error" is raised when the UDF tries to connect to an existing Excel instance - and there is none.

So the problem is solved now?

How do you like the speed?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - 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
smellyfingers

Oh and now reading the whole column works, you were right as usual :), thanx water!

Share this post


Link to post
Share on other sites
smellyfingers

the speed is very fast reading the whole column of 65528 rows and displaying it takes only 11 seconds :)

Share this post


Link to post
Share on other sites
water

That's great.

Most of the 11 seconds is spent in _ArrayDisplay to create the GUI.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - 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  

  • Similar Content

    • nooneclose
      By nooneclose
      My program has to first search for names in Column D that do not match up with column C. I got that search to work using arrays. It was slow and I could not figure out how to delete them so I just manually put coded the names that do not belong. I found their cell location but I do not know how to store that location and delete it.
      This is what I have so far.
      Local $NameToDelete1[6]  = _Excel_RangeFind($OpenWorkbook, "Smith, Bill") _ArrayDisplay($NameToDelete1, "Excel UDF: _Excel_RangeFind Example 1", "", 0, "|", "Sheet|Name|Cell|Value|Formula|Comment") _Excel_RangeDelete($OpenWorkbook.ActiveSheet, $NameToDelete1[2], $xlShiftUp)  
      Please help, I wanted to have this program done yesterday but I did not see this problem until yesterday. 
    • nooneclose
      By nooneclose
      I need to perform a subtotal in excel and I would like to automate this process using Autoit if possible like always any and all help will be greatly appreciated. 
      I can not find a good example but the two from Microsoft. Here is one of the two from msdn.microsoft.com/en-us/vba/excel-vba/articles/range-subtotal-method-excel
      I do not really understand how to translate this into AutoIt, but I gave it a try and here is what I have.
      $OpenRange      = "A1:E200" $xlSum          = -4157 $Added_Array[2] = [2, 3] $OpenRange.Subtotal("B1", $xlSum, $Added_Array, True, False, True) I just need to perform a subtotal on a range based on a header called department, and then perform a sum on the results.
    • nooneclose
      By nooneclose
      How to use _Excel_RangeSort to sort my excel file by three different headers Column A1, B1, and C1 have headers on which I want to sort by. The headers on which I want to sort are department, employee type, and name.
      I still really new to AutoIt so I do not actually know how to properly start this line or lines of code, to be honest. The example code is the best I can do.
      _Excel_RangeSort($OpenWorkbook, Default, "A1:C1", "1:1", $xlDescending, Default, $xlYes, Default, $xlSortRows) I just need to sort by those three headers in that order of department, employee type, and name, plus in descending order.
       
      any and all help would be greatly appreciated.  Thank you!
    • Daniza
      By Daniza
      It works fine on my Excel 2007, but after I Emailed My Area Manager he told me after he Enable Macro Security nothing happen's, can someone test this to run on Excel 2016? My AM can't provide me more information 

      Thank You in Advance,
      Please see attachment.
      <snip>
    • tuffgong
      By tuffgong
      Good morning. I have a system I am trying to automate that works like this: user fills a column in an Excel spreadsheet with values they would like printed and saves it to a folder on their desktop, they start the script and it formats their data into a text file (adding a prefix) and sends the text file as a .bch file where it needs to go. This is working:
      #include <Array.au3> #include <Excel.au3> #include <File.au3> #include <MsgBoxConstants.au3> Global $sSTCArray Global $sFilename = @DesktopDir & "\Labels\print.txt" Global $sWorkbook = @DesktopDir & "\Labels\Labels.xlsx" Global $oExcel = _Excel_Open(False,False,False,False,True) barcodePrint() Func barcodePrint() $Read = _Excel_BookOpen($oExcel, $sWorkbook, True, False, Default, Default, Default) FileOpen($sFilename, $FO_OVERWRITE) ;Global $oWorkbook = _Excel_BookAttach($oExcel) Global $sSTCArray = _Excel_RangeRead($Read) For $i = 0 to UBound($sSTCArray, 1) - 1 FileWriteLine($sFilename, "!StaticShelving1x3_ZPL," & $sSTCArray[$i]) Next _Excel_Close($oExcel) FileMove($sFilename, "***file path***\print.bch") EndFunc However, it only works if the user first formats the spreadsheet to text. I want to automate that. From what I have read it appears AutoIt does not like formatting cells that already have values. True? Can I pull the values from an un-formatted (default GENERAL format) spreadsheet and go straight to my text file? I have also considered opening a second spreadsheet, formatting it, and copying the values over. Like this:
        
      Global $sPrefix Global $oPath Global $sSTCArray Global $sFilename = @DesktopDir & "\Labels\print.txt" Global $sWorkbook = @DesktopDir & "\Labels\Labels.xlsx" Global $oExcel = _Excel_Open(False,False,False,False,True) Global $aArray Global $bExcel _Excel_BookNew($bExcel) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 $aArray = IniReadSection("***File path***\barcode.ini", "stc/rvt/mgm") $sPrefix = $aArray[1][1] $oPath = $aArray[2][1] barcodePrint() EndSwitch WEnd Func barcodePrint() $oWorkbook = _Excel_BookOpen($bExcel, @DesktopDir & "\Labels\print.xlsx") $oWorkbook.ActiveSheet.Columns("A").NumberFormat = "@" Local $Read = _Excel_BookOpen($oExcel, $sWorkbook, True, False, Default, Default, Default) $oCopy = _Excel_RangeRead($sWorkbook) _Excel_RangeWrite($oWorkbook, Default, $oCopy) FileOpen($sFilename, $FO_OVERWRITE) $sSTCArray = _Excel_RangeRead($Read,"Default","Default",3) ;_ArrayDisplay($sSTCArray) For $i = 0 to UBound($sSTCArray, 1) - 1 FileWriteLine($sFilename, $sPrefix & $sSTCArray[$i]) Next _Excel_Close($oExcel) ;FileMove($sFilename, $oPath) Exit EndFunc This does not like the formatting of $oWorkbook: "Variable must be of type 'Object'".  Do I need this second sheet? If so, how can I format it? Is there a better way to get the Excel values into a .txt file? Any ideas would be appreciated. Thanks!
×