首页 > 解决方案 > Excel VBA:根据日期(单元格值)删除行并附加到另一个工作表

问题描述

我的脚本正在运行,但我仍然在 CDate(S) > 日期(类型不匹配错误 13)这一行上遇到错误。

我认为日期格式正确,但显然不是。

Private Sub CommandButton21_Click()

'ActiveSheet.Unprotect "password"  'Change password accordingly.

    Dim x As Long
    Dim iCol As Integer
    Dim MaxRowList As Long
    Dim S As String

    Set wsSource = Worksheets("Sheet1")
    Set wsTarget = Worksheets("Sheet2")

    iCol = 1
    MaxRowList = wsSource.Cells(Rows.Count, iCol).End(xlUp).Row

    For x = MaxRowList To 1 Step -1
        S = wsSource.Cells(x, 16)
        If CDate(S) < Date Then
            AfterLastTarget = wsTarget.Cells(Rows.Count, 1).End(xlUp).Row + 1
            wsSource.Rows(x).Copy
            wsTarget.Rows(AfterLastTarget).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
            wsSource.Rows(x).Delete
        End If
    Next

   Application.ScreenUpdating = True

    'ActiveSheet.Protect "password"  'Change password accordingly.

MsgBox ("Update complete")

End Sub

标签: excelvba

解决方案


我认为你需要改变Dim S As Date

祝你好运


推荐阅读