Open up workbooks from a list & match a cell value, then copy the data adjacent to that value.

Bagsy

Active Member
Joined
Feb 26, 2005
Messages
467
Office Version
  1. 365
Platform
  1. Windows
Can anybody help please.
I have a workbook with a worksheet called “Cert Data” on this sheet in column A I have a list of cert numbers, in column N on the same sheet I have a list of cert registers
The cert registers are in number order
Material Receipt & Traceability Register 01 Pipe
Material Receipt & Traceability Register 02 Section
Etc.
The cert numbers are also in number order, and all 01’s are in the 01 register 02’s in the 02 register ect.
01-1234
01-1235
02-1234 Etc
In column A sheet1 of the cert registers are the cert numbers, what I am trying to do is open up each register one at a time find the cert numbers from my list, then copy the information from columns B through to J and paste adjacent to the matching cert numbers in my list on the the "cert data" sheet. Then close that register and open the next one, then repeat.
The registers are opening fine (thanks to help from this board), but my code where it is meant to copy does not work it just skips past it, and opens the next workbook.
any help is always appreciated Code below
VBA Code:
Sub CopyFromAllRegisters()
'Application.DisplayAlerts = False
'Application.ScreenUpdating = False

Dim i As Integer
Dim c As Integer
Dim directory As String
Dim filename As String
Dim ws As Worksheet
Dim wb As Workbook
Dim wbk As Workbook

Set wb = ActiveWorkbook
Set ws = ThisWorkbook.Worksheets("Cert Data")

'define location of material registers
directory = "L:\MATERIALS\Material Certification\"

'-----------------------------------------------------------------code to open each material register in turn
For i = 2 To Cells(Rows.Count, "N").End(xlUp).Row 'N is the column name  where the filenames are stored

   'define filename of material registers
   filename = Dir(directory & ws.Range("N" & i).Value & ".xlsm")
   
   If filename <> "" Then 'check if material register does not exist
      Set wbk = Workbooks.Open(directory & filename, ReadOnly:=True) 'open material register
      
      '-----------------------------------------------------------------Need to add my code here
wb.Activate
Worksheets("Cert Data").Select
Range("A1").Select
Range("A1", Range("A1").End(xlDown)).Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes 'sorting the numbers in order to match the order the excel material registers

    For c = 2 To ws.Cells(ws.Rows.Count, 1).End(xlUp).Row 'counting the cert numbers we need to search for
    a = ws.Cells(c, 1).Value

wbk.Activate
    For J = 2 To ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
        If Worksheets(1).Cells(J, 1).Value = a Then     'If cert number (Row 1) in column A on the first sheet on the Material registers
        '                                               '= the cert number(Value a)in column 1 in worksheet "Cert Data" in the active workbook
                                                        'then copy cells 2 thru to 10, from the Material register to the Cert Data sheet on active workbook
                                                        'then continue seach every row to look for more matches
                                                        'then repeat on each workbook as it is opened.

            Worksheets(1).Cells(J, 2).Value = ws.Cells(i, 2).Value 'copying from the first sheet of the material register, after finding the cert number. to matching cert number on "cert data" sheet
            Worksheets(1).Cells(J, 3).Value = ws.Cells(i, 3).Value
            Worksheets(1).Cells(J, 4).Value = ws.Cells(i, 4).Value
            Worksheets(1).Cells(J, 5).Value = ws.Cells(i, 5).Value
            Worksheets(1).Cells(J, 6).Value = ws.Cells(i, 6).Value
            Worksheets(1).Cells(J, 7).Value = ws.Cells(i, 7).Value
            Worksheets(1).Cells(J, 8).Value = ws.Cells(i, 8).Value
            Worksheets(1).Cells(J, 9).Value = ws.Cells(i, 9).Value
            Worksheets(1).Cells(J, 10).Value = ws.Cells(i, 10).Value

        End If
    Next J

Next c
      
      ''-----------------------------------------------------------------to here
   End If
   
   wbk.Close (False)
Next i 'loop ends here and it will continue to last material register as it works down the list

'Application.DisplayAlerts = True
'Application.ScreenUpdating = True
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Can anybody please help me I use this code below which works great whilst searching on an individual cert register.
Sub CopyRows()

VBA Code:
    Dim ws As Worksheet
Dim ws2 As Worksheet

