2014-07-03 206 views
2

我目前有.vbs文件,我雙擊並運行,將我的Excel文件轉換爲.txt文件,格式如下所示。我想將此添加爲VBA代碼片段,以便我可以從Excel內部創建文件。我沒有使用.VBS的經驗,所以這證明很困難,我沒有取得任何進展。將VBS腳本轉換爲Excel VBA

有沒有人有建議或想幫助將其轉換爲VBA?

我很感激任何幫助。

感謝,

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" 

回答

3

這是非常接近VBA,因爲它是。把所有東西放在VBA模塊中的Sub/End Sub塊中,並逐個調試每個錯誤。

你需要做的主要變化是:

1)Set objExcel = Excel.Application在地方,你目前擁有的生產線。你根本不需要objExcel,但是保留它會最大限度地減少你需要做的代碼更改。

2)如果你已經打開工作簿,然後寫Set objWorkbook = Workbooks("JE60.xlsm")

3)刪除該行objExcel.Quit否則你會不斷失去你的工作!

4)將wsript.echo替換爲debug.print

+0

完美運作。我最終改變了它的一部分,以便保存的文件名是工作簿名稱,用戶選擇要保存的目錄。謝謝! – user3566159

+0

@ user3566159 Plus爲了提高效率和防止錯誤,適當調整變量的值。另外建議一個變量陣列放置範圍循環 - 將大大提高速度。 – brettdj