从一张纸到另一张纸的参考误差

时间:2019-09-30 20:19:10

标签: excel vba

test sheet Jayce sheet

我有2张纸:测试 Jayce

test 工作表上,我有一个表,其总计包括一个求和方程。一旦我点击了“提交”按钮,信息就应该在比较所选日期之后将数据复制/粘贴到 Jayce 工作表中,以便可以将值放入正确的单元格中。

我不明白为什么我会不断出现#ref!错误。

Private Sub CommandButton21_Click()
'Sheet1(test)
'Sheet5(Jayce)

Application.ScreenUpdating = False

Dim dateRange As Range
Dim dateSelected As Date: dateSelected = Sheets(2).Range("Q5").Value
Dim ws As Worksheet: Set ws = ActiveWorkbook.Sheets("test")
Dim ws2 As Worksheet: Set ws2 = ActiveWorkbook.Sheets("Jayce")
Dim counterCopy As Range: Set counterCopy = Sheets(2).Range("M2")

For Each dateRange In Sheets(6).Range("B5:B370")
    If dateRange >= dateSelected And dateRange <= dateSelected Then
        counterCopy.Copy Destination:=Sheets(6).Range("D" & dateRange.Row)

        Sheet5.Range("D" & dateRange.Row).Style = "Normal"
        'ws.Range("G2:L19").ClearContents 'clear contents of table
    End If
Next
    MsgBox "done"

Application.ScreenUpdating = True
End Sub

1 个答案:

答案 0 :(得分:0)

您的问题可能出在此If语句上,它正在比较数据类型Range与数据类型Date

If dateRange >= dateSelected And dateRange <= dateSelected Then

我还将检查您在使用索引2和6并分别命名为“ test”和“ Jayce”时如何引用工作表,并尝试保持1种引用类型。