Results 1 to 4 of 4

Thread: Searching for cell

Hybrid View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    332

    Searching for cell

    I have an excel file with many columns that denote fields, like in a database

    Can I search for an ID in a particular column, and once the ID is found, modify a cell of another field in the same row as the ID just found? Anybody done this before?

  2. #2
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    Sure thing. Just find the text you are looking for in the column, the offset the column from there and do what you want with the cell. This finds "ID123" in column "E" and turns the cell three coumns over red.

    VB Code:
    1. Sub FindAndChange()
    2.    
    3.     Dim rngFound As Range
    4.     Dim strFind As String
    5.    
    6.     strFind = "ID123"
    7.    
    8.     ' Find strFind in Column "E".
    9.     Set rngFound = Columns("E:E").Find(What:=strFind, LookIn:=xlValues, _
    10.         LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    11.         MatchCase:=False)
    12.    
    13.     ' If found, make the cell three coumns over red.
    14.     If Not rngFound Is Nothing Then
    15.         rngFound.Offset(ColumnOffset:=3).Interior.ColorIndex = 3
    16.     End If
    17.  
    18. End Sub

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    332
    what does the offset part mean?

  4. #4
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    It means from the found cell, it "offsets" three columns--the cell three columns to the right.

    Offset Property (Range Object)


    Returns a Range object that represents a range that’s offset from the specified range. Read-only.

    Syntax

    expression.Offset(RowOffset, ColumnOffset)

    expression Required. An expression that returns a Range object.

    RowOffset Optional Variant. The number of rows (positive, negative, or 0 (zero)) by which the range is to be offset. Positive values are offset downward, and negative values are offset upward. The default value is 0.

    ColumnOffset Optional Variant. The number of columns (positive, negative, or 0 (zero)) by which the range is to be offset. Positive values are offset to the right, and negative values are offset to the left. The default value is 0.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width