Range(Cell.Find(“价格标签”), Range(Cells.Find(“价格标签”)).End(xlDown))

发布于 2025-01-11 07:50:26 字数 199 浏览 2 评论 0原文

我想找到“价格标签”在工作表上的位置,然后按照该列一直向下选择。 我写了

Range(Cells.Find("Price tag"), Range(Cells.Find("Price Tag")).End(xlDown))

但我收到了 [range method of object _global failed] 消息。 代码有什么问题,我该如何修复它?

I want to find where 'price tag' is on the sheet, and follow that column to select all way down.
I have wrote

Range(Cells.Find("Price tag"), Range(Cells.Find("Price Tag")).End(xlDown))

but I got [range method of object _global failed] message.
What is wrong with the code, and how can I fix it?

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

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

发布评论

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

评论(2

紅太極 2025-01-18 07:50:26

使用 Find 方法

  • 如果未找到搜索值,则结果将为 Nothing,因此最安全的做法是在 Find 方法中使用范围变量,然后测试针对 Nothing 的变量。
Option Explicit

Sub Test()
    
    Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
    Dim ColumnRange As Range

    Dim fCell As Range ' simplified due to assuming it is never in cell `A1`
    Set fCell = ws.Cells.Find("Price Tag", , xlFormulas, xlWhole, xlByRows)
    
    ' Decide what to do if found or not.
    If fCell Is Nothing Then
        MsgBox "'Price Tag' not found.", vbCritical
        Exit Sub
    Else
        Set ColumnRange = ws.Range(fCell, fCell.End(xlDown))
        MsgBox "'Price Tag' was found in cell '" & fCell.Address(0, 0) _
            & "' and the address of your range is '" _
            & ColumnRange.Address(0, 0) & "'.", vbInformation
    End If
        
    ' But usually you know in which row...
    With ws.Rows(1)
        Set fCell = .Find("Price Tag", .Cells(.Cells.Count), xlFormulas, xlWhole)
    End With
    
    ' or in which column it is:
    With ws.Columns("A")
        Set fCell = .Find("Price Tag", .Cells(.Cells.Count), xlFormulas, xlWhole)
    End With
    
End Sub

Using the Find Method

  • If the searched value is not found, the result will be Nothing, so it is safest to use a range variable with the Find method, and then test the variable against Nothing.
Option Explicit

Sub Test()
    
    Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
    Dim ColumnRange As Range

    Dim fCell As Range ' simplified due to assuming it is never in cell `A1`
    Set fCell = ws.Cells.Find("Price Tag", , xlFormulas, xlWhole, xlByRows)
    
    ' Decide what to do if found or not.
    If fCell Is Nothing Then
        MsgBox "'Price Tag' not found.", vbCritical
        Exit Sub
    Else
        Set ColumnRange = ws.Range(fCell, fCell.End(xlDown))
        MsgBox "'Price Tag' was found in cell '" & fCell.Address(0, 0) _
            & "' and the address of your range is '" _
            & ColumnRange.Address(0, 0) & "'.", vbInformation
    End If
        
    ' But usually you know in which row...
    With ws.Rows(1)
        Set fCell = .Find("Price Tag", .Cells(.Cells.Count), xlFormulas, xlWhole)
    End With
    
    ' or in which column it is:
    With ws.Columns("A")
        Set fCell = .Find("Price Tag", .Cells(.Cells.Count), xlFormulas, xlWhole)
    End With
    
End Sub
涫野音 2025-01-18 07:50:26

此功能将为您扩展选择范围。

Public Function DataCells(Source As Range) As Range
    Dim ColUsedRange As Range
    Dim Col As Range
    Dim RowCount As Long
    
    For Each Col In Source.Columns
        Set ColUsedRange = Range(Col, Col.EntireColumn.Cells(Source.Parent.Rows.Count).End(xlUp))
        If RowCount < ColUsedRange.Rows.Count Then RowCount = ColUsedRange.Rows.Count
    Next
    
    Set DataCells = Source.Resize(RowCount)
End Function

用法

Sub Test()
    Dim Target As Range
    Set Target = Cells.Find("Price tag")
    
    If Not Target Is Nothing Then
        Set Target = DataCells(Target)
        Application.Goto Target
    End If
End Sub

This function will extend the selections for you.

Public Function DataCells(Source As Range) As Range
    Dim ColUsedRange As Range
    Dim Col As Range
    Dim RowCount As Long
    
    For Each Col In Source.Columns
        Set ColUsedRange = Range(Col, Col.EntireColumn.Cells(Source.Parent.Rows.Count).End(xlUp))
        If RowCount < ColUsedRange.Rows.Count Then RowCount = ColUsedRange.Rows.Count
    Next
    
    Set DataCells = Source.Resize(RowCount)
End Function

Usage

Sub Test()
    Dim Target As Range
    Set Target = Cells.Find("Price tag")
    
    If Not Target Is Nothing Then
        Set Target = DataCells(Target)
        Application.Goto Target
    End If
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文