因此,我創建了此代碼(可能甚至不必顯示它 - 但以防萬一!),無法對其進行測試,因爲我在第一行獲得「用戶定義類型未定義」。我看到了,我應該設置在工具>選項是灰色的,但引用的 參考....Excel宏錯誤 - 「用戶定義類型未定義」 - 引用灰顯
Sub UploadDistributors()
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cnn = New ADODB.Connection
Set rs = New ADODB.Recordset
strConn = "Provider=SQLOLEDB-Removed for security-Network Library=DBMSSOCN"
cnn.Open strConn
Dim k
k = 0
For i = 1 To 352
sSQL = "INSERT INTO DistributorsNP (CompanyName, address, city, state, zip, phone, tollfree) VALUES ("
'CompanyName
sSQL = sSQL & Sheet1.Range("B" & i).Value & ","
'address
sSQL = sSQL & Sheet1.Range("C" & i).Value & ","
'city
sSQL = sSQL & Sheet1.Range("D" & i).Value & ","
'state
sSQL = sSQL & Sheet1.Range("E" & i).Value & ","
'zip
sSQL = sSQL & Sheet1.Range("F" & i).Value & ","
'phone
sSQL = sSQL & Sheet1.Range("G" & i).Value & ","
'tollfree
sSQL = sSQL & Sheet1.Range("H" & i).Value & ","
sSQL = sSQL & ")"
rs.Open sSQL, cnn, adOpenStatic, adLockReadOnly, adCmdText
rs.Close
Next
End Sub
as @MattCrum暗指下面。確保你在VBE中不在調試模式。 (按下菜單欄中的藍色方塊)。然後可以使用「工具>參考」進行編輯。 –