简体   繁体   中英

Excel Finding a string

I want to know how to use the find command to search for the string of the current active cell. I though this would work, but I keep getting the "Invalid Qualifier" Error

  Dim yaxis As String
  Dim yaxis2 As Range 
  Yaxis = ActiveCell.Value
  Yaxis2 = ActiveSheet.Cells.Find(What:=Yaxis.Value)

Error locates me to this part (What:= Yaxis .Value )

How can I use the find command to find the string yaxis Is equal to?

The problem is in your use of Yaxis.Value . Yaxis is a string variable and doesn't have a Value property. Instead, just use Yaxis . You will also need to use the Set keyword when assigning to an object variable. Here's the code:

Dim Yaxis As String
Dim Yaxis2 As Range
Yaxis = Workbooks("book_name.xlsx").Worksheets("sheet_name").Selection.Value
Set Yaxis2 = Workbooks("book_name.xlsx").Worksheets("other_sheet_name").Cells.Find(What:=Yaxis)

If you need to search for the value of the active cell on a different sheet, you must specify the sheet you want to search. There can only be one ActiveCell and it will be on the ActiveSheet .

For example, if Sheet1 is active and you want to search for the active cell's value on Sheet2 , use:

Dim r As Range
Set r = Sheet2.Cells.Find(ActiveCell.Value)

If Not r Is Nothing Then
    Debug.Print "Found active cell's value on Sheet2 at: " & r.Address
End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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