2017-10-14 80 views
2

簡短的問題。我有一個Excel表格(Sheet1,Sheet2)的Excel文件。在sheet1列A我有一個日期列表,在D列我有一些數字。在sheet2 A16上,我有一個約會。基於Sheet2中的日期清除Sheet1中的內容

我希望能夠刪除Sheet1單元格A16中找到的日期開始的Sheet1列D上的數字。在Sheet2中的日期!A16每天都會發生變化,所以我會刪除不同的日期開始數...

表1個圖像 enter image description here


表2圖像
enter image description here

回答

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 
+0

感謝您的幫助,但沒有任何事情發生時,我運行宏... – lofgren

+0

你可以進入代碼,看看會發生什麼?我假設工作表被稱爲「Sheet1」和「Sheet2」。在我的娛樂工作簿中爲我工作,所以它只是尋找差異的問題;) – pwwolff

+0

當然,我會再試一次。是的,工作表被命名爲「Sheet1」和「Sheet2」。第一次我沒有任何錯誤。但沒有發生.. :))第一次當我想到這個解決方案時,我認爲它會很容易。現在我卡住了:)所以,只需在Sheet2中找到日期!A16,在Sheet1中找到這個日期!列A,刪除整個列D,從Sheet2!A16中的日期開始。我會再試一次,讓你知道。非常感謝pwwolff! – lofgren

相關問題