我每次使用它時都會更新UsedRange。我的代碼如下:VBA:UsedRange不能正確更新
Sheets("Campaign").UsedRange 'Refresh UsedRange
LastRow = Sheets("Campaign").UsedRange.Rows.Count
這是檢查特定工作表中使用的最後一行。現在,它在第一次運行時會運行並輸出正確的整數,但如果您從具有最高行數的列中刪除行,則不會更新。問題是,如果您向該特定列添加更多行,它會更新。
這裏是我的實際代碼:
Sub CMPGN_MCRO()
Sheets("Campaign").UsedRange 'Refresh UsedRange.
LastRow = Sheets("Campaign").UsedRange.Rows.Count
Dim RangeString As String
Worksheets("Campaign").Select
Range("A1:A" & LastRow).Select
RangeString = Selection.Address
Range("A1").End(xlToRight).Select
RangeString = RangeString & ":" & Selection.Address 'Selects all the data in Campaign Worksheet.
Dim C As Range
i = 2
For Each C In Worksheets("Campaign").Range(RangeString).Cells
If Mid(C.Value, 6, 2) = "AC" Then
Worksheets("Sheet3").Range("A" & i) = C.Value 'Echo out all the filtered data and output it to another worksheet.
i = i + 1
End If
Next
MsgBox (LastRow)
End Sub
PS:我用MSGBOX呼應的是,在usedRange產生的整數。
VB.NET標記已刪除。 VB.NET <> VBA –