I'm new to vba and I have a simple problem but cannot get the code to work. I have a worksheet with headers. Column D's header is "Name name". I want to search thru this column and find the text "text". When found move to the left adjacent cell (of the found "text" cell) and enter "text2". Then move on to the next cell in "Name name". Here is what I have so far. Thanks for any suggestions and help.
Dim i As Long colLen = Range("Name Name" & Rows.Count).End(xlUp).Row For i = 1 To ColLen Cells.find(What:="text", After:=ActiveCell, LookIn:=xlFormulas, LookAt _ :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ False, SearchFormat:=False).Activate ActiveCell.Offset(0, -1).Select ActiveCell.Value = "text2" Next i
Last edited by rigray; 07-14-2011 at 11:04 AM.
Hi rigray and welcome to the forum. Please take a moment and read the forum rules located here and wrap code tags around your code as per Rule #3. After you do that someone will be able to help you.
Please leave a message after the beep!
Thanks. It should be corrected now. My apologies.
Try:
Sub MainMacro() Dim ws1 As Worksheet: Set ws1 = Sheets("Sheet1") Dim lastrow As Long Dim c As Range, iFind As Range Dim startaddress As String lastrow = ws1.Range("D" & Rows.Count).End(xlUp).Row Set iFind = ws1.Range("D2:D" & lastrow) With iFind Set c = .Find(what:="text", LookAt:=xlWhole, LookIn:=xlValues, SearchDirection:=xlNext) If Not c Is Nothing Then c.Offset(0, -1).Value = "text2" startaddress = c.Address Do c.Offset(0, -1).Value = "text2" Set c = .FindNext(c) Loop While Not c Is Nothing And c.Address <> startaddress End If End With End Sub
If you need help with explanations on why stuff does what it does let me know. The .FindNext stuff can be a little confusing.
Note: This is just showing you a way using Find (no loop). You could also use a loop without Find to accomplish the same thing.
Last edited by stnkynts; 07-14-2011 at 11:13 AM.
Hi rigray, let me know if this works:Sub rigrayLoops() Dim i As Long, rCnt As Long, flRng As Range, ColLen As Long, cl As Variant rCnt = Sheets(1).Cells(Rows.Count, 4).End(xlUp).Row For i = 1 To rCnt ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '''Setting the flRng starting at .Cells(2,4) represents the range "Name name".''' '''Where ever that cell is located, you should change the .Cells() address ''' '''accordingly. ''' Set flRng = Sheets(1).Range(Sheets(1).Cells(2, 4), Sheets(1).Cells(rCnt, 4)) For Each cl In flRng If cl.Value = "text" Then cl.Offset(0, -1).Value = "text2" Next cl Next i End Sub
Please leave a message after the beep!
One correction, I should have not been explicit with the column number in the original post. I need the code to search for a specific column named "Name name". I am having additional problems finding a column with a space in the name. I will try to implement the other aspect as you've described. Thanks.
To find "Name name" first, use:Sub rigrayLoops() Dim i As Long, rCnt As Long, flRng As Range, ColLen As Long, cl As Variant Dim Namename As Range Set Namename = Sheets(1).Cells.Find("Name name") If Not Namename Is Nothing Then rCnt = Sheets(1).Cells(Rows.Count, Namename.Column).End(xlUp).Row For i = 1 To rCnt Set flRng = Sheets(1).Range(Namename, Sheets(1).Cells(rCnt, 4)) For Each cl In flRng If cl.Value = "text" Then cl.Offset(0, -1).Value = "text2" Next cl Next i End If End Sub
Please leave a message after the beep!
bah Mordred beat me to it. I used a loop on the column search just to show a different way. Here is mine:
Sub MainMacro() Dim ws1 As Worksheet: Set ws1 = Sheets("Sheet1") Dim lastrow As Long, lastcol As Long, icol As Long, mycol As Long Dim c As Range, iFind As Range Dim startaddress As String lastcol = ws1.Cells(1, Columns.Count).Column For icol = 1 To lastcol If ws1.Cells(1, icol).Value = "Name name" Then mycol = icol Exit For End If Next icol lastrow = ws1.Cells(Rows.Count, mycol).End(xlUp).Row Set iFind = ws1.Range(Cells(2, mycol), Cells(lastrow, mycol)) With iFind Set c = .Find(what:="text", LookAt:=xlWhole, LookIn:=xlValues, SearchDirection:=xlNext) If Not c Is Nothing Then c.Offset(0, -1).Value = "text2" startaddress = c.Address Do c.Offset(0, -1).Value = "text2" Set c = .FindNext(c) Loop While Not c Is Nothing And c.Address <> startaddress End If End With End Sub
I was thinking more along the lines of the column search as well during my attempted implementation so I used your approach stnkynts, thanks for the help though Mordred. Anyway, I am getting and error at the definition of "lastrow". Any ideas?
I think it has something to do with mycol not getting defined properly. Here is the code now.
Dim wsA As Worksheet: Set wsA = Sheets("Status") Dim lastrow As Long, lastcol As Long, icol As Long, mycol As Long Dim c As Range, iFind As Range Dim startaddress As String lastcol = wsA.Cells(1, Columns.Count).Column For icol = 1 To lastcol If wsA.Cells(3, icol).Value = "EMS Composite Name" Then mycol = icol Exit For End If Next icol 'lastrow = wsA.Cells(Rows.Count, mycol).End(xlUp).Row lastrow = 120 Set iFind = wsA.Range(Cells(2, mycol), Cells(lastrow, mycol)) With iFind Set c = .find(what:="spare", LookAt:=xlWhole, LookIn:=xlValues, SearchDirection:=xlNext) If Not c Is Nothing Then c.Offset(0, -1).Value = "spare" startaddress = c.Address Do c.Offset(0, -1).Value = "spare" Set c = .FindNext(c) Loop While Not c Is Nothing And c.Address <> startaddress End If End With
ok. I found out why it was causing problems, it never found the header name I was using since I forgot i changed it on the test case. The code is running now, but it never ends and doesn't appear to be doing anything.I changed it to look at the third row for the column header name. Did I do this correctly?
Hmmm. Is your header name "EMS Composite Name" in row 3? If you could attach a copy of your workbook I could take a look at it to see where the gum up is.
I just noticed an error that i made which might be slowing things down.
Change this:
To this:lastcol = wsA.Cells(1, Columns.Count).Column
I apologize for this mistake.lastcol = wsA.Cells(1, Columns.Count).End(xlToLeft).Column
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks