-1

I would like to create a macro in Excel that:

1st: Finds a cell with a certain word.

2nd: Goes down one cell and left two cells (arrow keys) (from column C to column A)

3rd: Selects the cell in column A, and the adjacent cell in column B

4th: Auto fills columns A and B with their respective contents until the next instance of the same word in step 1. (repeat process)....Could this be done in a loop?

Thank you in advance!!!

Community
  • 1
  • 1
  • This isn't a code writing service. Can you please show what you've done to solve this problem? – Haris Jun 19 '14 at 13:51

2 Answers2

1

This ended up working. Got it from a nice fellow over at ozgrid:

Sub FindAndFill() Dim firstAdd As String, findWord As String Dim fCell As Range Dim firstRow As Long, nextRow As Long, lastRow As Long

findWord = InputBox("What are we looking for?", "Search word") 
If findWord = "" Then Exit Sub 


Application.ScreenUpdating = False 
With ActiveSheet.Range("C:C") 
    Set fCell = .Find(findWord) 
    If fCell Is Nothing Then 
        MsgBox findWord & " was not found.", vbOKOnly, "Not found" 
        Exit Sub 
    End If 

     'Define our limits
    firstAdd = fCell.Address 
    lastRow = ActiveSheet.UsedRange.Rows.Count 

    Do 
        firstRow = fCell.Row 
        Set fCell = .FindNext(fCell) 
        If fCell.Address = firstAdd Then 
            nextRow = lastRow 
        Else 
            nextRow = fCell.Row - 1 
        End If 
        Range(Cells(firstRow + 1, "A"), Cells(nextRow, "B")).FillDown 
    Loop Until nextRow = lastRow 
End With 


Application.ScreenUpdating = True 

End Sub

0

I THINK this does what you intend:

Sub stack()
Dim certain_word As String 'this will be your word
Dim count1, count2, count3, count4 As Long 'counts to move through loop

certain_word = "a" 
'if you wanted to find the word "a". you will have to change this
count1 = Application.CountA(Range("A:A"))
'this will find the number of rows, assuming row "A" will provide an accurate count
count2 = 1
count3 = count2 + 1

While count4 < count1
    If Range("C" & CStr(count2)).Value = certain_word Then
        If Range("C" & CStr(count3)) <> certain_word Then
            If Range("B" & CStr(count3)) <> "" Then
                Range("A" & CStr(count3)) = Range("A" & CStr(count2))
                Range("B" & CStr(count3)) = Range("B" & CStr(count2))
            End If
            count3 = count3 + 1
            count4 = count4 + 1
        Else
            count2 = count2 + 1
            count3 = count2 + 1
        End If
    Else
        count2 = count2 + 1
        count3 = count2 + 1
        count4 = count4 + 1
    End If
Wend
End Sub

The input is here on the right and output on the left. Let me know if this is right.

1   10  a       1   10  a
1   10  b       23  1   b
1   10  c       34  1   c
25  13  a       25  13  a
8   14  a       8   14  a
8   14  c       7   15  c
Tanner
  • 548
  • 8
  • 20