2

I currently have .vbs files that I double click and run to convert my Excel files to .txt files in the format as output below. I would like to add this as a VBA code snippet so I can create the files right from inside Excel. I have no experience with .VBS so this has proven difficult and I have made no progress.

Does anyone have suggestions or would like to help convert this to VBA?

I appreciate any help.

Thanks,

wscript.echo "Process Journal Entries for upload?"

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("S:\Accounting\JE60.xlsm")
set myfileFSO=CreateObject("Scripting.FileSystemObject")
set myts=myfileFSO.CreateTextFile("S:\Accounting\JE60.txt")
set objWorksheet=objWorkbook.Sheets(1)

introw=1
count=0
Do Until objWorksheet.Cells(intRow, 1).Value = "" 
count=count+1
introw=introw+1
loop

myts.write "G," & objworksheet.cells(6,2).value  & "," & objworksheet.cells(2,2).value & "," & objworksheet.cells(5,2).value & "," &objworksheet.cells(7,2).value & "," &objworksheet.cells(3,2).value & "," &objworksheet.cells(4,2).value & "," & objworksheet.cells(8,2).value & "," & objworksheet.cells(10,2).value & "," & objworksheet.cells(11,2).value & "," & objworksheet.cells(12,2).value & chr(10)    


for i=14 to count

myts.write "D," & objworksheet.cells(i,1).value & "," & objworksheet.cells(9,2).value & "," & objworksheet.cells(i,2).value & "," &objworksheet.cells(5,2).value & "," & objworksheet.cells(i,3).value & "," & objworksheet.cells(i,4).value & "," & objworksheet.cells(i,5).value &"," &objworksheet.cells(i,6).value &"," &objworksheet.cells(i,7).value &"," &objworksheet.cells(i,8).value &"," &objworksheet.cells(8,2).value & chr(10) 

if objworksheet.cells(i,9).value <> "" then
myts.write "A," &objworksheet.cells(i,9).value &"," &objworksheet.cells(i,10).value &"," &objworksheet.cells(i,11).value &"," &objworksheet.cells(i,12).value &"," &objworksheet.cells(i,13).value &"," &objworksheet.cells(i,14).value &"," &objworksheet.cells(i,15).value &"," &objworksheet.cells(i,16).value &"," &objworksheet.cells(i,17).value &"," &objworksheet.cells(i,18).value &"," &objworksheet.cells(i,19).value &"," &objworksheet.cells(i,20).value &"," &objworksheet.cells(i,21).value &"," &objworksheet.cells(i,22).value &"," &objworksheet.cells(i,23).value &"," &objworksheet.cells(i,24).value &"," &objworksheet.cells(i,25).value &"," &objworksheet.cells(i,26).value &"," &objworksheet.cells(i,27).value &"," &objworksheet.cells(i,28).value &"," &objworksheet.cells(i,29).value & chr(10) 

End if

next

myts.close
objExcel.Quit
set objExcel=nothing

' msgbox "Done."
wscript.echo "Done"
0

1 Answer 1

3

It's pretty close to VBA as it is. Put the whole thing in a Sub / End Sub block within a VBA module and debug each error one by one.

The major changes you need to make are:

1) Set objExcel = Excel.Application in place of the line you currently have. You don't really need objExcel at all but retaining it will minimise the amount of code changes you need to make.

2) If you already have the workbook open then write Set objWorkbook = Workbooks("JE60.xlsm")

3) Drop the line objExcel.Quit else you'll keep losing your work!

4) Replace wsript.echo with debug.print.

2
  • That worked perfectly. I ended up changing some of it so that the filename saved to was the workbook name and user selects the directory to save to. Thank you! Jul 4, 2014 at 17:15
  • @user3566159 Plus Dim your variables appropriately for efficiency and error prevention. Also suggest a variant array on place of the range loop - will greatly improve speed.
    – brettdj
    Jul 6, 2014 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.