簡體   English   中英

如何選擇在特定列中具有特定文本的單元格

[英]How to select a cell that has specific text in specific column

我想從以下位置更改我的地址代碼:

ThisWorkbook.Worksheets("Machine Specification").Cells(7, ActiveCell.Column).Value

對於更易於管理的一個,因此當我添加一些新行時不必重寫代碼。 因此,我正在尋找一個代碼,該代碼將在“C”列中查找特定字符串並將其用作活動列的行。

我不希望對任何文本值進行尺寸標注,因為我有數百條不同的行,我確實需要以下內容:

ThisWorkbook.Sheets("Machine Specification").Range("C:C").Find(What:="Lower Film Width", LookIn:=x1Values)

這個給出了一個語法錯誤,因為我現在不熟悉這個語法。 我用這段代碼的意思是查看列“B”並在單元格中找到具有“較低膠片寬度”的行。

請問有人可以分享代碼嗎?

獲取匹配行( Application.Match

Option Explicit

Sub GetMatchingRowOneLiner()
    MsgBox GetMatchingRow(ThisWorkbook.Worksheets("Machine Specification"), _
        "C", "Lower Film Width")
End Sub

Sub GetMatchingRowTEST()
    
    Const wsName As String = "Machine Specification"
    Const ColumnID As Variant = "C"
    Const Criteria As String = "Lower Film Width"
    
    Dim wb As Workbook: Set wb = ThisWorkbook
    Dim ws As Worksheet: Set ws = wb.Worksheets(wsName)
       
    Dim sRow As Long
    sRow = GetMatchingRow(ws, ColumnID, Criteria)
    
    If sRow = 0 Then
        MsgBox "Not found."
    Else
        MsgBox "The row is " & sRow & "."
    End If
    
End Sub

Function GetMatchingRow( _
    ByVal ws As Worksheet, _
    ByVal ColumnID As Variant, _
    ByVal Criteria As String) _
As Long
    Dim rIndex As Variant
    rIndex = Application.Match(Criteria, ws.Columns(ColumnID), 0)
    If IsNumeric(rIndex) Then
        GetMatchingRow = rIndex
    End If
End Function

暫無
暫無

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

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