简体   繁体   English

仅搜索 excel 文件的第一行

[英]Search only first row of excel file

I want to find a value only first row of my excel file.我只想在我的 excel 文件的第一行找到一个值。 but my code, find all my sheet.但是我的代码,找到我所有的工作表。

column = oBook.Sheets("Sheet1").Cells().Find(What:=CStr(size)).column

Find In First Row (Application.Match)在第一行中查找 (Application.Match)

  • Using Application.Match is simpler and more efficient.使用Application.Match更简单、更高效。 You can do the same with the Find method but there are a few more arguments that you need to use.您可以对 Find 方法执行相同的操作,但您需要使用更多的参数。
Sub FindColumn()
    
    Const Size As Double = 3.14
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' adjust!
    Dim ws As Worksheet: Set ws = wb.Sheets("Sheet1")
    Dim rrg As Range: Set rrg = ws.Rows(1)
    
    Dim CritString As String
    CritString = "*" & CStr(Size) & "*" ' contains
    
'    ' or
'    CritString = CStr(Size) & "*" ' begins with
'    ' or
'    CritString = "*" & CStr(Size) ' ends with
'    ' or
'    CritString = CStr(Size) ' number in a cell previously formatted as text
'    ' or
'    ' Use 'Size' if it's a number
        
    Dim SizeColumn As Variant ' error if not found
    SizeColumn = Application.Match(CritString, rrg, 0)
    
    If IsError(SizeColumn) Then Exit Sub
    
    ' Continue if found e.g.:
    MsgBox "The column for size " & Size & " is " & SizeColumn & ".", _
        vbInformation
    
End Sub

Please, try the next way:请尝试下一种方法:

 Dim rngSize As Range, column As Long
 Set rngSize = oBook.Sheets("Sheet1").Rows(1).Find(What:=CStr(size)) 
 If Not rngSize Is Nothing Then 
    column = rngSize.Column 
 Else 
    MsgBox size & " string could not be found in the first row..."
 End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM