13类型不匹配错误

时间:2018-04-05 14:50:12

标签: vba excel-vba type-mismatch excel

我试图在工作表之间发送数据。当我运行下面的代码时,我在IncidentReport变量上得到13个不匹配错误。在源表上,这些范围是数字,但一个单元格是日期(因此数字,日期,数字)。我试图将IncidentReport声明为字符串,整数和长,但我一直收到此错误。这个的正确变量是什么?

Private Sub Update_Click()
Dim AgentName As String
Dim IncidentReport As Long
Dim MyData As Workbook

'Names variable
Worksheets("sheet1").Select
    AgentName = Range("C2")
    IncidentReport = Range("D1:F1")
 'Opens master workbook
Set MyData = Workbooks.Open("C:\Users\ashley.graham\Field_AgentFolder\Incident Reports\Test folder\Incident reports 2018.xlsx")

'Calls sheet and selects cell
Worksheets("Incident Reports").Select
    Worksheets("Incident Reports").Range("a1").Select
    'finds next blank row for data entry
    RowCount = Worksheets("Incident Reports").Range ("A1").CurrentRegion.Rows.Count
    With Worksheets("Incident Reports").Range("A1")
        .Offset(RowCount, 0) = IncidentReport
        .Offset(RowCount, 1) = AgentName
    End With
    MyData.Save


End Sub

1 个答案:

答案 0 :(得分:0)

我认为这就是你要做的事情:

Private Sub Update_Click()

    Dim AgentName As String
    Dim IncidentReport As Range
    Dim MyData As Workbook
    Dim LastRow As Long

    With ThisWorkbook.Worksheets("Sheet1")
        AgentName = .Range("C2")
        Set IncidentReport = .Range("D1:F1")
    End With

    Set MyData = Workbooks.Open("C:\Users\ashley.graham\Field_AgentFolder\Incident Reports\Test folder\Incident reports 2018.xlsx")

    With MyData.Worksheets("Incident Reports")
        LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
        .Cells(LastRow, 1) = IncidentReport.Cells(1) 'This will put the first cell value here as don't know what you want.
        .Cells(LastRow, 2) = AgentName
    End With

    MyData.Save

End Sub