如何在Excel VBA中查找连续数据的单元格?

发布于 2024-08-08 11:41:08 字数 478 浏览 10 评论 0原文

替代文本

给定图像...如果我知道有一些数据从 Range("B3") 开始。
如何找到单元格 E3 之前具有连续数据的单元格?由于 F3 为空白,因此不应考虑 G3 及以后的内容。 结果可以是范围对象 (B3:E3) 或单元格计数(在本例中为 4)。

通过将 B3 设置为活动单元格并执行...

Range(ActiveCell, ActiveCell.End(xlToRight).Count

我确实得到了计数,但是此方法并不可靠,如果只有 B3 有数据,它会对单元格进行计数,直到工作表末尾。
当然,这也可以通过循环单元格来实现,但我宁愿使用工作表函数或其他一些有效的方法。

alt text

Given the Image... If I know that there is some data starting at Range("B3").
How can I find the cells with contiguous data that is till cell E3? Since F3 is blank G3 onwards should not be considered.
The result could either be a range object (B3:E3) or count of cells( 4 in this case).

By setting B3 as the Active cell and doing..

Range(ActiveCell, ActiveCell.End(xlToRight).Count

I do get the count, however this method is not reliable, in case only B3 has data it counts the cells till the end of the sheet.
Of course this could also be achieved by looping through the cells but I'd rather use a Worksheet Function or some other efficient method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

戏蝶舞 2024-08-15 11:41:08

您似乎正在尝试确定从单元格 B3 开始的一行中使用的连续列数。

下面的代码将根据您的数据返回 $B$3:$E$3 和 4 的值。如果只有单元格 B3 有数据,它将返回 $B$3 和 1。

Sub GetDataArea()

Dim strCellToTest As String
Dim rngMyRange As Range
Dim lngColumns As Long

strCellToTest = "B3"

lngColumns = ActiveWorkbook.ActiveSheet.Range("" & strCellToTest).End(xlToRight).Column - 1

If lngColumns >= 256 Then
 Set rngMyRange = ActiveWorkbook.ActiveSheet.Range("" & strCellToTest)
 lngColumns = 1
Else
 Set rngMyRange = ActiveWorkbook.ActiveSheet.Range _
 (strCellToTest & ":" & Range("" & strCellToTest).Offset(0, lngColumns - 1).Address)
End If

MsgBox "Columns: " & lngColumns & vbCr & vbLf & "Range: " & rngMyRange.Address

End Sub

It seems that you are trying to determine the number of continuous columns used by in a row, starting from cell B3.

The code below will return the values of $B$3:$E$3 and 4 based on your data. If only cell B3 has data, it will return $B$3 and 1.

Sub GetDataArea()

Dim strCellToTest As String
Dim rngMyRange As Range
Dim lngColumns As Long

strCellToTest = "B3"

lngColumns = ActiveWorkbook.ActiveSheet.Range("" & strCellToTest).End(xlToRight).Column - 1

If lngColumns >= 256 Then
 Set rngMyRange = ActiveWorkbook.ActiveSheet.Range("" & strCellToTest)
 lngColumns = 1
Else
 Set rngMyRange = ActiveWorkbook.ActiveSheet.Range _
 (strCellToTest & ":" & Range("" & strCellToTest).Offset(0, lngColumns - 1).Address)
End If

MsgBox "Columns: " & lngColumns & vbCr & vbLf & "Range: " & rngMyRange.Address

End Sub
寻找一个思念的角度 2024-08-15 11:41:08

<代码>

Intersect(Activecell.CurrentRegion, ActiveCell.EntireRow)

将返回 B3:E3。或者

If IsEmpty(ActiveCell.Offset(0,1).Value) Then
   Set rMyRange = ActiveCell
Else
   Set rMyRange = ActiveCell.Parent.Range(ActiveCell, ActiveCell.End(xlToRight))
End If

rMyRange 还将返回 B3:E3

Intersect(Activecell.CurrentRegion, ActiveCell.EntireRow)

Will return B3:E3. Alternatively

If IsEmpty(ActiveCell.Offset(0,1).Value) Then
   Set rMyRange = ActiveCell
Else
   Set rMyRange = ActiveCell.Parent.Range(ActiveCell, ActiveCell.End(xlToRight))
End If

rMyRange will also return B3:E3

最舍不得你 2024-08-15 11:41:08

您可以使用 CurrentRegion 属性。这将返回与指定范围相邻的范围。所以...

Range("B3").CurrentRegion returns the range B3:E3
Range("B3").CurrentRegion.Columns.Count returns 4
Range("B3").CurrentRegion.Cells.Count also returns 4

但是,如果您的数据位于第 4 行及以下(假设您的数据位于 B4:E6 中),那么您将得到这些结果

Range("B3").CurrentRegion returns the range B3:E6
Range("B3").CurrentRegion.Columns.Count returns 4
Range("B3").CurrentRegion.Cells.Count returns 16

这是您想要的结果吗?

You could use the CurrentRegion property. This returns the range that is contiguous to the specified range. So...

Range("B3").CurrentRegion returns the range B3:E3
Range("B3").CurrentRegion.Columns.Count returns 4
Range("B3").CurrentRegion.Cells.Count also returns 4

However, if you had data in rows 4 and below (let's say you had data in B4:E6), then you would get these results

Range("B3").CurrentRegion returns the range B3:E6
Range("B3").CurrentRegion.Columns.Count returns 4
Range("B3").CurrentRegion.Cells.Count returns 16

Is this what you were after?

眼眸里的快感 2024-08-15 11:41:08

我喜欢使用一个函数来计算包含值的列,直到遇到空单元格。返回值可用于设置 FOR NEXT 循环来搅动表。我将这样做:

Sub tester()
    Dim Answer
    Answer = CountColumns(3, 2)
    MsgBox "There are " & Answer & " columns."
 End Sub
Public Function CountColumns(ByVal startRow As Integer, ByVal startColumn As Integer)
    'Pass starting location in spreadsheet for function to loop through until
    'empty cell is found. Return count of columns function loops through

     Do While ActiveSheet.Cells(startRow, startColumn).Value <> ""
        startColumn = startColumn + 1
     Loop
     startColumn = startColumn - 1
     CountColumns = startColumn
 End Function

I like to use a function that counts columns that contain values until it encounters an empty cell. The return value can be used to set up a FOR NEXT loop to churn through a table. Here is how I would do it:

Sub tester()
    Dim Answer
    Answer = CountColumns(3, 2)
    MsgBox "There are " & Answer & " columns."
 End Sub
Public Function CountColumns(ByVal startRow As Integer, ByVal startColumn As Integer)
    'Pass starting location in spreadsheet for function to loop through until
    'empty cell is found. Return count of columns function loops through

     Do While ActiveSheet.Cells(startRow, startColumn).Value <> ""
        startColumn = startColumn + 1
     Loop
     startColumn = startColumn - 1
     CountColumns = startColumn
 End Function
池予 2024-08-15 11:41:08

根据您需要获得的通用程度,它可能很简单,

Application.WorksheetFunction.Count([b4:e4])

如果您想绑定 ActiveCell,请尝试

Application.WorksheetFunction.Count(intersect(activecell.CurrentRegion, activecell.EntireRow))

Depending on how general you need to get, it could be as simple as

Application.WorksheetFunction.Count([b4:e4])

If you want to tie in the ActiveCell, try

Application.WorksheetFunction.Count(intersect(activecell.CurrentRegion, activecell.EntireRow))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文