根据Sheet2中的日期清除Sheet1中的内容

时间:2017-10-14 19:43:54

标签: vba

简短的问题。我有一个excel文件有两张(Sheet1,Sheet2)。在sheet1列A上我有一个日期列表,在列D上我有一些数字。在Sheet2 A16上,我有一个约会。

我希望能够从Sheet2单元格A16中找到的日期开始删除Sheet1列D上的数字。 Sheet2!A16中的日期将每天更改,因此我将删除从不同日期开始的数字...

第1张图片 enter image description here


第2张图片
enter image description here

1 个答案:

答案 0 :(得分:0)

尝试以下几点:

Sub UpdateColD()
  Dim ws1 As Worksheet
  Dim strDate As String
  Dim lngLastRow As Long
  Dim lngDateRow As Long

  Set ws1 = ActiveWorkbook.Worksheets("Sheet1")

  'Find the last row in column D - we will clear all cells until and including this row
  lngLastRow = ws1.Range("D" & Rows.Count).End(xlUp).Row

  ' Get Date - careful with formatting and types, I've used a string
  strDate = ActiveWorkbook.Worksheets("Sheet2").Cells(16, 1)

  ' Iterate over all rows until you find the value
  For r = 1 To lngLastRow
    If ws1.Cells(r, 1) = strDate Then
        lngDateRow = r
        Exit For
    End If
  Next r

  ' Only clear if the date was found
  If lngDateRow > 0 Then
      ws1.Range(ws1.Cells(lngDateRow + 1, 4), ws1.Cells(lngLastRow, 4)).Clear
  End If
End Sub