簡體   English   中英

檢查哪個是特定列中最后一個具有值的單元格,然后在excel vba中選擇其下方的單元格

[英]Check which is the last cell in a specific column which has a value and select the cell below it in excel vba

您好,我正在嘗試編寫一些代碼來獲取特定列(例如A列)中具有值的最后一個單元格。 我只知道這一點:

Cells(3, 2).Value = quantity

要確定最后一行,請使用以下公式

lastRow = Cells(rows.count,1).End(xlUp).row
  • rows.count =該版本的Excel(2003年為65k,等等)中的總行數,即該Excel版本中的最后一行
  • 1在rows.count =列A的右邊,2將是B的依此類推,依此類推
  • xlUp大致意味着它將從下往上搜索

您可以在此命令中搜索更多內容。

現在,一旦知道了最后一行,就可以使用它來獲取其值

Variable = Cells(lastRow,1).Value

如果要查找范圍中最后一個單元格的行,最好使用find函數。

例如:對於特定列n; 使用下面的代碼將其設置為數量

Dim n As Long, quantity As Long
n = 3
quantity = 5000
Cells(Columns(n).Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, _ 
    SearchDirection:=xlPrevious).Row, n).Value = quantity

像這樣的東西檢查:

  • A列中至少有一個值(否則無選擇)
  • 最后一個值不在最后一行中(嘗試偏移一行時避免錯誤)

Sub LastRow()
Dim rng1 As Range
Set rng1 = Columns("A").Find("*", [a1], xlValues, , xlByRows, xlPrevious)
If Not rng1 Is Nothing Then
    If rng1.Row <> Rows.Count Then rng1.Offset(1, 0).Activate
End If
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM