![](/img/trans.png)
[英]How to find first and last cell in ExcelInterop and graph range in C#
[英]How to find the first and last populated cell in a range with whitespace
对于宏,我正在使用VBA通过查找填充的第一行和最后一行来确定日期范围。 我已经找到一种通过使用End.xlToRight查找填充的第一行和最后一个行的方法,但是仅当它是一个连续填充的行时才有效,例如:
183 | 183 | 183 | 183 | 183 | 183 | 183 | 183 |
现在一些行如下:
183 | 183 | 183 | 183 | empty | 183 | 183 | 183 | 183 |
要么
183 | 183 | 183 | 183 | 183 | 183 | 183 | 305| 305| 305| 305
什么是找到系列中第一个和最后一个填充单元格与中间空单元格的坐标的好方法?
谢谢!
如果没有什么超出您的数据,则从该行的最右边开始,然后向左走。 即,如果您的数据在第1行,则
.cells(1,columns.count).end(xltoleft)
请尝试以下。 仅当数据从行A列开始时才能正常工作:
last_row = ActiveSheet.UsedRange.Rows.Count
对于列:
last_column = ActiveSheet.UsedRange.Columns.Count
您还可以使用:
last_row = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
最安全的方法是使用以下公式,即使数据不在A列中开始,该公式也将返回最后一行。
last_row = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row
Sub FindMethod()
With ThisWorkbook.ActiveSheet
' First Used Row
Dim FirstUR As Long
If Not .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), -4123, , 1) _
Is Nothing Then _
FirstUR = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count)).Row
' First Used Column
Dim FirstUC As Integer
If Not .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), -4123, , 1) _
Is Nothing Then FirstUC = _
.Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), , , 2).Column
' Last Used Row
Dim LastUR As Long
If Not .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), -4123, , 1) _
Is Nothing Then LastUR = .Cells.Find("*", , , , , 2).Row
' Last Used Column
Dim LastUC As Integer
If Not .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), -4123, , 1) _
Is Nothing Then LastUC = .Cells.Find("*", , , , 2, 2).Column
' First Used Cell (First Cell of the Used Range)
Dim FirstUCell As Range
If Not .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), -4123, , 1) _
Is Nothing Then Set FirstUCell = .Cells(.Cells.Find("*", _
.Cells(.Rows.Count, .Columns.Count)).Row, .Cells.Find("*", _
.Cells(.Rows.Count, .Columns.Count), , , 2).Column)
' Last Used Cell (Last Cell of the Used Range)
Dim LastUCell As Range
If Not .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), -4123, , 1) _
Is Nothing Then Set LastUCell = .Cells(.Cells.Find("*", , , , 1, 2) _
.Row, .Cells.Find("*", , , , 2, 2).Column)
' Used Range (Not UsedRange)
Dim URng As Range
If Not .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), -4123, , 1) _
Is Nothing Then Set URng = .Range(.Cells(.Cells.Find("*", _
.Cells(.Rows.Count, .Columns.Count)).Row, .Cells.Find("*", _
.Cells(.Rows.Count, .Columns.Count), , , 2).Column), .Cells(.Cells _
.Find("*", , , , 1, 2).Row, .Cells.Find("*", , , , 2, 2).Column))
' Usage - Rows, Columns
Debug.Print "First Used Row = " & FirstUR
Debug.Print "First Used Column = " & FirstUC
Debug.Print "Last Used Row = " & LastUR
Debug.Print "Last Used Column = " & LastUC
' Usage - Ranges
If Not FirstUCell Is Nothing Then
Debug.Print "First Used Cell Address = " & FirstUCell.Address
Else
Debug.Print "First Used Cell = Nothing (Empty Sheet)"
End If
If Not LastUCell Is Nothing Then
Debug.Print "Last Used Cell Address = " & LastUCell.Address
Else
Debug.Print "Last Used Cell = Nothing (Empty Sheet)"
End If
If Not FirstUCell Is Nothing Then
Debug.Print "Used Range Address = " & URng.Address
Else
Debug.Print "Used Range = Nothing (Empty Sheet)"
End If
' ' Some Thoughts:
' FirstUR = FirstUCell.Row
' FirstUC = FirstUCell.Column
' LastUR = LastUCell.Row
' LastUC = LastUCell.Column
'
' FirstUR = URng.Row
' FirstUC = URng.Column
' LastUR = URng.Rows.Count - URng.Row + 1
' LastUC = URng.Columns.Count - URng.Column + 1
'
' Set FirstUCell = .Cells(FirstUR, FirstUC)
' Set LastUCell = .Cells(LastUR, LastUC)
'
' Set FirstUCell = .Cells(URng.Row, URng.Column)
' Set FirstUCell = URng.Cells(1, 1)
' Set LastUCell = .Cells(URng.Rows.Count - URng.Row + 1, _
' URng.Columns.Count - URng.Column + 1)
' Set LastUCell = URng.Cells(URng.Rows.Count, URng.Columns.Count)
'
' Set URng = .Range(FirstUCell, LastUCell)
' Set URng = .Range(.Cells(FirstUR, FirstUC), .Cells(LastUR, LastUC))
' Set URng = .Range(FirstUCell.Address & ":" & LastUCell.Address)
' Various Resize and Offset possibilities
End With
End Sub
在这种情况下,最好使用CurrentRegion
因此,如果数据从单元格A1中开始,则可以执行以下操作: Range("A1").CurrentRegion
当前标记的答案将为您提供工作表上所有数据的区域,这并不是真正要问的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.