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 (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
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 (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
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 (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
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 (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
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 (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
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 (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
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 (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
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 (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
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 (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
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

    • Skeletor
      By Skeletor
      Hi Guys,
      How would you use _Excel_FilterSet to filter the excel sheet from largest number to smallest number?
      _Excel_FilterSet($oWorkbook, "Sheet1", "A2:I2", 1, ">20", 1, "<40") ; OR _Excel_FilterSet($oWorkbook, "Sheet1", "A2:I2", 1, ">", 1, "<") Excel Sheet attached. Trying to filter on the last column "I2"
      Inventory.xlsx
    • Skeletor
      By Skeletor
      Hi All,
      Has anyone come across this before?
      Code below:
      $SheetList = _Excel_SheetList($oWorkbook) _FileWriteFromArray("C:\" & $SheetListOutput, $SheetList, 1) Result:
      ResultABC| ResultDEF| ResultGHI| ResultJKL| ResultMNO| It created these "|" vertical bars?
    • Skeletor
      By Skeletor
      Hi All,

      I would like to know how you would take a FileLineRead and insert it into an array which then inserts it into Excel?
      One thing to know is the files content is broken up, so I only use half of the content within $FileRead1.
      So its imperative that the $value1, $value2, etc variables be used. 
      Code below:
      $FileRead1 = FileReadLine("C:\temp\sample.txt",1) For $count = 1 To _FileCountLines($FileRead1) Step 1 $string = FileReadLine($FileRead1, $count) $input = StringSplit($string, ",", 1) $value1 = $input[1] $value2 = $input[2] $value3 = $input[3] $value4 = $input[4] _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "A1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "B1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value4, "D1") Next  
    • AnonymousX
      By AnonymousX
      Hello,
      I'm trying to write a script that moves copies excel cells into an array. I'll than manipulate the values and send array into another program. 
      I don't want range to be specific to a workbook, or sheet, or set of cells.
      I want user to be able to highlight desired cells and to copy either normally ("Ctrl+C") or by a hotkey ("Alt+C"). 
      Could someone help me with this?
      Thank you,
      I've tried to write the framework: (edited)
      #include <MsgBoxConstants.au3> #include <Array.au3> #include <Excel.au3> HotKeySet("!v", "Pastedata") While True Sleep(1000) WEnd func Makearray() local $bArray ;User has cells already copied ;Convert clipboard into an array ;I don;t know how excel stores data to clipboard so don;t know how to bring it into array _Arraydisplay($bArray) MsgBox(0,0,$bArray) return $bArray endfunc func Pastedata() Local $aArray MsgBox(0,0,"wait",1) ;make array based on assumption user has already copied a range to clipboard $aArray = Makearray() ;paste code ;don;t worry about this I got the rest endfunc  
    • nooneclose
      By nooneclose
      Hey. I'm working on a new project and was wondering if there is a better way to "update" my Column E array. 
      Here is my code: 
      Local $nI  = 0                                                            ;Creates a name index of 0: nI = Name index Local $nII = 1                                                            ;Creates a name index of 1 for second loop: nII = Name Index 2 For    $iN = 0 To $IndexRows Step 1                                       ;Checks the roster for any names that appear twice      For $iN2 = 0 To $IndexRows Step 1          if $d_Names[$nI] == $d_Names[$nII] And $d_Names[$nII] <> "" Then              Local $timeSheetName = _ArraySearch($e_Names, $d_Names[$nI], 0, 0, 0, 0, 1)              ;MsgBox($MB_SYSTEMMODAL, "Found it", $d_Names[$nI] & " In column E on Row " & $timeSheetName)              Local $eI  = $timeSheetName + 1              ;ConsoleWrite($timeSheetName & @CRLF)              ;ConsoleWrite($eI & @CRLF)              ;ConsoleWrite(@CRLF)              _Excel_RangeInsert($OpenWorkbook.ActiveSheet, "E" & $eI & ":F" & $eI, $xlShiftDown)                                                                          ;Inserts a empty cell in columns E and F.              _Excel_RangeWrite($OpenWorkbook, $OpenWorkbook.ActiveSheet, $d_Names[$nII], "E" & $eI)                                                                         ;Fills the empty cell in columns E with the doubled name              $aArray_Index = 2                                           ;Array element counter              For $Index = 2 To $IndexRows Step 1                        ;Loops through every row in the Excel file unto no rows are found or a null row is found                  $Array_Value_E = _Excel_RangeRead($OpenWorkbook, Default, "E"&$Index)                  $e_names[$aArray_Index] = $Array_Value_E                ;While the code loops every value in column E is stored in the E array (updating the array)                  $aArray_Index += 1              Next              ExitLoop          EndIf      Next      $nI  += 1      $nII += 1 Next Basically, It checks a roster for people whose name appears twice then inserts a new "row" for that person because they work in two different departments.
      I have to find that name however in Column E if two appear in column D. My code works but I think it is not as efficient as it could be. 
      Any ideas on how to improve the "update" for my array?
      (once it finds the double names in Column D it then searches for that name by going name by name in the Column E array and once it finds it inserts a new row. However, the E array doesn't have that new row stored in it so I have to "update" the array to properly find the next name)
      Any and all tips would be greatly appreciated. 
       
      NOTE: Just assume I'm opening the excel file properly please do not add that code in, it only complicates your answer. 
×