点击我收到错误"参数不是可选的"

时间:2018-02-19 06:49:32

标签: excel vba excel-vba

我在Excel中尝试了以下代码。它连接成功。我想从用户那里获取输入值。为了从用户那里获得价值,我使用了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 和{{1}}。

在没有以这种方式获得价值的情况下工作正常。但是当我在excel表上添加值并单击按钮来获取记录时,

它给了我错误

  

"参数不是可选的"

如果有人可以查看此内容吗?

以下是代码:

{{1}}

1 个答案:

答案 0 :(得分:2)

试试这个:

mon = Range("N3").Value

而不是

mon = Range.Value("N3")