我在 Excel 中尝试了以下代码。正在连接成功。我想从用户那里获取输入值。为了从用户那里获取值(value),我使用了 Range.Value("N3")
和 Range.Value("N4")
。
它工作得很好,但没有以这种方式获得值(value)。但是当我在 Excel 工作表上添加值并单击按钮获取记录时,
它给了我错误
"Argument is not optional"
这里有人可以检查一下吗?
这是代码:
Private Sub CommandButton1_Click()
Dim mon As String
Dim yea As String
mon = Range.Value("N3")
yea = Range.Value("N4")
' Create a connection object.
Dim cnPubs As ADODB.Connection
Set cnPubs = New ADODB.Connection
' Provide the connection string.
Dim strConn As String
'Use the SQL Server OLE DB Provider.
strConn = "PROVIDER=SQLOLEDB;"
'Connect to the Pubs database on the local server.
strConn = strConn & "DATA SOURCE=(local);INITIAL CATALOG=inFlow;"
'Use an integrated login.
strConn = strConn & " INTEGRATED SECURITY=sspi;"
'Now open the connection.
cnPubs.Open strConn
' Create a recordset object.
Dim rsPubs As ADODB.Recordset
Set rsPubs = New ADODB.Recordset
With rsPubs
' Assign the Connection object.
.ActiveConnection = cnPubs
' Extract the required records.
.Open "exec dbo.ReportTotalCompanyMonthlySales ' & yea & ',' & mon&'"
' Copy the records into cell A1 on Sheet1.
Sheet1.Range("A1").CopyFromRecordset rsPubs
' Tidy up
.Close
End With
End Sub
最佳答案
试试这个:
mon = Range("N3").Value
而不是
mon = Range.Value("N3")
关于vba - 单击时出现错误 "Argument is not optional",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48860845/