+ Reply to Thread
Results 1 to 2 of 2

selection.find - write a number in the cell next to found cell

  1. #1
    Registered User
    Join Date
    06-10-2012
    Location
    Oslo
    MS-Off Ver
    Excel 2011 Mac
    Posts
    1

    selection.find - write a number in the cell next to found cell

    Hi!

    I'm new to VBA so I was hoping for some help with my macro:

    What I am trying to do:

    I want to find all cells with a specific text (for example "a"), and then write a number in the cell to the right of the found cell (for example "1")

    I no have tried for a while, and here is what I have got for now:

    Sub find()

    Range("b5:b35").Select
    Selection.find(what:="a", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False).Activate

    If Not ActiveCell Is Nothing Then Range(ActiveCell, ActiveCell.End(xlToRight)).Select
    With ActiveCell.Offset(0, 1)
    .Value = "1"

    End With

    End Sub


    But it doesn't run as I hoped:

    - Even if there are different cells with "a" within the range, I only get "1" written in one of the cells right of one "a" cell. I want to be able to get "1" next to all cells with "a" within the range.

    - Even if I run my macro more times, I will only get one cell with "1" in (and it is the exact same cell).

    Can someone please help me with a tip, correction or a link to some suitable information?

  2. #2
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: selection.find - write a number in the cell next to found cell

    Sir_Nemo,

    Welcome to the Excel Forum.

    You did not show us what strings are in the range B5:B35.

    Try:

    Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

    1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
    2. Open your workbook
    3. Press the keys ALT + F11 to open the Visual Basic Editor
    4. Press the keys ALT + I to activate the Insert menu
    5. Press M to insert a Standard Module
    6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
    7. Press the keys ALT + Q to exit the Editor, and return to Excel
    8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


    Please Login or Register  to view this content.

    Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm


    Then run the FindMya macro.
    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1