mardi 31 mars 2015

VBA: Opening and reading a CSV file without sheet name

I'm trying to write VBA Codes that will read a given CSV file name and do some changes to the data before writing into another CSV file.



Set oSht_Input = Workbooks.Open(file_Path, UpdateLinks:=False).Worksheets(extractDataFile.convertedSheetName)
lastRow = oSht_Input.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

For Rows = 2 To lastRow
extractedDataFileRecord.Variable1 = CStr(oSht_Input.Cells(Rows, 1))

.... code continues


This code is working fine generally.


However, the flaw is that CSV files will just copy the file name and place into the sheet name. Therefore I can safely assume that the variable convertedSheetName is equivalent to the filename provided.


This code will work if the file name is short enough to be copied into the sheet name. However if the file name is too long, the filename gets cut off within the sheet name, thus I am not able to get the correct reference to set the Worksheet object.


Is there a better way to actually get the sheet reference, so that I can read the Cell Values within the sheet?


Thanks!


Aucun commentaire:

Enregistrer un commentaire