+ Reply to Thread
Results 1 to 3 of 3

Find all specified cell values in directory of workbooks, paste text in offset column

Hybrid View

Telemanes Find all specified cell... 06-20-2020, 06:15 PM
AlphaFrog Re: Find all specified cell... 06-20-2020, 10:45 PM
Telemanes Re: Find all specified cell... 06-21-2020, 06:44 AM
  1. #1
    Registered User
    Join Date
    07-24-2017
    Location
    Stamford, UK
    MS-Off Ver
    2016
    Posts
    7

    Find all specified cell values in directory of workbooks, paste text in offset column

    Hi There,

    I was wondering if someone could please help me with my VBA code that is not working - The reason why is beyond my skill level. Having extensively looked to tweak other peoples code I am still not getting the result I need. I will try to be as concise as possible: I have some software that generates reports from a large database, generating multiple workbooks for each section, and multiple sheets within each for subsections (The only way the software will do this unfortunately). I have attached a pseudonym example of what I might get out of one spreadsheet.

    What I need to do is write some VBA code which will go through all the workbooks within a folder, find any instance of a customer ID (input box) and then change the value (Input box) of the cell 3 columns to the right (this is a fixed offset). As a pseudonym example, I might want to make a comment on all workbooks/worksheets that a specific person had an issue flag up with their account. This would mean anyone accessing these reports would have this helpful note. Each ID could appear in any selection of workbooks(and worksheets within each workbook)

    I copied some code from two different sources in the below example, but it only opens all the workbooks.

    Sub LoopThroughFiles()
        Dim myID As Variant
        myID = InputBox("Enter Customer ID")
        Dim myResponse As Variant
        meResponse = InputBox("Enter the comment to be entered in all instances")
        Dim xFd As FileDialog
        Dim xFdItem As Variant
        Dim xFileName As String
    'Select Folder containing workbooks
        Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
        If xFd.Show = -1 Then
            xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
            xFileName = Dir(xFdItem & "*.xlsx*")
            Do While xFileName <> ""
                With Workbooks.Open(xFdItem & xFileName)
                
    'What to do in each workbook
    Dim Sh As Worksheet
    Dim Loc As Range
    
    For Each Sh In ThisWorkbook.Worksheets
    With Sh.UsedRange
            Set Loc = .Cells.Find(What:=myID)
            If Not Loc Is Nothing Then
                Do Until Loc Is Nothing
                    Loc.Offset(0.3).Value = myResponse
    		ActiveWorkbook.Save
                    Set Loc = .FindNext(Loc)
                    Loop
                    End If
                    End With
                    Set Loc = Nothing
                    Next
    'End of routine within each workbook
    
                End With
                xFileName = Dir
            Loop
        End If
    End Sub
    Thank you so much in advance for any help with this
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Find all specified cell values in directory of workbooks, paste text in offset column

    Sub LoopThroughFiles()
        Dim myID       As String
        Dim myResponse As String
        Dim Sh         As Worksheet
        Dim Loc        As Range
        Dim FirstFound As String
        Dim counter    As Long
        
        myID = Application.InputBox("Enter Customer ID", "Customer ID", Type:=2)
        If myID = "False" Then Exit Sub 'user canceled
        myResponse= Application.InputBox("Enter the comment to be entered in all instances", "Comment", Type:=2)
        If myResponse= "False" Then Exit Sub 'user canceled
        
        Dim xFdItem   As String
        Dim xFileName As String
        'Select Folder containing workbooks
        With Application.FileDialog(msoFileDialogFolderPicker)
            If .Show = -1 Then
                xFdItem = .SelectedItems(1) & Application.PathSeparator
            Else: Exit Sub 'user canceled
            End If
        End With
        
        Application.ScreenUpdating = False
        xFileName = Dir(xFdItem & "*.xlsx")
        Do While xFileName <> ""
            With Workbooks.Open(xFdItem & xFileName)
                'What to do in each workbook
                For Each Sh In .Worksheets
                    Set Loc = Sh.UsedRange.Cells.Find(myID, , xlValues, xlWhole, 1, 1, 0)
                    If Not Loc Is Nothing Then
                        FirstFound = Loc.Address
                        Do
                            Loc.Offset(0, 3).Value = myResponse
                            counter = counter + 1
                            Set Loc = Sh.UsedRange.FindNext(Loc)
                        Loop Until Loc.Address = FirstFound
                    End If
                Next Sh
                'End of routine within each workbook
                .Close SaveChanges:=True
            End With
            xFileName = Dir
        Loop
        Application.ScreenUpdating = True
        MsgBox counter & " entries edited.", vbInformation, "ID: " & myID
        
    End Sub
    Last edited by AlphaFrog; 06-20-2020 at 11:04 PM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    07-24-2017
    Location
    Stamford, UK
    MS-Off Ver
    2016
    Posts
    7

    Re: Find all specified cell values in directory of workbooks, paste text in offset column

    Hi AlphaFrog,

    This is perfect, worked first time and also worked faster than I anticipated it would! Thank you so much for your help, and I hope it didn't take much time to alter what I had (I can see at least a bit of the original code was retained so it wasn't a complete restart thank goodness)

    Best wishes,
    Telemanes

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. VBA: Look for partial text in Column A from Column B and paste text offset Column A
    By coryspeth in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-07-2018, 05:53 AM
  2. copy multiple ranges, find next empty cell in first column and paste values as text
    By Zlatko.Pan in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-18-2018, 06:13 PM
  3. Replies: 1
    Last Post: 02-04-2014, 01:57 PM
  4. [SOLVED] Find text in column A then copy the cell to the right and paste it in sheet2
    By Alring in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-22-2013, 02:11 AM
  5. Loop to find a cell, then copy offset paste
    By mr.alexander in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-29-2013, 08:48 AM
  6. Replies: 0
    Last Post: 09-06-2012, 04:06 AM
  7. code to find text, offset 1 column and paste to new workbook not working
    By trillium in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-10-2011, 07:55 AM

Tags for this Thread

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