Find, then find Next and append into certain cells as you go...

mystik5

New Member
Joined
Apr 5, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I am looking to search through a worksheet, find a specific variable that is set from a user input. Find the value, then write a value to the right of field where the entry is found. I also need to do this as a loop, so wherever X is found in that list, write a value to the right of the search result, then find the next one and repeat.

This is what i have so far:

Set src = Workbooks.Open("\\a networkpath\excelfilename.xlsx")
cells.find(What:=myvariable. After:=Range("A1"))
g = activecell.offset(0, -1).value
Activecell.offset(0, -1).value = g & vbNewLine & Now() & " Email sent on this date"
Actvecell.offset)0,22).value = myvariable

So if i run this code it will find the search term once, since there is no loop and append as needed. It is just getting this to loop that i am having an issue. any help would be truly appreciated. I know this has to be an easy solution, however all the examples i have tried have not worked. thanks in advance for any and all help.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi, I would like to try this, can you demonstrate what you said with about 10 rows of sample data so I can see it.
 
Upvote 0
i have attached some sample data as requested. What I would like to do is to take input on the bank name, in this case we will use the example of Bank Fun. I would like to search the entire file, find the first entry of Bank Fun, go to the left of it to append a comment to the existing comment, then go to the right and add a date of when a reach out occurred, which will also come from user input. Then loop through the rest of the file looking for Bank fun appending to the comments section and adding in the date. do this until no more Bank Fun's are found.

Thanks for your help!!


sample_data.xlsx
ABCDEFGHIJK
1PhaseDateCommentsBank NameContact NameBackup ContactEmail AddressCountryRegionAML ApplicableDate of Contact
211/8/2022Test Comment1Bank FunJohn SmithTerry Handleemail@somewhere.comUSANAMY
321/16/2022Test Comment2Bank ToyJohn SmithTerry Handleemail@somewhere.comUSANAMN
413/1/2022Test Comment3Bank FoodJohn SmithTerry Handleemail@somewhere.comCanadaNAMN
533/3/2022Test Comment4Bank FunJohn SmithTerry Handleemail@somewhere.comBrazilLATAMY
633/18/2022Test Comment5Bank PizzaJohn SmithTerry Handleemail@somewhere.comMexicoLATAMN
744/1/2022Test Comment6Bank YoloJohn SmithTerry Handleemail@somewhere.comChinaAPACN
856/8/2022Test Comment7Bank FunJohn SmithTerry Handleemail@somewhere.comJapanAPACY
968/1/2022Test Comment8Bank WestJohn SmithTerry Handleemail@somewhere.comIrelandEMEAY
1078/15/2022Test Comment9Bank FunJohn SmithTerry Handleemail@somewhere.comChileLATAMY
11
Sheet1
 
Upvote 0
Ok, the below should give you the described results:

VBA Code:
Option Explicit
Sub Show()

UserForm1.Show vbModeless

End Sub

Private Sub forBank_Search()
    Application.ErrorCheckingOptions.BackgroundChecking = False
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    Dim txtBank As String, txtAppd As String, txtDate As Date
    Dim lastRow As Long, i As Long
         
    If IsDate(UserForm1.txtDate.Value) = False Then     'Checks to see if the date value is in a valid date format.
        MsgBox "Verify that the date is valid"          'You could remove these four lines and make txtDate
        Exit Sub                                        'a string if your regions Date format doesn't work.
    End If
    
    txtBank = UserForm1.txtBankName.Value
    txtAppd = UserForm1.txtAppendComment.Value
    txtDate = UserForm1.txtDate.Value
    
    lastRow = Cells(Rows.Count, "D").End(xlUp).Row      'Find the lastRow
    
    For i = 2 To lastRow                                'Checks to see if the bank name exists and updates column C
        If Range("D" & i).Value = txtBank Then
            Range("C" & i).Value = Range("C" & i).Value & " - " & txtAppd & " - " & txtDate
        End If
    Next i

    Columns("C:C").EntireColumn.AutoFit
    
    Application.ErrorCheckingOptions.BackgroundChecking = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    
    UserForm1.Label4.Caption = "Process Complete"
    
End Sub

For the userform, I added 4 labels, 3 textboxes, and 3 commandbuttons. Below are the Names on the UserForm for the textboxes and buttons. If you change, you will need to change in the code. The labels captions can be anything.

Label1 - Bank Name
Label2 - Append Comment
Label3 - Date
Label4 - "---"

txtBankName, txtAppendComment, txtDate

btnAppend
btnClear
btnExit

Below is the code for the form:

VBA Code:
Private Sub btnAppend_Click()
Application.Run "Module1.forBank_Search"
End Sub

Private Sub btnClear_Click()
    txtBankName = ""
    txtAppendComment = ""
    txtDate = ""
    Label4 = "---"
End Sub

Private Sub btnExit_Click()
    Unload Me
End Sub
 

Attachments

  • forBank_Search.jpg
    forBank_Search.jpg
    100.9 KB · Views: 8
Upvote 0
Where to past the result?
suppose paste to range L11 and downwards:
VBA Code:
Option Explicit
Sub test()
Dim lr&, k&, f, fAddr As String, Bname As String, arr(1 To 6500, 1 To 1)
lr = Cells(Rows.Count, "D").End(xlUp).row
Bname = InputBox("Choose bank name:")
    Set f = Range("D1:D" & lr).Find(What:=Bname, after:=Range("D1"))
        If Not f Is Nothing Then
            fAddr = f.Address
            k = k + 1
            arr(k, 1) = f.Offset(0, -1) & vbNewLine & f.Offset(0, -2) & " Email sent on this date"
            Do
               Set f = Range("D1:D" & lr).FindNext(f)
               If Not f Is Nothing Then
                    k = k + 1
                    arr(k, 1) = f.Offset(0, -1) & vbNewLine & f.Offset(0, -2) & " Email sent on this date"
                End If
             Loop Until f.Address = fAddr
        End If
   Range("L11").Resize(k - 1, 1).value = arr
End Sub

Capture.JPG
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,877
Members
449,056
Latest member
ruhulaminappu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top