antmar904 Posted July 19, 2018 Share Posted July 19, 2018 Hi I'm trying to read only column A in Excel but I am unable to. Also, how would I skip reading cell A:1 which is the column name? $Data = _Excel_RangeRead($Book, Default, "A:A") <-- This is not working "A:A" If @error Then MsgBox(0, "ERROR: Reading Cells", "ERROR: " & @error & " Extended: " & @extended) EndIf Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted July 19, 2018 Moderators Share Posted July 19, 2018 (edited) @antmar904 it is not working because you do not have it coded correctly. Look at the help file under _Excel_RangeRead, specifically Example script #3 to see what you are doing wrong. Edit: and as far as removing the first cell, I personally would just read the entire range in and then delete that index from the array (or skip over it in any loops), much easier. Edited July 19, 2018 by JLogan3o13 "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum! Link to comment Share on other sites More sharing options...
water Posted July 19, 2018 Share Posted July 19, 2018 I can't test at the moment but I think "A:A" should be a valid range. What's the value of @error after calling _Excel_RangeRead? My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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 More sharing options...
Moderators JLogan3o13 Posted July 19, 2018 Moderators Share Posted July 19, 2018 I have always used $oWorkbook.ActiveSheet.Usedrange.Columns("A:A") rather than just "A:A", otherwise it tries to pull every cell in the column, used or not. "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum! Link to comment Share on other sites More sharing options...
antmar904 Posted July 19, 2018 Author Share Posted July 19, 2018 18 minutes ago, JLogan3o13 said: I have always used $oWorkbook.ActiveSheet.Usedrange.Columns("A:A") rather than just "A:A", otherwise it tries to pull every cell in the column, used or not. @JLogan3o13 Yes this is what I am seeing. Link to comment Share on other sites More sharing options...
antmar904 Posted July 19, 2018 Author Share Posted July 19, 2018 I ended up using a txt file for right now just to get the job done and will rewrite it using the excel file a little later and post. Link to comment Share on other sites More sharing options...
water Posted July 19, 2018 Share Posted July 19, 2018 Using the UsedRange approach as posted by @JLogan3o13 is by far the best solution My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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 More sharing options...
ternal Posted July 21, 2018 Share Posted July 21, 2018 correct use of excel would be the best solution as said before however I had a similar issue with trying to read columns of a big list (50k+). sometimes it would run perfectly on xlsx files sometimes it would cause an error on same files. Excel functions like search are a lot slower as well for big files to read into an array I ended up changing them from xlsx to csv and then used _FileReadToArray. If you still would like to use txt files this function will convert all xlsx files to csv in a directory. There might be an UDF for this however I did not find any. #include <Excel.au3> #include <File.au3> File_Excel2CSV("C:\user") Func File_Excel2CSV(Const $FilePath) Local $o_Excel = _Excel_Open(False); opens an excel without making it visible If @error then MsgBox(0, "Error opening excel in Func File_Excel2CSV", "Error : " & @error & @CRLF & "Extended : "& @extended ) Local $a_Filelist = _FileListToArray($FilePath, "*", $FLTA_FILES, True); get all files in the directory provided by $filepath Local $a_xlsxlist[UBound($a_Filelist , 1)] Local $iCount = 0 If @error = 1 Then MsgBox(0, 0, "Path was invalid.") If @error = 4 Then MsgBox(0, 0, "No file(s) were found.") For $i = 1 to UBound($a_Filelist, 1) - 1 ; find all .xlsx files Local $a_Split_Line = StringSplit($a_Filelist[$i],".", $STR_NOCOUNT) If $a_Split_Line[1] == "xlsx" Then $a_xlsxlist[$iCount] = $a_Filelist[$i] $iCount +=1 EndIf Next ReDim $a_xlsxlist[$iCount] ;get rid of free space in the list with all xlsx files For $i = 0 to UBound ($a_xlsxlist, 1) - 1 Local $t_CSVName = StringSplit($a_xlsxlist[$i], ".", $STR_NOCOUNT) ; get the name of the file Local $o_WBtemp = _Excel_BookOpen($o_Excel, $a_xlsxlist[$i], False, False) ; open the xlsx file _Excel_BookSaveAs($o_WBtemp, $t_CSVName[0] & ".csv", $xlCSVMSDOS, True) ;save as csv If @error then MsgBox(0, "Error saving excel in Func File_Excel2CSV","error : " & @error & @CRLF & "Extended : " & @extended & @CRLF & "Filepath : " & $t_CSVName[0] & ".csv") _Excel_BookClose($o_WBtemp, False); close the file If @error then MsgBox(0, "error", "failed to close book in Func File_Excel2CSV" & @CRLF & "error : " & @error & @CRLF & "Extended : " & @extended) Next _Excel_Close($o_Excel, False) If @error then MsgBox(0, "error", "failed to close excel in Func File_Excel2CSV" & @CRLF & "error : " & @error & @CRLF & "Extended : " & @extended) EndFunc Link to comment Share on other sites More sharing options...
antmar904 Posted July 23, 2018 Author Share Posted July 23, 2018 On 7/21/2018 at 4:55 AM, ternal said: correct use of excel would be the best solution as said before however I had a similar issue with trying to read columns of a big list (50k+). sometimes it would run perfectly on xlsx files sometimes it would cause an error on same files. Excel functions like search are a lot slower as well for big files to read into an array I ended up changing them from xlsx to csv and then used _FileReadToArray. If you still would like to use txt files this function will convert all xlsx files to csv in a directory. There might be an UDF for this however I did not find any. #include <Excel.au3> #include <File.au3> File_Excel2CSV("C:\user") Func File_Excel2CSV(Const $FilePath) Local $o_Excel = _Excel_Open(False); opens an excel without making it visible If @error then MsgBox(0, "Error opening excel in Func File_Excel2CSV", "Error : " & @error & @CRLF & "Extended : "& @extended ) Local $a_Filelist = _FileListToArray($FilePath, "*", $FLTA_FILES, True); get all files in the directory provided by $filepath Local $a_xlsxlist[UBound($a_Filelist , 1)] Local $iCount = 0 If @error = 1 Then MsgBox(0, 0, "Path was invalid.") If @error = 4 Then MsgBox(0, 0, "No file(s) were found.") For $i = 1 to UBound($a_Filelist, 1) - 1 ; find all .xlsx files Local $a_Split_Line = StringSplit($a_Filelist[$i],".", $STR_NOCOUNT) If $a_Split_Line[1] == "xlsx" Then $a_xlsxlist[$iCount] = $a_Filelist[$i] $iCount +=1 EndIf Next ReDim $a_xlsxlist[$iCount] ;get rid of free space in the list with all xlsx files For $i = 0 to UBound ($a_xlsxlist, 1) - 1 Local $t_CSVName = StringSplit($a_xlsxlist[$i], ".", $STR_NOCOUNT) ; get the name of the file Local $o_WBtemp = _Excel_BookOpen($o_Excel, $a_xlsxlist[$i], False, False) ; open the xlsx file _Excel_BookSaveAs($o_WBtemp, $t_CSVName[0] & ".csv", $xlCSVMSDOS, True) ;save as csv If @error then MsgBox(0, "Error saving excel in Func File_Excel2CSV","error : " & @error & @CRLF & "Extended : " & @extended & @CRLF & "Filepath : " & $t_CSVName[0] & ".csv") _Excel_BookClose($o_WBtemp, False); close the file If @error then MsgBox(0, "error", "failed to close book in Func File_Excel2CSV" & @CRLF & "error : " & @error & @CRLF & "Extended : " & @extended) Next _Excel_Close($o_Excel, False) If @error then MsgBox(0, "error", "failed to close excel in Func File_Excel2CSV" & @CRLF & "error : " & @error & @CRLF & "Extended : " & @extended) EndFunc Looks good, thank you! Link to comment Share on other sites More sharing options...
water Posted July 23, 2018 Share Posted July 23, 2018 BTW: Do not use the "Quote" button when you intend to reply to an answer. We all know what has been posted before. Post your reply at the end of the thread in the "Reply to this thread ..." field. Unnecessary quoting just clutters the thread FrancescoDiMuro 1 My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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 More sharing options...
antmar904 Posted July 23, 2018 Author Share Posted July 23, 2018 Roger Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now