iT邦幫忙

0

Excel VBA問題

  • 分享至 

  • xImage

大家好,我使用EXCEL寫了一個可以亂數出題的檔案,學員點選交卷後檔案會自動另存到一個指定路徑,使用我自己的電腦測試一切都正常,但寄給同事測試交卷時回出現錯誤訊息「執行階段錯誤'1004' 'Save As'方法('Workbook'物件)失敗,請問該怎麼排除?
https://ithelp.ithome.com.tw/upload/images/20220303/201470547l3RJ20IkN.jpg
https://ithelp.ithome.com.tw/upload/images/20220303/20147054nzL1Gr7NON.jpg``Private Sub CommandButton1_Click()
Dim i As Integer
Dim x As Integer
Dim y, z As Integer

Response = MsgBox("考券送出後無法再進行變更,是否確定交卷?", vbYesNo + vbQuestion)
If Response = vbYes Then
'送出答案回寫sheet2
sheet2.Cells(2, 5) = TextBox11.Value
sheet2.Cells(3, 5) = TextBox12.Value
sheet2.Cells(4, 5) = TextBox13.Value
sheet2.Cells(5, 5) = TextBox14.Value
sheet2.Cells(6, 5) = TextBox15.Value
sheet2.Cells(7, 5) = TextBox16.Value
sheet2.Cells(8, 5) = TextBox17.Value
sheet2.Cells(9, 5) = TextBox18.Value
sheet2.Cells(10, 5) = TextBox19.Value
sheet2.Cells(11, 5) = TextBox20.Value

y = sheet3.Range("H2") '扣分
z = 100

'比對答案
x = 2
For i = 21 To 30
If sheet2.Cells(x, 5) <> sheet2.Cells(x, 4) Then
    Me("TextBox" & i).Value = sheet2.Cells(x, 4)
    Me("TextBox" & i).BackColor = &HC0C0FF
    sheet2.Cells(x, 6) = sheet2.Cells(x, 2)
    z = z - y
Else
    Me("TextBox" & i).Value = sheet2.Cells(x, 4)
End If
x = x + 1
Next

TextBox31.Value = z
sheet2.Range("I2") = z

ThisWorkbook.Save
TmpN1 = sheet2.Range("H2")
ActiveWorkbook.SaveAs Filename:="\Tsbnas2\檢測檔案" & TmpN1 & ".xls", FileFormat _
:=xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:= _
False, CreateBackup:=False

CommandButton1.Enabled = False
End If

End Sub

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
海綿寶寶
iT邦大神 1 級 ‧ 2022-03-03 12:07:44
最佳解答

猜測是「路徑」原因
建議逐步執行
在出錯時在除錯視窗下以下指令看看路徑是否合法

? "\Tsbnas2\檢測檔案" & TmpN1 & ".xls"

詳細除錯理論及示範請看這篇

尼克 iT邦大師 1 級 ‧ 2022-03-03 16:09:27 檢舉

VBA大師

我要發表回答

立即登入回答