Need a Find/Match Macro Desperately

copleyr

Active Member
Joined
Aug 24, 2009
Messages
381
Hello all:

I am in desperate need of efficiency with constantly updating files (1000's each week)

1) I need a macro that can look in column "A" of my "Index" worksheet.

2) I then need it to match the first 7 characters of each entry in column "A" with the file names in the folder "C:\Users\copleyr\Desktop\All/". All of the files in this folder are excel files, each with the same naming convention.

3) once it matches, I need it to copy the row of the matching 7 characters in the "Index" worksheet, columns "A" through "BM", and paste it in the "Defaults" tab, row "70" of the matching file in "C:\Users\copleyr\Desktop\All/" .


I have some pieces of macro that helps (below), from prior macros. I just need somebody to help me consolidate everything:

Code:
SummarySheet = ActiveWorkbook.Name
MyPathName = "C:\Users\copleyr\Desktop\All/" MyFileName = Dir(MyPathName)
X = 0
Do While MyFileName <> ""
    X = X + 1
    Workbooks.Open (MyPathName & MyFileName)
    Application.DisplayAlerts = False
    Sheets("Defaults").Range("A70:BM70").Copy
    Workbooks(SummarySheet).Activate
    Range("A" & X & ":BM" & X).PasteSpecial xlPasteValuesAndNumberFormats
    Range("A" & X & ":BM" & X).PasteSpecial xlPasteFormats
    Workbooks(MyFileName).Close False
    MyFileName = Dir
Application.DisplayAlerts = True
Loop
End Sub

Code:
Sub ListFiles()
Dim MyPathName As String
Dim MyFileName As String
Dim NumChars As Long
Dim X As Long

    NumChars = 7 'Change this to the number of characters you want to return
    MyPathName = "C:\Users\copleyr\Desktop\All/" 'Change this to the folder you want to return
    MyFileName = Dir(MyPathName)
    Do While MyFileName <> ""
        X = X + 1
        Sheet1.Cells(X, 1) = Left(MyFileName, NumChars)
        MyFileName = Dir
    Loop
End Sub


Can anybody point me in the right direction of getting started? Thank you so much for your help in advance!
 
Updated and tested code:

Code:
Sub UpdateFiles()
    Const MyPathName As String = "C:\Users\copleyr\Desktop\All\"

    Dim MyFileName As String
    Dim wb As Workbook
    Dim lRow As Long

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    With ThisWorkbook.Worksheets("Sheet1")

        For lRow = 2 To .Range("A" & .Rows.Count).End(xlUp).Row

            If Len(Trim(.Range("A" & lRow).Text)) Then

                Application.StatusBar = "Processing row " & lRow & "..."

                MyFileName = Dir(MyPathName & .Range("A" & lRow).Text & "*.xlsx")

                Do While Len(MyFileName) > 0

                    Set wb = Workbooks.Open(MyPathName & MyFileName, 0)

                    .Range("A" & lRow, "BN" & lRow).Copy Destination:=wb.Worksheets("Defaults").Range("A70")

                    wb.Close savechanges:=True

                    Set wb = Nothing

                    MyFileName = Dir

                Loop

            End If

        Next lRow

    End With

    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    Application.StatusBar = False

    MsgBox "Ready.", vbInformation, "Status"

End Sub
 
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Forum statistics

Threads
1,224,602
Messages
6,179,839
Members
452,948
Latest member
UsmanAli786

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