确定特定范围内的最后一个单元并填充,VBA

时间:2019-01-30 13:58:54

标签: excel vba

因此,我需要确定“修订级别”(“ C82:F91”),“修订日期”(“ G82:G91”)和“修订注释或注释”中每个区域的第一个空白单元格(“ H82:R91”),并在其中填写正确的信息。对于“ revlvl”,我需要将其添加到先前的值中,而我知道如何计算其他信息。

因此,它需要做的是从000修订级别开始,将001置于修订级别,将今天的日期输入修订日期,并将输入框中的文本输入注释和注释。然后,如果再次运行循环,它将在第三行中放入003,日期和注释。

循环中还有其他引用,因为它正在比较上面两个图表中的日期,它们可以正常工作,不需要更改。有问题的部分将突出显示,当前位置已进行硬编码。

This is the sheet im working with and the ranges

enter image description here

If Sheets("Monthly Status").Range("G82") = "" Then
'do nothing
Else

    Dim i As Integer
    Dim Revnotes As Range
    Dim RevDate As Range
    Dim revlvl As Range

    Set Revnotes = Sheets("Monthly Status").Range("H83:R83")
    Set RevDate = Sheets("Monthly Status").Range("G83")
    Set revlvl = Sheets("Monthly Status").Range("C83:F91")

    For i = 0 To 49

    cRow = i + 23

        For j = 0 To 2

                If Sheets("Monthly Status").Cells(cRow, j + 11) <> Sheets("Monthly Status").Cells(cRow, j + 37) Then
                    RevDate = Date
                    Revnotes = InputBox("You Changed dates, you must imput notes for this revison!", "Notes")
                    revlvl = Sheets("Monthly Status").Range("C83") + 1

                        Application.ScreenUpdating = False

                        Sheets("Monthly Status").Range("K23:M72").Copy
                        Sheets("Monthly Status").Range("AK23:AM72").PasteSpecial xlPasteValues

                        Application.ScreenUpdating = True

                    End
                End If
            Next j
        Next i

    End If

1 个答案:

答案 0 :(得分:1)

要查找范围C82:F91中的下一个空闲行,请使用以下内容,因此您无需循环。

Dim rng As Range
Set rng = Worksheets("Monthly Status").Range("C82:F91")

Dim NextFreeRow As Long
With rng.Cells(rng.Rows.Count, 1)
    If .Value = vbNullString Then 'test if range is full
        NextFreeRow = .End(xlUp).Row + 1 'find next free row
    Else
        MsgBox "range is full"
        Exit Sub
    End If
End With

'add +1 to the previous value
Worksheets("Monthly Status").Cells(NextFreeRow, "C").Value = Worksheets("Monthly Status").Cells(NextFreeRow - 1, "C").Value + 1

此示例应帮助您解决问题。