Set ws = Worksheets("Search Certs")
Set ws2 = Worksheets("01-PIPE")

    Dim a As String

    For i = 2 To ws2.Cells(ws2.Rows.Count, 1).End(xlUp).Row
    a = ws2.Cells(i, 1).Value

    For J = 2 To ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
        If ws.Cells(J, 1).Value = a Then 'If value in column 1 on  search certs sheet = value (Value a)in column 1 on 02-section sheet then copy columns 1 thru to 10

            ws.Cells(J, 2).Value = ws2.Cells(i, 2).Value 'offsetting columns & copying
            ws.Cells(J, 3).Value = ws2.Cells(i, 3).Value
            ws.Cells(J, 4).Value = ws2.Cells(i, 4).Value
            ws.Cells(J, 5).Value = ws2.Cells(i, 5).Value
            ws.Cells(J, 6).Value = ws2.Cells(i, 6).Value
            ws.Cells(J, 7).Value = ws2.Cells(i, 7).Value
            ws.Cells(J, 8).Value = ws2.Cells(i, 8).Value
            ws.Cells(J, 9).Value = ws2.Cells(i, 9).Value
            ws.Cells(J, 10).Value = ws2.Cells(i, 10).Value

        End If
    Next J

Next i
End Sub
I had hoped I could just modify it to allow me to expand this search to all registers by opening the cert registers one at a time and copying the data as above code from the first sheet in the workbooks as I opened them, the cert registers are all opening fine, its just the bit in the middle that isn't.

I have tried a number of things ( I think it is just not looking at the first sheet (column A) in the workbooks as I open them, but really not sure) as I can't get it to do anything to give me a clue with what's wrong. I really do not know how to even start to get anything working from this point, or in fact this code will actually work at all. I have tried to reference the workbook by using wbk on the line as below, but this made no difference.
VBA Code:
wbk.Worksheets(1).Cells(J, 2).Value = ws.Cells(i, 2).Value
Does anybody know what I have missed out, there are no errors it just doesn't do anything. Any help at all is appreciated, Full code below
VBA Code:
Sub CopyFromAllRegisters()
'Application.DisplayAlerts = False
'Application.ScreenUpdating = False

Dim i As Integer
Dim c As Integer
Dim directory As String
Dim filename As String
Dim ws As Worksheet
Dim wb As Workbook
Dim wbk As Workbook


Set wb = ActiveWorkbook
Set ws = ThisWorkbook.Worksheets("Cert Data")


'define location of material registers
directory = "L:\MATERIALS\Material Certification\"

'-----------------------------------------------------------------code to open each material register in turn
For i = 2 To Cells(Rows.Count, "N").End(xlUp).Row 'N is the column name  where the filenames are stored

   'define filename of material registers
   filename = Dir(directory & ws.Range("N" & i).Value & ".xlsm")
   
   If filename <> "" Then 'check if material register does not exist
      Set wbk = Workbooks.Open(directory & filename, ReadOnly:=True) 'open material register
      
      '-----------------------------------------------------------------Need to add my code here
wb.Activate
Worksheets("Cert Data").Select
Range("A1").Select
Range("A1", Range("A1").End(xlDown)).Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes 'sorting the numbers in order to match the order the excel material registers

    For c = 2 To ws.Cells(ws.Rows.Count, 1).End(xlUp).Row 'counting the cert numbers we need to search for
    a = ws.Cells(c, 1).Value

wbk.Activate
    For J = 2 To ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
        If wbk.Worksheets(1).Cells(J, 1).Value = a Then     'If cert number (Row 1) in column A on the first sheet on the Material registers
        '                                               '= the cert number(Value a)in column 1 in worksheet "Cert Data" in the active workbook
                                                        'then copy cells 2 thru to 10, from the Material register to the Cert Data sheet on active workbook
                                                        'then continue seach every row to look for more matches
                                                        'then repeat on each workbook as it is opened.
                                                        
            wbk.Worksheets(1).Cells(J, 2).Value = ws.Cells(i, 2).Value
            wbk.Worksheets(1).Cells(J, 3).Value = ws.Cells(i, 3).Value
            wbk.Worksheets(1).Cells(J, 4).Value = ws.Cells(i, 4).Value
            wbk.Worksheets(1).Cells(J, 5).Value = ws.Cells(i, 5).Value
            wbk.Worksheets(1).Cells(J, 6).Value = ws.Cells(i, 6).Value
            wbk.Worksheets(1).Cells(J, 7).Value = ws.Cells(i, 7).Value
            wbk.Worksheets(1).Cells(J, 8).Value = ws.Cells(i, 8).Value
            wbk.Worksheets(1).Cells(J, 9).Value = ws.Cells(i, 9).Value
            wbk.Worksheets(1).Cells(J, 10).Value = ws.Cells(i, 10).Value


        End If
    Next J

Next c
      
      ''-----------------------------------------------------------------to here
   End If
   
   wbk.Close (False)
Next i 'loop ends here and it will continue to last material register as it works down the list

'Application.DisplayAlerts = True
'Application.ScreenUpdating = True
End Sub
 
Upvote 0
In an effort to get this code to work I have changed some of the code and added four numbers from the 01-register in cells A2 – A5. The 01-register is the first one to be opened, so it should find these numbers straight away, as I step through the code it gets to these lines
VBA Code:
wbk.Activate
For J = 2 To wbk.Worksheets(1).Cells(ws.Rows.Count, 1).End(xlUp).Row
If wbk.Worksheets(1).Cells(J, 1).Value = a Then
Then it goes straight to this line
VBA Code:
Next J
As I continue stepping through the code it then goes to the copy part of the code and goes through as if it was working, but it does not copy the values over.
VBA Code:
wbk.Worksheets(1).Cells(J, 2).Value = ws.Cells(i, 2).Value
wbk.Worksheets(1).Cells(J, 3).Value = ws.Cells(i, 3).Value
wbk.Worksheets(1).Cells(J, 4).Value = ws.Cells(i, 4).Value
wbk.Worksheets(1).Cells(J, 5).Value = ws.Cells(i, 5).Value
wbk.Worksheets(1).Cells(J, 6).Value = ws.Cells(i, 6).Value
wbk.Worksheets(1).Cells(J, 7).Value = ws.Cells(i, 7).Value
wbk.Worksheets(1).Cells(J, 8).Value = ws.Cells(i, 8).Value
wbk.Worksheets(1).Cells(J, 9).Value = ws.Cells(i, 9).Value
wbk.Worksheets(1).Cells(J, 10).Value = ws.Cells(i, 10).Value
Then as I continue it goes to
VBA Code:
Next J
Then skips the copy code completely and continues to skip the remainder of the four times,
where it should step through the copy part of the code 4 times for the four cert numbers in A2 – A5 which I know are in the 01-register as I copied the top four numbers. Can anybody shed any light on where I have gone wrong please
 
Upvote 0
Still persevering with this
This would be easy I said, just modify the code slightly I said.

I have now changed tack just to see If I can get it to copy & paste just part of the row. I managed to get it to find a cert number from my list and then copy & paste into my other workbook, but the code only finds the top number on my list, it doesn’t seem to then go down the list to the next number then copy & paste information from that row, etc.
Something is wrong on these lines of code any advice at all.
VBA Code:
For c = 2 To ws.Cells(ws.Rows.Count, 1).End(xlUp).Row 'counting the cert numbers we need to search for
a = ws.Cells(c, 1).Value

wbk.Activate
For J = 2 To wbk.Worksheets(1).Cells(ws.Rows.Count, 1).End(xlUp).Row
If wbk.Worksheets(1).Cells(J, 1).Value = a Then
'If cert number (Row 1) in column A on the first sheet on the Material registers
'= the cert number(Value a)in column 1 in worksheet "Cert Data" in the active workbook
'then copy cells 2 thru to 10, from the Material register to the Cert Data sheet on active workbook
'then continue search every row to look for more matches
'then repeat on each workbook as it is opened.

Full code below, please excuse the mess trying anything I can think of between the add my code here lines
VBA Code:
'Option Explicit
Sub CopyFromAllRegisters()
'Application.DisplayAlerts = False
'Application.ScreenUpdating = False

Dim i As Integer
Dim c As Integer

Dim directory As String
Dim filename As String
Dim ws As Worksheet
Dim wb As Workbook
Dim wbk As Workbook

Dim a As String


Set wb = ActiveWorkbook
Set ws = ThisWorkbook.Worksheets("Cert Data")

'define location of material registers
directory = "L:\MATERIALS\Material Certification\"

