Looking for some help, it's been a while....

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Sorry, for some reason I posted the start of a message and didn't continue it...

So I am looking for a little help... this is what I need to do.

I have a Master workbook and a folder full of workbooks that contain charges for different customers.

I have named 3 cells in each of the charges workbooks ACCNTNAME, ACCNTNUM, AND TOTALCOST.

What I need to do from the master sheet is to loop through all the charges files in the directory and copy ONLY these specific named cells into the master sheet, starting at a certain cell down downwards so it would look like;

ACCNTNAME ACCNTNUM TOTALCOST
ACCNTNAME ACCNTNUM TOTALCOST

And repeat going down until it has completed the loop. I haven't touched vba in a while and am having a real issue lol.

Any advice would be appreciated.

Regards
 
Upvote 0
Hi cweggleto81,

Welcome to MrExcel!!

See how this goes:

Code:
Option Explicit
Sub Macro1()
    
    Dim objFSO As Object, objFolder As Object, objFile As Object
    Dim strFolderName As String
    Dim wb As Workbook
    Dim lngMyRow As Long
    
    Application.ScreenUpdating = False
   
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    strFolderName = "C:\" '<-Foler containing Excel files. Remember trailing "\". Change to suit.
   
    Set objFolder = objFSO.GetFolder(strFolderName)
   
    For Each objFile In objFolder.Files
        If InStr(objFSO.GetExtensionName(objFile.Name), "xls") > 0 Then 'Only interested in Excel files
            Set wb = Workbooks.Open(objFolder & objFile.Name)
            'Puts the data from the named ranges into the next available row in columns A, B abd C in 'Sheet1'. Change to suit if necessary.
            On Error Resume Next 'In case there's no data in 'Sheet1' manually set the variable
                lngMyRow = ThisWorkbook.Sheets("Sheet1").Range("A:C").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
                If lngMyRow = 0 Then
                    lngMyRow = 2 'Default initial output row number. Change to suit if necessary
                End If
            On Error GoTo 0
            With ThisWorkbook.Sheets("Sheet1")
                .Range("A" & lngMyRow) = wb.Names("ACCNTNAME").RefersToRange
                .Range("B" & lngMyRow) = wb.Names("ACCNTNUM").RefersToRange
                .Range("C" & lngMyRow) = wb.Names("TOTALCOST").RefersToRange
            End With
            wb.Close False
        End If
    Next objFile
    
    Application.ScreenUpdating = True
 
End Sub

Regards,

Robert
 
Upvote 0
Keep getting this error when I try and run the macro, have set the path correctly and used the trailing \

"U:\New Charges\New Account SheetsFeel Unique.xlsx' cannot be found. Check your spelling, or try a different path.

Now I would assume this is because we are missing a \ between Sheets and Feel but the trailing \ IS in the code... confused :)
 
Upvote 0
So you need to set the strFolderName variable like so:

strFolderName = "U:\New Charges\New Account Sheets"
 
Upvote 0
Actually should be:

strFolderName = "U:\New Charges\New Account Sheets\"
 
Upvote 0
Hi,
The code does read strFolderName = "U:\New Charges\New Account Sheets" but I still get the same error. Any idea..
 
Upvote 0
Sorry, probably better I post the whole code.

Code:
Option Explicit
Sub Macro1()
    
    Dim objFSO As Object, objFolder As Object, objFile As Object
    Dim strFolderName As String
    Dim wb As Workbook
    Dim lngMyRow As Long
    
    Application.ScreenUpdating = False
   
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    strFolderName = "U:\New Charges\New Account Sheets\" '<-Foler containing Excel files. Remember trailing "\". Change to suit.
   
    Set objFolder = objFSO.GetFolder(strFolderName)
   
    For Each objFile In objFolder.Files
        If InStr(objFSO.GetExtensionName(objFile.Name), "xlsx") > 0 Then 'Only interested in Excel files
            Set wb = Workbooks.Open(objFolder & objFile.Name)
            'Puts the data from the named ranges into the next available row in columns A, B abd C in 'Sheet1'. Change to suit if necessary.
            On Error Resume Next 'In case there's no data in 'Sheet1' manually set the variable
                lngMyRow = ThisWorkbook.Sheets("Sheet1").Range("A:C").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
                If lngMyRow = 0 Then
                    lngMyRow = 4 'Default initial output row number. Change to suit if necessary
                End If
            On Error GoTo 0
            With ThisWorkbook.Sheets("Sheet1")
                .Range("A" & lngMyRow) = wb.Names("ACCNTNAME").RefersToRange
                .Range("B" & lngMyRow) = wb.Names("ACCNTNUM").RefersToRange
                .Range("C" & lngMyRow) = wb.Names("TOTALCOST").RefersToRange
            End With
            wb.Close False
        End If
    Next objFile
    
    Application.ScreenUpdating = True
 
End Sub

So confused right now because it should work!
 
Upvote 0
Try an extra trailing backslash \ (see post 6).
 
Last edited:
Upvote 0
Code:
Option Explicit
Sub Macro1()
    
    Dim objFSO As Object, objFolder As Object, objFile As Object
    Dim strFolderName As String
    Dim wb As Workbook
    Dim lngMyRow As Long
    
    Application.ScreenUpdating = False
   
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    strFolderName = "U:\New Charges\New Account Sheets\" '<-Foler containing Excel files. Remember trailing "\". Change to suit.
   
    Set objFolder = objFSO.GetFolder(strFolderName)
   
    For Each objFile In objFolder.Files
        If InStr(objFSO.GetExtensionName(objFile.Name), "xlsx") > 0 Then 'Only interested in Excel files
            Set wb = Workbooks.Open(objFolder & objFile.Name)
            'Puts the data from the named ranges into the next available row in columns A, B abd C in 'Sheet1'. Change to suit if necessary.
            On Error Resume Next 'In case there's no data in 'Sheet1' manually set the variable
                lngMyRow = ThisWorkbook.Sheets("Sheet1").Range("A:C").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
                If lngMyRow = 0 Then
                    lngMyRow = 4 'Default initial output row number. Change to suit if necessary
                End If
            On Error GoTo 0
            With ThisWorkbook.Sheets("Sheet1")
                .Range("A" & lngMyRow) = wb.Names("ACCNTNAME").RefersToRange
                .Range("B" & lngMyRow) = wb.Names("ACCNTNUM").RefersToRange
                .Range("C" & lngMyRow) = wb.Names("TOTALCOST").RefersToRange
            End With
            wb.Close False
        End If
    Next objFile
    
    Application.ScreenUpdating = True
 
End Sub

This is what I have. I do have the extra trailing oblique.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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