Parece não haver por aí muitos códigos que demonstram como converter o conteúdo de planilhas em arquivos texto. Pelo menos não de forma reutilizável. Espero que este possa ajudar a muitos, especialmente os neófitos.
'Prompts for accepting user inputLet strViewPath = Trim (InputBox ("PLANILHA - Por favor, digite o path do arquivo",,"C:\Bernardes\"))Let strTest = Trim (InputBox ("TEXTO - Por favor, digite o arquivo texo",,"sample"))If Right (strViewPath, 1) <> "\" ThenLet strViewPath = strViewPath & "\"
End IfLet strTestName = strTest
Let strTextFilePath = strViewPath
'Assign the values for the excel and text file that needs to be convertedLet TestToConvert = strViewPath + strTest + ".xls"
Let TextFile =strTextFilePath + strTestName + ".txt"
'Create the excel objectSet oExcel = CreateObject("Excel.Application")Let oExcel.Visible = False
'Open the excel file for conversionLet oExcel.DisplayAlerts = False
oExcel.Workbooks.Open TestToConvert, True'Call the text streamer function that will convert the fileTextStreamer TextFile, oExcel'Exit the Excel fileoExcel.QuitPrivate Sub TextStreamer(TextFileName, objExcel)'Declare constants for reading,writing and appending to a text fileConst ForReading = 1, ForWriting = 2, ForAppending = 3Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0Dim fs, f, ts, x, y, LastRow, LastColumn, c, objSheet, shts()'Create the file system object for text file editingSet fs = CreateObject("Scripting.FileSystemObject")fs.CreateTextFile TextFileNameSet f = fs.GetFile(TextFileName)Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault)'Get the number of worksheets in the source excel fileLet intNoOfSheets = objExcel.Worksheets.count
Let z = intNoOfSheets
'Traverse through every sheet that needs to be convertedFor i = 1 to intNoOfSheets'Activate the first worksheetobjExcel.Worksheets(z).ActivateobjExcel.Worksheets(z).SelectSet objSheet = objExcel.ActiveWorkbook.Worksheets(z)Let strSheetName = objsheet.nameobjSheet.Cells(1).SelectLet LastRow = objSheet.UsedRange.Rows.Count + 2Let LastColumn = objSheet.UsedRange.Columns.CountobjSheet.Cells(1).Selectts.write "["&strSheetName&"]"ts.write Chr(13) & Chr(10)'Loop through the rows and columns in the excel worksheet and write the data to the text fileFor x = 0 To LastRowFor y = 0 To LastColumn -1If objExcel.ActiveCell.Offset(x, y).Value <> "" thents.write (objExcel.ActiveCell.Offset(x, y).Value)'ts.write Chr(9)End IfNextts.write Chr(13) & Chr(10)NextLet z= z-1Next'Close the excel file test streamerts.Closemsgbox "Conversion Complete!"End Sub
Reference:
Aditya Kalra
Inspiration:
Tags: VBA, Excel, convert, text, to text, planilha, sheet, worksheet,
Nenhum comentário:
Postar um comentário