'-----------------------------------------------------------------code to open each material register in turn
For i = 2 To Cells(Rows.Count, "N").End(xlUp).Row 'N is the column name  where the filenames are stored

   'define filename of material registers
   filename = Dir(directory & ws.Range("N" & i).Value & ".xlsm")
   
   If filename <> "" Then 'check if material register does not exist
      Set wbk = Workbooks.Open(directory & filename, ReadOnly:=True) 'open material register
      
      '-----------------------------------------------------------------Need to add my code here
wb.Activate
Worksheets("Cert Data").Select
Range("A1").Select
Range("A1", Range("A1").End(xlDown)).Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes 'sorting the numbers in order to match the order the excel material registers

    For c = 2 To ws.Cells(ws.Rows.Count, 1).End(xlUp).Row 'counting the cert numbers we need to search for
    a = ws.Cells(c, 1).Value


wbk.Activate
    For J = 2 To wbk.Worksheets(1).Cells(ws.Rows.Count, 1).End(xlUp).Row
        If wbk.Worksheets(1).Cells(J, 1).Value = a Then                 'If cert number (Row 1) in column A on the first sheet on the Material registers
                                                                        '= the cert number(Value a)in column 1 in worksheet "Cert Data" in the active workbook
                                                                        'then copy cells 2 thru to 10, from the Material register to the Cert Data sheet on active workbook
                                                                        'then continue search every row to look for more matches
                                                                        'then repeat on each workbook as it is opened.
                                                                        
                 wbk.Worksheets(1).Cells(J, 2).Copy
                 wb.Activate
                 wb.Sheets("Cert Data").Cells(i, 2).PasteSpecial Paste:=xlPasteValues
                 wbk.Activate
                                                                        
                                                                        
'            wbk.Worksheets(1).Cells(J, 2).Copy , wb.Sheets("Cert Data").Cells(i, 2).Paste
'            wbk.Worksheets(1).Cells(J, 3).Copy , wb.Sheets("Cert Data").Cells(i, 3).Paste
'            wbk.Worksheets(1).Cells(J, 4).Copy , wb.Sheets("Cert Data").Cells(i, 4).Paste
'            wbk.Worksheets(1).Cells(J, 5).Copy , wb.Sheets("Cert Data").Cells(i, 5).Paste
'            wbk.Worksheets(1).Cells(J, 6).Copy , wb.Sheets("Cert Data").Cells(i, 6).Paste
'            wbk.Worksheets(1).Cells(J, 7).Copy , wb.Sheets("Cert Data").Cells(i, 7).Paste
'            wbk.Worksheets(1).Cells(J, 8).Copy , wb.Sheets("Cert Data").Cells(i, 8).Paste
'            wbk.Worksheets(1).Cells(J, 9).Copy , wb.Sheets("Cert Data").Cells(i, 9).Paste
'            wbk.Worksheets(1).Cells(J, 10).Copy , wb.Sheets("Cert Data").Cells(i, 10).Paste
                                                        
'            wbk.Worksheets(1).Cells(J, 2).Value = ws.Cells(i, 2).Value
'            wbk.Worksheets(1).Cells(J, 3).Value = ws.Cells(i, 3).Value
'            wbk.Worksheets(1).Cells(J, 4).Value = ws.Cells(i, 4).Value
'            wbk.Worksheets(1).Cells(J, 5).Value = ws.Cells(i, 5).Value
'            wbk.Worksheets(1).Cells(J, 6).Value = ws.Cells(i, 6).Value
'            wbk.Worksheets(1).Cells(J, 7).Value = ws.Cells(i, 7).Value
'            wbk.Worksheets(1).Cells(J, 8).Value = ws.Cells(i, 8).Value
'            wbk.Worksheets(1).Cells(J, 9).Value = ws.Cells(i, 9).Value
'            wbk.Worksheets(1).Cells(J, 10).Value = ws.Cells(i, 10).Value

End If
 
    Next J

Next c
        
      ''-----------------------------------------------------------------to here
   End If
   
   wbk.Close (False)
Next i 'loop ends here and it will continue to last material register as it works down the list

'Application.DisplayAlerts = True
'Application.ScreenUpdating = True
End Sub
 
Upvote 0
OK Managed to get this working, couple of silly errors I found, then a great deal of luck got this to work.
I have attached the code in case it helps anybody else

