VBA动态改变'lastrow'

时间:2017-04-16 21:07:49

标签: excel vba excel-vba

我正在尝试根据运行宏的工作表动态更改'lastrow'。目前我正在使用两张纸。 Sheet1有大约150行数据,Sheet 2只有2行。我期望当我选择Sheet2并设置lastrow它将从Sheet2获取行数,而不是它存储来自sheet1的行数。我有什么想法可以调整这个吗?

当前代码:

sub row_count()
dim lastrow as long
lastrow = Range("A" & Rows.Count).End(xlUp).row

if lastrow = 150 then
with sheets("sheet2")
.select
lastrow = Range("A" & Rows.Count).End(xlUp).row
msgbox lastrow '<----- Always returns the value of sheet1 instead of sheet2.
end with

end sub

3 个答案:

答案 0 :(得分:1)

您正在使用With block,这意味着该计划可以查看&#39;使用&#39; &#39;以&#39;结束在关键字&#39;使用&#39;之后添加任何内容作为前缀,以便仅为sheet2修改代码:

Sub row_count()

Dim lastrow As Long

lastrow = Sheets("sheet2").Range("A" & Rows.Count).End(xlUp).Row

If lastrow = 150 Then
    With Sheets("sheet2")
    ' .Select = Sheets("sheet2").Select
    .Select
    ' .Range = Sheets("sheet2").Range
    lastrow = .Range("A" & Rows.Count).End(xlUp).Row
    MsgBox lastrow
End With

End Sub

如果您希望代码在当前可见的工作表上运行,则应将其更改为使用ActiveSheet属性:

Sub row_count()

Dim lastrow As Long

lastrow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row

If lastrow = 150 Then
    With ActiveSheet ' use the currently visible sheet
    .Select
    lastrow = .Range("A" & Rows.Count).End(xlUp).Row
    MsgBox lastrow
End With

End Sub

但是,有一些方法可以改进此代码:为了灵活性,您可以将工作表作为parameter传递。此外,如果上次使用的行中已有数据,则End函数可能会返回第一个使用过的行(它与点击最后一行并按Ctrl和向上箭头相同,所以你应该从下面的单元格开始)。最后,您无需选择工作表来获取最后一行:

Sub GetRowCounts()

row_count Sheets("sheet1")
row_count Sheets("sheet2")

End Sub
Sub row_count(ws As Worksheet)

Dim lastrow As Long

lastrow = ws.Range("A1000000").End(xlUp).Row

MsgBox lastrow

End Sub

答案 1 :(得分:0)

我认为这些例子最容易理解。

Sub FindingLastRow()

'PURPOSE: Different ways to find the last row number of a range
'SOURCE: www.TheSpreadsheetGuru.com

Dim sht As Worksheet
Dim LastRow As Long

Set sht = ThisWorkbook.Worksheets("Sheet1")

'Ctrl + Shift + End
  LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row

'Using UsedRange
  sht.UsedRange 'Refresh UsedRange
  LastRow = sht.UsedRange.Rows(sht.UsedRange.Rows.Count).Row

'Using Table Range
  LastRow = sht.ListObjects("Table1").Range.Rows.Count

'Using Named Range
  LastRow = sht.Range("MyNamedRange").Rows.Count

'Ctrl + Shift + Down (Range should be first cell in data set)
  LastRow = sht.Range("A1").CurrentRegion.Rows.Count

End Sub

https://www.thespreadsheetguru.com/blog/2014/7/7/5-different-ways-to-find-the-last-row-or-last-column-using-vba

保持开放的心态,有很多方法可以做同样的事情。

答案 2 :(得分:0)

如果这句话...

lastrow = Range("A" & Rows.Count).End(xlUp).row

...实际上总是返回Sheet1的最后一行而不是Sheet2的最后一行,这是因为您一直在查看Sheet1上打开的工作簿。

只要您具有上述(Range(...))的Range或Cells语句而没有对工作表的明确引用,则ActiveSheet就是它所引用的。

为避免这种情况,这就是您要做的:

Dim Sht_1 as Worksheet
Dim Sht_2 as Worksheet
Dim Sht_1_Lastrow as Long
Dim Sht_2_Lastrow as Long

Set Sht_1 = ActiveWorkbook.Worksheets(1)
Set Sht_2 = ActiveWorkbook.Worksheets(2)

Sht_1_Lastrow = Sht_1.Range("A" & Sht_1.Rows.Count).End(xlUp).Row
Sht_2_Lastrow = Sht_2.Range("A" & Sht_2.Rows.Count).End(xlUp).Row

Sht_1_Lastrow = Sht_1.Cells(Sht_1.Rows.Count, "A").End(xlUp).Row
Sht_2_Lastrow = Sht_2.Cells(Sht_2.Rows.Count, "A").End(xlUp).Row

上面的代码块突出显示了使LastRow变量显式绑定到特定工作表的区别...

这样您的问题就不会发生...