如何创建从特定单元格到变量单元格的范围

时间:2017-10-10 16:02:10

标签: excel-vba vba excel

我想将范围从“D250”设置为始终更改的单元格

但我不能把变量设置为代码行!设置newRng = ws.Range(“D250:endRngMun”)endRngMun总是在改变......

Sub Compare()

    Dim ws  As Worksheet
    Dim munCompt As Long
    Dim totalMun As Long
    Dim endRngMun As String



    Set ws = Sheets("T1")

    ' find last row with data in Column B at current regioun starting at cell B10
    With ws.Range("D250").CurrentRegion
        munCompt = .Rows(.Rows.Count).Row
    End With

    Dim Rng         As Range
    Set Rng = ws.Range("D250:D" & munCompt)

    ' find last row inside the range, ignore values inside formulas
    munCompt = Rng.Find(What:="*", _
                        After:=Range("D250"), _
                        LookAt:=xlPart, _
                        LookIn:=xlValues, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlPrevious, _
                        MatchCase:=False).Row


    totalMun = Application.WorksheetFunction.CountA(Rng)

    endRngMun = "D" & 250 + totalMun - 1

    Dim newRng As Range
    Set newRng = ws.Range("D250:endRngMun")

    ' for debug
    Debug.Print newRng.Count




End Sub

0 个答案:

没有答案