|
Our accounting system exports data into Excel as a text file. Is there a way to be able to run a macro out of my Personal Macro Workbook without saving and reopening the data as an .xlsx file?
Hi, my data is separated by| can you plz guide me
……So here is a very basic code.
<font face=Calibri><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><SPAN style="color:#00007F">Sub</SPAN> BasicMacroTextFileToExcel()<br><SPAN style="color:#007F00">' Very Simply alternative to the Spreadsheet way using Wizzard, Opening as text File in Excel etc</SPAN><br><SPAN style="color:#007F00">'</SPAN><br><SPAN style="color:#007F00">'First Bit can be ommited if fielnames are always known. It basically gets all the File info.</SPAN><br><SPAN style="color:#007F00">'Note text File And Excel File must not be in the same Directory as you are being asked for it here.</SPAN><br><SPAN style="color:#00007F">On</SPAN> Err <SPAN style="color:#00007F">GoTo</SPAN> TheEnd <SPAN style="color:#007F00">' if anything goes wrong go to the end rather than crasching!</SPAN><br><SPAN style="color:#007F00">'</SPAN><br><SPAN style="color:#007F00">'First Bit: TextDataFileName and seperator input</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> Sp <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> <SPAN style="color:#007F00">' Allocate memory for The seperater used by the Text file</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> DtaFleNm <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN> <SPAN style="color:#007F00">'The DataFilename itself is a string. But a vasriant is specified as you may have a Boolean input in the Dialogue boy if the user cancels</SPAN><br> DtaFleNm = Application.GetOpenFilename(FileFilter:="Hallo,*.txt,") <SPAN style="color:#007F00">'The Application Method GetOpenFilename Displays the standard <SPAN style="color:#00007F">Open</SPAN> dialog box. The optional arguments:= writes a name,limits type of files looked for</SPAN><br> <SPAN style="color:#00007F">If</SPAN> DtaFleNm = <SPAN style="color:#00007F">False</SPAN> <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN> <SPAN style="color:#007F00">'Stops Porgram if User Cancels (which gives the boolean False)</SPAN><br> <SPAN style="color:#00007F">Let</SPAN> Sp = Application.InputBox("Enter a separator character.", Type:=2) <SPAN style="color:#007F00">'prompts user for separator character</SPAN><br> <SPAN style="color:#00007F">If</SPAN> Sp = vbNullString <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN> <SPAN style="color:#007F00">'Stops Porgram if User Cancels (which gives an empty string)</SPAN><br><SPAN style="color:#007F00">'End First Bit TextDataFileName and seperator input</SPAN><br><SPAN style="color:#007F00">'</SPAN><br><SPAN style="color:#007F00">' 'If you want to skip the first bit, delete it or comment it out and type in the text File path and seperator in lines like this:</SPAN><br><SPAN style="color:#007F00">' Dim DtaFleNm As String</SPAN><br><SPAN style="color:#007F00">' Let DtaFleNm = ThisWorkbook.Path & "\TextDataFile.txt"</SPAN><br><SPAN style="color:#007F00">' Dim Sp As String</SPAN><br><SPAN style="color:#007F00">' Let Sp = "|"</SPAN><br><SPAN style="color:#007F00">' 'Note: If you do it this way you should put the Active Excel File and Data Text File in the same Directory</SPAN><br><SPAN style="color:#007F00">'</SPAN><br><SPAN style="color:#007F00">'Start Main Bit: Get data from text file line by line, then for each line go through every Column. As each data bit is found it is put in the approriate place in the Excel File</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> Rw <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>, StCm <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>, Cm <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN> <SPAN style="color:#007F00">'Before you start You should select cell</SPAN><br><SPAN style="color:#00007F">Let</SPAN> StCm = ActiveCell.Column <SPAN style="color:#007F00">'where you want to start data to come in. Column is then reset in loop</SPAN><br><SPAN style="color:#00007F">Let</SPAN> Rw = ActiveCell.Row <SPAN style="color:#007F00">'Row increases downwards so the initial and loop caount can be same variable</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> DtaFleLn <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> TxtPs <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN> <SPAN style="color:#007F00">'text character position in text file looking form left</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> SpPs <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN> <SPAN style="color:#007F00">'position of seperater lookinf from left</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> TxtDta <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> <SPAN style="color:#007F00">'String used temporarily for each Text data value</SPAN><br> Open DtaFleNm <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Input</SPAN> <SPAN style="color:#00007F">As</SPAN> 3 <SPAN style="color:#007F00">' Sort of opens up a data highway and gives it route number (here 3) to distinguisch it from any others currently open. (If you do not know how many Highways are already open and have a number, then use instead of a number someting like #HgWyNm here, and HgWyNm everyhwere where the number is. This gives it the next available number (Remember to Dim it at the start also.))</SPAN><br> <SPAN style="color:#00007F">Do</SPAN> <SPAN style="color:#00007F">Until</SPAN> EOF(3) <SPAN style="color:#007F00">'Do until going your at the end of the data highway</SPAN><br> Line <SPAN style="color:#00007F">Input</SPAN> #3, DtaFleLn <SPAN style="color:#007F00">'Bring in a line from text file(Automatically goes to next line or next part of highway after this command. So we need mo row count for the data file.)</SPAN><br> <SPAN style="color:#00007F">If</SPAN> Right(DtaFleLn, 1) <> Sp <SPAN style="color:#00007F">Then</SPAN> DtaFleLn = DtaFleLn & Sp <SPAN style="color:#007F00">'Glue a seperator on the end of the data file line if there is not one there</SPAN><br> <SPAN style="color:#00007F">Let</SPAN> Cm = StCm <SPAN style="color:#007F00">'go to first column</SPAN><br> <SPAN style="color:#00007F">Let</SPAN> TxtPs = 1 <SPAN style="color:#007F00">' Initial Data Text Position is at 1 (Ist Left poisition in text file line</SPAN><br> <SPAN style="color:#00007F">Let</SPAN> SpPs = InStr(TxtPs, DtaFleLn, Sp) <SPAN style="color:#007F00">'Find position of next seperator</SPAN><br> <SPAN style="color:#00007F">While</SPAN> SpPs >= 1 <SPAN style="color:#007F00">'As long as a next Seperator position is found, we go througth columns looking for data</SPAN><br> <SPAN style="color:#00007F">Let</SPAN> TxtDta = Mid(DtaFleLn, TxtPs, SpPs - TxtPs) <SPAN style="color:#007F00">'this gives the next data value</SPAN><br> <SPAN style="color:#00007F">Let</SPAN> ActiveSheet.Cells(Rw, Cm).NumberFormat = "@" <SPAN style="color:#007F00">'This is not allways necerssary but can help avoid annoying problems with data turning into times or dates</SPAN><br> <SPAN style="color:#00007F">Let</SPAN> ActiveSheet.Cells(Rw, Cm).Value = TxtDta <SPAN style="color:#007F00">'Put Text data in excel cell</SPAN><br> <SPAN style="color:#00007F">Let</SPAN> ActiveSheet.Cells(Rw, Cm).Value = Trim(ActiveSheet.Cells(Rw, Cm).Value) <SPAN style="color:#007F00">'Most not necerssary but can help to clean up data if any extra spaces came in</SPAN><br> <SPAN style="color:#00007F">Let</SPAN> TxtPs = SpPs + 1 <SPAN style="color:#007F00">'Start of next data is just after next seperator</SPAN><br> <SPAN style="color:#00007F">Let</SPAN> Cm = Cm + 1 <SPAN style="color:#007F00">'Increase Excel Column number</SPAN><br> <SPAN style="color:#00007F">Let</SPAN> SpPs = InStr(TxtPs, DtaFleLn, Sp) <SPAN style="color:#007F00">'Get the next Seperator position by starting at the current data and going right until the next seperator is found</SPAN><br> <SPAN style="color:#00007F">Wend</SPAN> <SPAN style="color:#007F00">'go to next text data to the right in the data file Line</SPAN><br> <SPAN style="color:#00007F">Let</SPAN> Rw = Rw + 1 <SPAN style="color:#007F00">'Increase row number for Excel(The tecxt file automatically goes to the next row after reading a line</SPAN><br> <SPAN style="color:#00007F">Loop</SPAN> <SPAN style="color:#007F00">'Go to the next Row in the Data Text file</SPAN><br>TheEnd:<br> <SPAN style="color:#00007F">Close</SPAN> 3 <SPAN style="color:#007F00">'Very important to do this. Shuts highway Off. There can be strange errors if you do not do that. (It is here so that even after an error it will be done</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN> <SPAN style="color:#007F00">'BasicMacroTextFileToExcel()</SPAN></FONT>
……And here again simplified a bit.
Code:
Sub SimplifiedBasicMacroTextToExcel()
On Err GoTo TheEnd
DtaFleNm = Application.GetOpenFilename(FileFilter:="Hallo,*.txt,")
If DtaFleNm = False Then Exit Sub
Sp = Application.InputBox("Enter a separator character.", Type:=2)
If Sp = vbNullString Then Exit Sub
StCm = ActiveCell.Column
Rw = ActiveCell.Row
Open DtaFleNm For Input As 3
Do Until EOF(3)
Line Input #3, DtaFleLn
If Right(DtaFleLn, 1) <> Sp Then DtaFleLn = DtaFleLn & Sp
Cm = StCm
TxtPs = 1
SpPs = InStr(TxtPs, DtaFleLn, Sp)
While SpPs >= 1
TxtDta = Mid(DtaFleLn, TxtPs, SpPs - TxtPs)
ActiveSheet.Cells(Rw, Cm).Value = TxtDta
TxtPs = SpPs + 1
Cm = Cm + 1
SpPs = InStr(TxtPs, DtaFleLn, Sp)
Wend
Rw = Rw + 1
Loop
TheEnd:
Close 3
End Sub
It calls up a Text file that looks a bit like this.
[TABLE="width: 500"]
<tbody>[TR]
[TD]1[/TD]
[TD]|[/TD]
[TD]2 [/TD]
[TD]|[/TD]
[TD] 3[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]|[/TD]
[TD] 5[/TD]
[TD]|[/TD]
[TD] 6[/TD]
[/TR]
</tbody>[/TABLE]
Then using that text file, it applies it to an Excel File that initially looks like this (With Cell “B2” arbitrarily selected)
And after running the macro the Excel File looks like this:
. Here is the Excel and the text File
FileSnack | Easy file sharing
FileSnack | Easy file sharing
. If that is anything like you are interested in, and then if you have any questions, then come back. Someone should help.
. Alan Elston