How this works
I have a workbook with a worksheet called “Cert Data”
On this sheet in column A, I have a list of cert numbers (starting in row 2 header in row 1)
In column N on the same sheet, I have a list of names of cert registers (Excel workbooks) (starting in row 2 header in row 1) (The filepath for these is hardcoded in the macro)
The cert numbers are sorted A-Z (optional) I hoped this may speed things up a bit.
When you run the code it looks at the cert numbers in column A and opens each register in turn, looks for the cert numbers then copy's the relevant data for each cert number from the next 9 columns and pastes it adjacent to the relevant cert number on my sheet, named cert data.
Just this one request, if anybody can help speed this up a bit I would be grateful, but least it works

Merry Christmas Everybody
VBA Code:
Sub CopyFromAllRegisters()
Application.DisplayAlerts = False
Application.ScreenUpdating = False

Dim i As Integer
Dim c As Integer

Dim directory As String
Dim filename As String
Dim ws As Worksheet
Dim wb As ThisWorkbook
Dim wbk As Workbook

Dim a As String

Set wb = ActiveWorkbook
Set ws = ThisWorkbook.Worksheets("Cert Data")

'define location of material registers
directory = "L:\MATERIALS\Material Certification\"

'-----------------------------------------------------------------code to open each material register in turn
For i = 2 To Cells(Rows.Count, "N").End(xlUp).Row 'N is the column name  where the filenames are stored

   'define filename of material registers
   filename = Dir(directory & ws.Range("N" & i).Value & ".xlsm")
   
   If filename <> "" Then 'check if material register does not exist
      Set wbk = Workbooks.Open(directory & filename, ReadOnly:=True) 'open material register
      
      '-----------------------------------------------------------------Need to add my code here
wb.Activate
Worksheets("Cert Data").Select
Range("A1").Select
Range("A1", Range("A1").End(xlDown)).Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes 'sorting the numbers in order to match the order the excel material registers

       For c = 2 To ws.Cells(ws.Rows.Count, 1).End(xlUp).Row 'counting the cert numbers we need to search for
    a = ws.Cells(c, 1).Value

wbk.Activate
    For J = 2 To wbk.Worksheets(1).Cells(ws.Rows.Count, 1).End(xlUp).Row
        If wbk.Worksheets(1).Cells(J, 1).Value = a Then                 'If cert number (Row 1) in column A on the first sheet on the Material registers
                                                                        '= the cert number(Value a)in column 1 in worksheet "Cert Data" in the active workbook
                                                                        'then copy cells 2 thru to 10, from the Material register to the Cert Data sheet on active workbook
                                                                        'then continue search every row to look for more matches
                                                                        'then repeat on each workbook as it is opened.
                                                                        
                 wbk.Worksheets(1).Cells(J, 2).Copy
                 wb.Sheets("Cert Data").Cells(c, 2).PasteSpecial Paste:=xlPasteValues
                 wbk.Worksheets(1).Cells(J, 3).Copy
                 wb.Sheets("Cert Data").Cells(c, 3).PasteSpecial Paste:=xlPasteValues
                 wbk.Worksheets(1).Cells(J, 4).Copy
                 wb.Sheets("Cert Data").Cells(c, 4).PasteSpecial Paste:=xlPasteValues
                 wbk.Worksheets(1).Cells(J, 5).Copy
                 wb.Sheets("Cert Data").Cells(c, 5).PasteSpecial Paste:=xlPasteValues
                 wbk.Worksheets(1).Cells(J, 6).Copy
                 wb.Sheets("Cert Data").Cells(c, 6).PasteSpecial Paste:=xlPasteValues
                 wbk.Worksheets(1).Cells(J, 7).Copy
                 wb.Sheets("Cert Data").Cells(c, 7).PasteSpecial Paste:=xlPasteValues
                 wbk.Worksheets(1).Cells(J, 8).Copy
                 wb.Sheets("Cert Data").Cells(c, 8).PasteSpecial Paste:=xlPasteValues
                 wbk.Worksheets(1).Cells(J, 9).Copy
                 wb.Sheets("Cert Data").Cells(c, 9).PasteSpecial Paste:=xlPasteValues
                 wbk.Worksheets(1).Cells(J, 10).Copy
                 wb.Sheets("Cert Data").Cells(c, 10).PasteSpecial Paste:=xlPasteValues

End If

    Next J

 Next c
        
      ''-----------------------------------------------------------------to here
   End If
   
   wbk.Close (False)
Next i 'loop ends here and it will continue to last material register as it works down the list

Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,334
Members
452,636
Latest member
laura12345

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