从单元格复制值到文本框中的<Excel-VBA>“types不匹配”错误

我目前正在做一个简单的用户窗体<>工作表数据编辑界面,位于同一工作簿。 我的用户表单button位于工作表A上,而数据库(将从中拉出数据)位于另一工作表中。 我目前正在研究searchfunction(下面包含的整个代码块),并且在以下行中遇到“types不匹配”错误:

MsgBox ws.Range("B" + cRow).Value 

我曾尝试使用CVar()和其他替代方法,但它不能解决问题。

我的目标工作stream程是当用户在“txtCompany”文本框中键入公司名称并单击searchbutton时,它将在“公司名称”(D列)列中search数据库中相似的名称,并返回所有其他值在那行到我的文本框中的用户表单。

如果有人能够启发我什么是造成这个问题,请不吝赐教。 Sub的完整代码如下:

 Private Sub btnSearch_Click() Dim lRow As Long Dim ws As Worksheet Dim srcterm As String Dim datevalue As String Dim cCol, cRow As Integer Set ws = ThisWorkbook.Worksheets("Database") lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row Dim x As Control For Each x In frmSearch.Controls If TypeName(x) = "TextBox" Then x.Value = "" End If Next x srcterm = txtCompany.Value MsgBox lRow For Each cell In ws.Range("D3:D" & lRow) If cell.Value Like srcterm Then cRow = cell.Row MsgBox cRow MsgBox ws.Range("B" + cRow).Value With frmSearch .txtDate.Value = ws.Range("B" + cRow).Value .txtCustomer.Value = ws.Cells("C" + cRow).Value .txtCompany.Value = ws.Cells("D" + cRow).Value .txtAddress.Value = ws.Cells("E" + cRow).Value .txtContact.Value = ws.Cells("F" + cRow).Value .txtEmail.Value = ws.Cells("G" + cRow).Value .txtStatus.Value = ws.Cells("H" + cRow).Value End With datevalue = ws.Cells("A" + cRow).Value End If Next cell End Sub 

"B" + cRow

这不是如何将数字连接到VBA中的string。 你应该使用:

 "B" & cRow ' ^^^ 

好的, +运算符用于连接string,即"a" + "b"但在string和数字上尝试时,它是types不匹配的 。 你可以使用"B" + CStr(cRow)但是我build议你完全放弃在VBA中使用string连接的+运算符,并且坚持用&运算符来处理这个问题。