Discussion Locked

Editing Excel Database with a Macro

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I am trying to automate adding a new column to an existing database based on certain column criteria. One of the things I'm attempting to automate is to create a macro that Finds specific info. down one column, then tabs over four columns and enters a word. Then returns back to the original column and continues the same Find again and repeats the changes all the way down 10,000 entries.

I tried this macro:
Sub Macro2()

Cells.Find(What:="k", After:=ActiveCell, LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
False).Activate
Range("E4225").Select
ActiveSheet.Paste
Range("A4226").Select
Cells.Find(What:="k", After:=ActiveCell, LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
False).Activate
Range("E4245").Select
ActiveSheet.Paste
Range("A4246").Select

The problem with the macro above is it only wants to repeat the action for the rows it found when I recorded the macro. Is there a way to have the macro repeat the find command, and make changes to different rows all the way down a column?

Would appreciate any help

Thanks,

Randy

5 replies

by peyison

Randy,

This macro should do what you're looking for. It also will start the find at the cell A1, and stop looking when Find goes back to the top of the page.

Sub Macro2()
Dim iLastRow As Integer
Dim rResult As Range

'start the find in A1Cells(1, 1).Activate

Do

Set rResult = Cells.Find(What:="k", After:=ActiveCell, LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
False)

If rResult Is Nothing Then
MsgBox "No values found"
Exit Sub
End If

If rResult.Row > iLastRow Then
iLastRow = rResult.Row
rResult.Offset(0, 4).Select
ActiveSheet.Paste
ActiveCell.Offset(1, -4).Select
End If

Loop Until rResult.Row < iLastRow

MsgBox "Done"

End Sub

Hope this helps

by sankulpune

Hello Randy ,

Here is another way to do it. You run this macro after selecting the column or the range in the column in which the item is to be found.

Sub FindAndWrite()

Dim strToFind As String
Dim strToWrite As String

strToFind = InputBox("string to find")
strToWrite = InputBox("string to write")

Dim objCell As Object

For Each objCell In Selection

If InStr(1, objCell, strToFind) > 0 Then
Cells(objCell.Row, objCell.Column + 4) = strToWrite
End If

Next
End Sub

I have successfully tried this code and would be happy to help you successfully implement this code.

Your feedback would be highly appreciated.

With best regards,

SanganakSakha,
Your Friend In Need.

This discussion is closed to new replies.