检查一个单元格区域的值,然后在制作一张新纸后清除内容

时间:2018-11-21 19:42:19

标签: excel vba excel-vba

我潜伏在StackOverflow上已经有一段时间了,我整理了一些代码来帮助自动生成新的每日报告表。我的代码运行得很好,可以找到以前的报告,用+1生成一个新报告,并清除一些需要每天重置的范围。

我当前的问题是试图清除一些条件单元格。我已经在这里和其他excel / VBA网站上尝试了几个紧密的用例,但是我对VBA不够熟练,无法直接将其翻译成我的代码。

更具体地说,对于所有47至79行,我需要检查合并单元格H47:I47是否包含值“ N”,以进行简单的是,否检查

如果这种情况成立,那么我需要.clearcontents检查单元格左侧的列。所以A47:G47。然后,我需要它对每行(直到79行)重复执行此检查。

我认为当前的工作表命名结构使我的问题变得过于复杂,并试图引用不断变化的“ sht_NewWeek)

我将粘贴我到目前为止的内容,并期待您的帮助

Sub CreateNewSheet()

    Dim wrkSht As Worksheet
    Dim lWkNum As Long
    Dim lCurNum As Long
    Dim sht_LastWeek As Worksheet
    Dim sht_NewWeek As Worksheet

    'Find previous week and set reference to it.
    For Each wrkSht In ThisWorkbook.Worksheets
        If IsNumeric(Replace(wrkSht.Name, "Report ", "")) Then
            lCurNum = CLng(Replace(wrkSht.Name, "Report ", ""))
            If lCurNum > lWkNum Then lWkNum = lCurNum
        End If
    Next wrkSht
    Set sht_LastWeek = ThisWorkbook.Worksheets("Report " & lWkNum)

    'Create new sheet, set reference to it and rename.
    sht_LastWeek.Copy after:=Sheets(sht_LastWeek.Index)
    Set sht_NewWeek = Sheets(sht_LastWeek.Index + 1)
    sht_NewWeek.Name = "Report " & lCurNum + 1

    'Clear the cells and relink formula to previous sheet, while updating report date to today
    With sht_NewWeek
        .Range("D16:M16,B20:B44,C20:C44,D20:M44,D19:M19").ClearContents
        .Range("F12").Value = "=Today()"
        .Cells.Replace What:="'Report " & lCurNum - 1 & "'!", _
                       Replacement:="'Report " & lCurNum & "'!", _
                       LookAt:=xlPart
    End With


End Sub

0 个答案:

没有答案