我是新手在請各位熟手幫我看看哪裡錯了!!vba

jho20186
因公司需求我做了了一張表單,但現都顯示"此處需要物件"可以請各位熟手幫幫忙看一下如何修正嗎?

Dim D As String
Dim T As String
T = Left(ActiveWorkbook.Name, 5) + "檢查\"
If TextBox5.Text = "" And Not (TextBox6.Text = "") Then
If IsDate(TextBox6.Text) And TextBox6.TextLength = 9 Then
Cells(4, "E") = "訂單編號:" & TextBox1.Text
Cells(5, "F") = TextBox2.Text
Cells(4, "H") = TextBox3.Text
Cells(5, "H") = TextBox4.Text
Cells(4, "J") = TextBox5.Text
Cells(5, "J") = TextBox6.Text
MsgBox "資料輸入成功"
E = Left(ActiveWorkbook.Name, 20)
D = E + "_進料檢驗表單_Range(B5)--" + Left(TextBox6.Text, 3) + Mid(TextBox6.Text, 5, 2) + Mid(TextBox6.Text, 8, 2) + "生產(" + TextBox1.Text + "--" + TextBox2.Text + "-" + TextBox4.Text + ")"
ActiveWorkbook.SaveAs Filename:="\\Charmingal\品檢\C\031\3\" + T + D & xlBook.xlSheet.Range(D) & ".xls"
Workbooks(ActiveWorkbook.Name).SaveAs "\\Charmingal\品檢\C\031\3\" + T + D
QC_FORM.Hide
Else
MsgBox "日期錯誤!!"
End If
End If

If TextBox6.Text = "" And Not (TextBox5.Text = "") Then
If IsDate(TextBox5.Text) And TextBox5.TextLength = 9 Then
Cells(4, "E") = "訂單編號:" & TextBox1.Text
Cells(5, "F") = TextBox2.Text
Cells(4, "H") = TextBox3.Text
Cells(5, "H") = TextBox4.Text
Cells(4, "J") = TextBox5.Text
Cells(5, "J") = TextBox6.Text
MsgBox "資料輸入成功"
E = Left(ActiveWorkbook.Name, 20)
D = E + "_進料檢驗表單_RimProfile--" + Left(TextBox6.Text, 3) + Mid(TextBox6.Text, 5, 2) + Mid(TextBox6.Text, 8, 2) + "生產(" + TextBox1.Text + "--" + TextBox2.Text + "-" + TextBox4.Text + ")"
ActiveWorkbook.SaveAs Filename:="\\Charmingal\品檢\C\031\3\" + T + D & xlBook.xlSheet.Range(D) & ".xls"
Workbooks(ActiveWorkbook.Name).SaveAs "\\Charmingal\品檢\C\031\3\" + T + D
QC_FORM.Hide
Else
MsgBox "進貨日期錯誤!!"
End If
End If

If TextBox6.Text = "" And TextBox5.Text = "" Then
Cells(4, "E") = "訂單編號:" & TextBox1.Text
Cells(5, "F") = TextBox2.Text
Cells(4, "H") = TextBox3.Text
Cells(5, "H") = TextBox4.Text
Cells(4, "J") = TextBox5.Text
Cells(5, "J") = TextBox6.Text
MsgBox "資料輸入成功"
E = Left(ActiveWorkbook.Name, 20)

D = E + "_進料檢驗表單_RimProfile--" + Left(TextBox6.Text, 3) + Mid(TextBox6.Text, 5, 2) + Mid(TextBox6.Text, 8, 2) + "生產(" + TextBox1.Text + "--" + TextBox2.Text + "-" + TextBox4.Text + ")"
Workbooks(ActiveWorkbook.Name).SaveAs "\\Charmingal\品檢\C\031\3\" + T + D
QC_FORM.Hide
End If

If Not (TextBox6.Text = "") And Not (TextBox5.Text = "") Then
If IsDate(TextBox5.Text) And TextBox5.TextLength = 9 And IsDate(TextBox6.Text) And TextBox6.TextLength = 9 Then
Cells(4, "E") = "訂單編號:" & TextBox1.Text
Cells(5, "F") = TextBox2.Text
Cells(4, "H") = TextBox3.Text
Cells(5, "H") = TextBox4.Text
Cells(4, "J") = TextBox5.Text
Cells(5, "J") = TextBox6.Text
MsgBox "資料輸入成功"
E = Left(ActiveWorkbook.Name, 20)
D = E + "_進料檢驗表單_Range(B5)--" + Left(TextBox6.Text, 3) + Mid(TextBox6.Text, 5, 2) + Mid(TextBox6.Text, 8, 2) + "生產(" + TextBox1.Text + "--" + TextBox2.Text + "-" + TextBox4.Text + ")"
xlBook.SaveAs Filename:="\\Charmingal\品檢\C\031\3\" + T + D & xlBook.xlSheet.Range("D") & ".xls"
Workbooks(ActiveWorkbook.Name).SaveAs "\\Charmingal\品檢\C\031\3\" + T + D
QC_FORM.Hide
Else
MsgBox "日期錯誤!!"
End If
End If
Allen
應該會告知第幾行錯誤吧?
香帥
Textbox沒指定是哪一個userform的,可能會發生找不到物件,因此產生此處需要物件
尤其是多表單,例如有些表單有textbox3,有些沒有,在沒有textbox3表單內的程式碼
就會發生錯誤
香帥
再補充一下,除了上述多表單外,
在單一表單因您沒用userform1.去顯示各textbox來選擇
容易打錯例如表單只有textbox3,程式碼應該打textbox3.text卻打錯成Textbos3.text或打成textbox33.text
執行時也會產生此處需要物件的錯誤
jho20186
那請問各位如果我要把D = E + "_進料檢驗表單_Range(B5)--" + Left(TextBox6.Text, 3) + Mid(TextBox6.Text, 5, 2) + Mid(TextBox6.Text, 8, 2) + "生產(" + TextBox1.Text + "--" + TextBox2.Text + "-" + TextBox4.Text + ")"顯示成為資料夾的檔名,Range(B5)要如何設定成為我想要的名稱??因為我希望Range(B5)這裡可以隨著我要的檔案名稱做變動??
回到頂部