2014-01-13 25 views
0

我已經得到了以下的VBA代碼VBA代碼編寫工作,以製表符分隔的文件失敗

sub aemaketxtfile(className As String, rosterFileHandle As String) 
Dim i As Long 
Dim j As Long 
Dim cellContent As String 
Dim gradebookContent As String 
With Worksheets(className).UsedRange 
    gradebookContent = " % seating" & vbCrLf 
    For i = 1 To .Rows.Count 
     For j = 1 To .Columns.Count 
     cellContent = "a" 
     cellContent = .Cells(i, j).Value 
     gradebookContent = gradebookContent & cellContent & vbTab 
     ' gradebookContent = gradebookContent & vbCrLf & Join$(Application.Transpose(Application.Transpose(.Rows(i).Value)), vbTab) 
    Next 
     gradebookContent = gradebookContent & vbCrLf 
    Next 
    End With 

Open Replace(ThisWorkbook.FullName, "seating_chart.xlsm", rosterFileHandle) For Output As #1 
'Open rosterFileHandle For Output As #1 
    Print #1, Mid$(gradebookContent, Len(vbCrLf) + 1) 
Close #1 
End Sub 

Sub makeALLtxt() 
Call aemaketxtfile("all students", "rosterAllStudents.txt") 
End Sub 

每次我運行它,我得到一個錯誤:「運行時錯誤‘52’:錯誤的文件名或數

我用在另一個Excel文件的代碼沒有任何問題。我不明白爲什麼它不會在這裏工作。

如果我註釋掉第一Open Replace ....,並取消對第二,我可以得到的東西工作,但我不滿意,因爲有些時候Excel似乎是將文件保存到隨機目錄。

+0

的名字你檢查路徑的結果呢? – sam092

+0

嘗試用'ThisWorkbook.Path&「\」&rosterFileHandle'替換Replace(ThisWorkbook.FullName,「seating_chart.xlsm」,rosterFileHandle)'。我認爲這會給你找到的結果而不依賴於ThisWorkbook的名字。 –

+0

@TonyDallimore如果你發表你的評論作爲答案,我會接受。 –

回答

1

嘗試更換

Replace(ThisWorkbook.FullName, "seating_chart.xlsm", rosterFileHandle) 

ThisWorkbook.Path & "\" & rosterFileHandle 

我覺得這給了你尋找不依靠的ThisWorkbook

相關問題