Consulting

Results 1 to 18 of 18

Thread: Macro code isn't working

  1. #1

    Macro code isn't working

    Hello

    I have been trying to use the code I found on this site that will Combine all data from all worksheets in all workbooks in a specified directory.
    [VBA]
    Option Explicit
    Dim Path As String 'string variable to hold the path to look through
    Dim FileName As String 'temporary filename string variable
    Dim tWB As Workbook 'temporary workbook (each in directory)
    Dim tWS As Worksheet 'temporary worksheet variable
    Dim mWB As Workbook 'master workbook
    Dim aWS As Worksheet 'active sheet in master workbook
    Dim RowCount As Long 'Rows used on master sheet
    Dim uRange As Range 'usedrange for each temporary sheet

    '***** Set folder to cycle through *****
    Path = ThisWorkbook.Path & "\subdirectory\" 'Change as needed, ie "C:\"

    Application.EnableEvents = False 'turn off events
    Application.ScreenUpdating = False 'turn off screen updating
    Set mWB = Workbooks.Add(1) 'create a new one-worksheet workbook
    Set aWS = mWB.ActiveSheet 'set active sheet variable to only sheet in mWB
    If Right(Path, 1) <> Application.PathSeparator Then 'if path doesnt end in "\"
    Path = Path & Application.PathSeparator 'add "\"
    End If
    FileName = Dir(Path & "*.xls", vbNormal) 'set first file's name to filename variable
    Do Until FileName = "" 'loop until all files have been parsed
    If Path <> ThisWorkbook.Path Or FileName <> ThisWorkbook.Name Then
    Set tWB = Workbooks.Open(FileName:=Path & FileName) 'open file, set to tWB variable
    For Each tWS In tWB.Worksheets 'loop through each sheet
    Set uRange = tWS.Range("A2", tWS.Cells(tWS.UsedRange.Row + tWS.UsedRange.Rows _
    .Count - 1, tWS.UsedRange.Column + tWS.UsedRange.Columns.Count - 1)) 'set used range
    If RowCount + uRange.Rows.Count > 65536 Then 'if the used range wont fit on the sheet
    aWS.Columns.AutoFit 'autofit mostly-used worksheet's columns
    Set aWS = mWB.Sheets.Add(After:=aWS) 'add a new sheet that will accommodate data
    RowCount = 0 'reset RowCount variable
    End If
    If RowCount = 0 Then 'if working with a new sheet
    aWS.Range("A1", aWS.Cells(1, uRange.Columns.Count)).Value = _
    tWS.Range("A1", tWS.Cells(1, uRange.Columns.Count)).Value 'copy headers from tWS
    RowCount = 1 'add one to rowcount
    End If
    aWS.Range("A" & RowCount + 1).Resize(uRange.Rows.Count, uRange.Columns.Count).Value _
    = uRange.Value 'move data from temp sheet to data sheet
    RowCount = RowCount + uRange.Rows.Count 'increase rowcount accordingly
    Next 'tWS
    tWB.Close False 'close temporary workbook without saving
    End If
    FileName = Dir() 'set next file's name to FileName variable
    Loop
    aWS.Columns.AutoFit 'autofit columns on last data sheet
    mWB.Sheets(1).Select 'select first data sheet on master workbook
    Application.EnableEvents = True 're-enable events
    Application.ScreenUpdating = True 'turn screen updating back on

    'Clear memory of the object variables
    Set tWB = Nothing
    Set tWS = Nothing
    Set mWB = Nothing
    Set aWS = Nothing
    Set uRange = Nothing
    End Sub
    [/VBA]
    When I try to run the macro I get an error code (number 52)
    I have workbooks that all have the same colums and headings and instead of me opening each one and cutting and pasting into a large master workbook I would like a macro that will go into the folder open each one and combine it. The master sheet needs to be updated once a week.

    Please help

    Edit: vba tags added. Jives, if you select your code and hit the vba button when posting it will format it as it is in the visual basic editor.

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Hi Jives,
    This works perfectly for me if you name the sub...
    notice the top of your sub above, it looks like this:
    [VBA]
    Option Explicit
    Dim Path As String 'string variable to hold the path to look through[/VBA]

    change it to look like this:
    [VBA]Option Explicit
    Sub combine()
    Dim Path As String 'string variable to hold the path to look through[/VBA]
    give your sub a useful name of your choice.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    It still isnt' working for me. This is what is says:


    [VBA]
    Option Explicit

    Sub CombineSheetsFromAllFilesInADirectory()
    Sub combine()
    Dim Path As String 'string variable to hold the path to look through
    Dim FileName As String 'temporary filename string variable
    Dim tWB As Workbook 'temporary workbook (each in directory)
    Dim tWS As Worksheet 'temporary worksheet variable
    Dim mWB As Workbook 'master workbook
    Dim aWS As Worksheet 'active sheet in master workbook
    Dim RowCount As Long 'Rows used on master sheet
    Dim uRange As Range 'usedrange for each temporary sheet

    '***** Set folder to cycle through *****
    Path = ThisWorkbook.Path & "\P:\Compiled Project Logs\" 'Change as needed, ie "C:\"

    Application.EnableEvents = False 'turn off events
    Application.ScreenUpdating = False 'turn off screen updating
    Set mWB = Workbooks.Add(1) 'create a new one-worksheet workbook
    Set aWS = mWB.ActiveSheet 'set active sheet variable to only sheet in mWB
    If Right(Path, 1) <> Application.PathSeparator Then 'if path doesnt end in "\"
    Path = Path & Application.PathSeparator 'add "\"
    End If
    FileName = Dir(Path & "*.xls", vbNormal) 'set first file's name to filename variable
    Do Until FileName = "" 'loop until all files have been parsed
    If Path <> ThisWorkbook.Path Or FileName <> ThisWorkbook.Name Then
    Set tWB = Workbooks.Open(FileName:=Path & FileName) 'open file, set to tWB variable
    For Each tWS In tWB.Worksheets 'loop through each sheet
    Set uRange = tWS.Range("A2", tWS.Cells(tWS.UsedRange.Row + tWS.UsedRange.Rows _
    .Count - 1, tWS.UsedRange.Column + tWS.UsedRange.Columns.Count - 1)) 'set used range
    If RowCount + uRange.Rows.Count > 65536 Then 'if the used range wont fit on the sheet
    aWS.Columns.AutoFit 'autofit mostly-used worksheet's columns
    Set aWS = mWB.Sheets.Add(After:=aWS) 'add a new sheet that will accommodate data
    RowCount = 0 'reset RowCount variable
    End If
    If RowCount = 0 Then 'if working with a new sheet
    aWS.Range("A1", aWS.Cells(1, uRange.Columns.Count)).Value = _
    tWS.Range("A1", tWS.Cells(1, uRange.Columns.Count)).Value 'copy headers from tWS
    RowCount = 1 'add one to rowcount
    End If
    aWS.Range("A" & RowCount + 1).Resize(uRange.Rows.Count, uRange.Columns.Count).Value _
    = uRange.Value 'move data from temp sheet to data sheet
    RowCount = RowCount + uRange.Rows.Count 'increase rowcount accordingly
    Next 'tWS
    tWB.Close False 'close temporary workbook without saving
    End If
    FileName = Dir() 'set next file's name to FileName variable
    Loop
    aWS.Columns.AutoFit 'autofit columns on last data sheet
    mWB.Sheets(1).Select 'select first data sheet on master workbook
    Application.EnableEvents = True 're-enable events
    Application.ScreenUpdating = True 'turn screen updating back on

    'Clear memory of the object variables
    Set tWB = Nothing
    Set tWS = Nothing
    Set mWB = Nothing
    Set aWS = Nothing
    Set uRange = Nothing
    End Sub
    [/VBA]

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Now you have two opening statements:
    [VBA]Sub CombineSheetsFromAllFilesInADirectory()
    Sub combine()
    [/VBA]

    The first line starts with the word Sub
    followed by the name of the sub: Sub CombineSheetsFromAllFilesInADirectory
    you only need one of these but you have to have one.....

    I can't get the code to error out as you have mentioned ...error 52. Could you give more details on the error if fixing the opening statement doesn't help?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    Thank you very much for responding so quickly and accurately!! I removed on the doubles and tried to run it. This is what it now looks like:

    [VBA]
    Option Explicit

    Sub Combine()
    Dim Path As String 'string variable to hold the path to look through
    Dim FileName As String 'temporary filename string variable
    Dim tWB As Workbook 'temporary workbook (each in directory)
    Dim tWS As Worksheet 'temporary worksheet variable
    Dim mWB As Workbook 'master workbook
    Dim aWS As Worksheet 'active sheet in master workbook
    Dim RowCount As Long 'Rows used on master sheet
    Dim uRange As Range 'usedrange for each temporary sheet

    '***** Set folder to cycle through *****
    Path = ThisWorkbook.Path & "\P:\Compiled Project Logs\" 'Change as needed, ie "C:\"

    Application.EnableEvents = False 'turn off events
    Application.ScreenUpdating = False 'turn off screen updating
    Set mWB = Workbooks.Add(1) 'create a new one-worksheet workbook
    Set aWS = mWB.ActiveSheet 'set active sheet variable to only sheet in mWB
    If Right(Path, 1) <> Application.PathSeparator Then 'if path doesnt end in "\"
    Path = Path & Application.PathSeparator 'add "\"
    End If
    FileName = Dir(Path & "*.xls", vbNormal) 'set first file's name to filename variable
    Do Until FileName = "" 'loop until all files have been parsed
    If Path <> ThisWorkbook.Path Or FileName <> ThisWorkbook.Name Then
    Set tWB = Workbooks.Open(FileName:=Path & FileName) 'open file, set to tWB variable
    For Each tWS In tWB.Worksheets 'loop through each sheet
    Set uRange = tWS.Range("A2", tWS.Cells(tWS.UsedRange.Row + tWS.UsedRange.Rows _
    .Count - 1, tWS.UsedRange.Column + tWS.UsedRange.Columns.Count - 1)) 'set used range
    If RowCount + uRange.Rows.Count > 65536 Then 'if the used range wont fit on the sheet
    aWS.Columns.AutoFit 'autofit mostly-used worksheet's columns
    Set aWS = mWB.Sheets.Add(After:=aWS) 'add a new sheet that will accommodate data
    RowCount = 0 'reset RowCount variable
    End If
    If RowCount = 0 Then 'if working with a new sheet
    aWS.Range("A1", aWS.Cells(1, uRange.Columns.Count)).Value = _
    tWS.Range("A1", tWS.Cells(1, uRange.Columns.Count)).Value 'copy headers from tWS
    RowCount = 1 'add one to rowcount
    End If
    aWS.Range("A" & RowCount + 1).Resize(uRange.Rows.Count, uRange.Columns.Count).Value _
    = uRange.Value 'move data from temp sheet to data sheet
    RowCount = RowCount + uRange.Rows.Count 'increase rowcount accordingly
    Next 'tWS
    tWB.Close False 'close temporary workbook without saving
    End If
    FileName = Dir() 'set next file's name to FileName variable
    Loop
    aWS.Columns.AutoFit 'autofit columns on last data sheet
    mWB.Sheets(1).Select 'select first data sheet on master workbook
    Application.EnableEvents = True 're-enable events
    Application.ScreenUpdating = True 'turn screen updating back on

    'Clear memory of the object variables
    Set tWB = Nothing
    Set tWS = Nothing
    Set mWB = Nothing
    Set aWS = Nothing
    Set uRange = Nothing
    End Sub
    [/VBA]
    When I receive the error message, I try to debug and this is what is hightlishted as being "wrong"

    FileName = Dir(Path & "*.xls", vbNormal) 'set first file's name to filename variable


  6. #6
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    You can't have your path be this workbook path and hard code it...you must do one or the other....from your last post.
    [VBA]Path = ThisWorkbook.Path & "\P:\Compiled Project Logs\" 'Change as needed, ie "C:\"
    [/VBA]

    Try the line like this if your sure it's the correct path:
    [VBA]Path = "P:\Compiled Project Logs\"[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  7. #7
    I changed that part--thanks. I'm still getting an error code for this part;

    FileName = Dir(Path & "*.xls", vbNormal) 'set first file's name to filename variable

  8. #8
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    what's the error?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  9. #9
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Jives,
    When you post code, please highlight is and click the VBA button. This formats it, making it more readable.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Have you saved the workbook where you are running the code?

    [vba]
    If Path <> ThisWorkbook.Path Or FileName <> ThisWorkbook.Name Then
    [/vba]
    You have just changed Path in the preceding code by adding \, so generally Path <> ThisWorkbook.Path.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  11. #11
    This is what it currently looks like. I have saved the workbook where I'm trying to use the macro as MasterLog.xls
    The name of the folder where the other folders that I need the macro for is called Compiled Project Logs which is located on my computers P drive. In the body of the code it says"If Path <> ThisWorkbook.Path Or FileName <> ThisWorkbook.Name Then" What should that line be changed to? I'm sorry I'm still not getting it!


    [VBA]Option Explicit

    Sub Combine()
    Dim Path As String 'string variable to hold the path to look through
    Dim FileName As String 'temporary filename string variable
    Dim tWB As Workbook 'temporary workbook (each in directory)
    Dim tWS As Worksheet 'temporary worksheet variable
    Dim mWB As Workbook 'master workbook
    Dim aWS As Worksheet 'active sheet in master workbook
    Dim RowCount As Long 'Rows used on master sheet
    Dim uRange As Range 'usedrange for each temporary sheet

    '***** Set folder to cycle through *****
    Path = "P:\Compiled Project Logs\"

    Application.EnableEvents = False 'turn off events
    Application.ScreenUpdating = False 'turn off screen updating
    Set mWB = Workbooks.Add(1) 'create a new one-worksheet workbook
    Set aWS = mWB.ActiveSheet 'set active sheet variable to only sheet in mWB
    If Right(Path, 1) <> Application.PathSeparator Then 'if path doesnt end in "\"
    Path = Path & Application.PathSeparator 'add "\"
    End If
    FileName = Dir(Path & "*.xls", vbNormal) 'set first file's name to filename variable
    Do Until FileName = "" 'loop until all files have been parsed
    If Path <> ThisWorkbook.Path Or FileName <> ThisWorkbook.Name Then
    Set tWB = Workbooks.Open(FileName:=Path & FileName) 'open file, set to tWB variable
    For Each tWS In tWB.Worksheets 'loop through each sheet
    Set uRange = tWS.Range("A2", tWS.Cells(tWS.UsedRange.Row + tWS.UsedRange.Rows _
    .Count - 1, tWS.UsedRange.Column + tWS.UsedRange.Columns.Count - 1)) 'set used range
    If RowCount + uRange.Rows.Count > 65536 Then 'if the used range wont fit on the sheet
    aWS.Columns.AutoFit 'autofit mostly-used worksheet's columns
    Set aWS = mWB.Sheets.Add(After:=aWS) 'add a new sheet that will accommodate data
    RowCount = 0 'reset RowCount variable
    End If
    If RowCount = 0 Then 'if working with a new sheet
    aWS.Range("A1", aWS.Cells(1, uRange.Columns.Count)).Value = _
    tWS.Range("A1", tWS.Cells(1, uRange.Columns.Count)).Value 'copy headers from tWS
    RowCount = 1 'add one to rowcount
    End If
    aWS.Range("A" & RowCount + 1).Resize(uRange.Rows.Count, uRange.Columns.Count).Value _
    = uRange.Value 'move data from temp sheet to data sheet
    RowCount = RowCount + uRange.Rows.Count 'increase rowcount accordingly
    Next 'tWS
    tWB.Close False 'close temporary workbook without saving
    End If
    FileName = Dir() 'set next file's name to FileName variable
    Loop
    aWS.Columns.AutoFit 'autofit columns on last data sheet
    mWB.Sheets(1).Select 'select first data sheet on master workbook
    Application.EnableEvents = True 're-enable events
    Application.ScreenUpdating = True 'turn screen updating back on

    'Clear memory of the object variables
    Set tWB = Nothing
    Set tWS = Nothing
    Set mWB = Nothing
    Set aWS = Nothing
    Set uRange = Nothing
    End Sub
    [/VBA]

  12. #12
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Here you set Path to a string value, ending in \
    Path = "P:\Compiled Project Logs\"

    Application.EnableEvents = False 'turn off events
    Application.ScreenUpdating = False 'turn off screen updating
    Set mWB = Workbooks.Add(1) 'create a new one-worksheet workbook
    Set aWS = mWB.ActiveSheet 'set active sheet variable to only sheet in mWB

    Why examine Path? You've just set it to a value.
    If Right(Path, 1) <> Application.PathSeparator Then 'if path doesnt end in "\"
    Path = Path & Application.PathSeparator 'add "\"
    End If
    FileName = Dir(Path & "*.xls", vbNormal) 'set first file's name to filename variable
    Do Until FileName = "" 'loop until all files have been parsed

    ThisWorkbook.Path will not return the final \ so Path will never equal ThisWorkbook.Path and the code will always run

    If Path <> ThisWorkbook.Path Or FileName <> ThisWorkbook.Name Then
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  13. #13

    I'm really not this dense in other areas of my life

    I think I follow what your saying but I still don't understand the solution. Can you please pretend that I'm completly stupid and tell me what to take out or add? I would be ever so so grateful!!!

  14. #14
    I tried to run the macro again and this time I received a "run time error '1004' Application-defined or object-defined error"

    I hit the debug option and this is what it looks like
    [vba]Option Explicit

    Sub Combine()
    Dim Path As String 'string variable to hold the path to look through
    Dim FileName As String 'temporary filename string variable
    Dim tWB As Workbook 'temporary workbook (each in directory)
    Dim tWS As Worksheet 'temporary worksheet variable
    Dim mWB As Workbook 'master workbook
    Dim aWS As Worksheet 'active sheet in master workbook
    Dim RowCount As Long 'Rows used on master sheet
    Dim uRange As Range 'usedrange for each temporary sheet

    '***** Set folder to cycle through *****
    Path = "P:\Compiled Project Logs\"

    Application.EnableEvents = False 'turn off events
    Application.ScreenUpdating = False 'turn off screen updating
    Set mWB = Workbooks.Add(1) 'create a new one-worksheet workbook
    Set aWS = mWB.ActiveSheet 'set active sheet variable to only sheet in mWB
    If Right(Path, 1) <> Application.PathSeparator Then 'if path doesnt end in "\"
    Path = Path & Application.PathSeparator 'add "\"
    End If
    FileName = Dir(Path & "*.xls", vbNormal) 'set first file's name to filename variable
    Do Until FileName = "" 'loop until all files have been parsed
    If Path <> ThisWorkbook.Path Or FileName <> ThisWorkbook.Name Then
    Set tWB = Workbooks.Open(FileName:=Path & FileName) 'open file, set to tWB variable
    For Each tWS In tWB.Worksheets 'loop through each sheet
    Set uRange = tWS.Range("A2", tWS.Cells(tWS.UsedRange.Row + tWS.UsedRange.Rows _
    .Count - 1, tWS.UsedRange.Column + tWS.UsedRange.Columns.Count - 1)) 'set used range
    If RowCount + uRange.Rows.Count > 65536 Then 'if the used range wont fit on the sheet
    aWS.Columns.AutoFit 'autofit mostly-used worksheet's columns
    Set aWS = mWB.Sheets.Add(After:=aWS) 'add a new sheet that will accommodate data
    RowCount = 0 'reset RowCount variable
    End If
    If RowCount = 0 Then 'if working with a new sheet
    aWS.Range("A1", aWS.Cells(1, uRange.Columns.Count)).Value = _
    tWS.Range("A1", tWS.Cells(1, uRange.Columns.Count)).Value 'copy headers from tWS
    RowCount = 1 'add one to rowcount
    End If
    aWS.Range("A" & RowCount + 1).Resize(uRange.Rows.Count, uRange.Columns.Count).Value _
    = uRange.Value 'move data from temp sheet to data sheet
    RowCount = RowCount + uRange.Rows.Count 'increase rowcount accordingly
    Next 'tWS
    tWB.Close False 'close temporary workbook without saving
    End If
    FileName = Dir() 'set next file's name to FileName variable
    Loop
    aWS.Columns.AutoFit 'autofit columns on last data sheet
    mWB.Sheets(1).Select 'select first data sheet on master workbook
    Application.EnableEvents = True 're-enable events
    Application.ScreenUpdating = True 'turn screen updating back on

    'Clear memory of the object variables
    Set tWB = Nothing
    Set tWS = Nothing
    Set mWB = Nothing
    Set aWS = Nothing
    Set uRange = Nothing
    End Sub
    [/vba]

  15. #15
    Quote Originally Posted by jives@moii.c
    I tried to run the macro again and this time I received a "run time error '1004' Application-defined or object-defined error"

    I hit the debug option and this is what it looks like
    [vba]Option Explicit

    Sub Combine()
    Dim Path As String 'string variable to hold the path to look through
    Dim FileName As String 'temporary filename string variable
    Dim tWB As Workbook 'temporary workbook (each in directory)
    Dim tWS As Worksheet 'temporary worksheet variable
    Dim mWB As Workbook 'master workbook
    Dim aWS As Worksheet 'active sheet in master workbook
    Dim RowCount As Long 'Rows used on master sheet
    Dim uRange As Range 'usedrange for each temporary sheet

    '***** Set folder to cycle through *****
    Path = "P:\Compiled Project Logs\"

    Application.EnableEvents = False 'turn off events
    Application.ScreenUpdating = False 'turn off screen updating
    Set mWB = Workbooks.Add(1) 'create a new one-worksheet workbook
    Set aWS = mWB.ActiveSheet 'set active sheet variable to only sheet in mWB
    If Right(Path, 1) <> Application.PathSeparator Then 'if path doesnt end in "\"
    Path = Path & Application.PathSeparator 'add "\"
    End If
    FileName = Dir(Path & "*.xls", vbNormal) 'set first file's name to filename variable
    Do Until FileName = "" 'loop until all files have been parsed
    If Path <> ThisWorkbook.Path Or FileName <> ThisWorkbook.Name Then
    Set tWB = Workbooks.Open(FileName:=Path & FileName) 'open file, set to tWB variable
    For Each tWS In tWB.Worksheets 'loop through each sheet
    Set uRange = tWS.Range("A2", tWS.Cells(tWS.UsedRange.Row + tWS.UsedRange.Rows _
    .Count - 1, tWS.UsedRange.Column + tWS.UsedRange.Columns.Count - 1)) 'set used range
    If RowCount + uRange.Rows.Count > 65536 Then 'if the used range wont fit on the sheet
    aWS.Columns.AutoFit 'autofit mostly-used worksheet's columns
    Set aWS = mWB.Sheets.Add(After:=aWS) 'add a new sheet that will accommodate data
    RowCount = 0 'reset RowCount variable
    End If
    If RowCount = 0 Then 'if working with a new sheet
    aWS.Range("A1", aWS.Cells(1, uRange.Columns.Count)).Value = _
    tWS.Range("A1", tWS.Cells(1, uRange.Columns.Count)).Value 'copy headers from tWS
    RowCount = 1 'add one to rowcount
    End If
    aWS.Range("A" & RowCount + 1).Resize(uRange.Rows.Count, uRange.Columns.Count).Value _
    = uRange.Value 'move data from temp sheet to data sheet
    RowCount = RowCount + uRange.Rows.Count 'increase rowcount accordingly
    Next 'tWS
    tWB.Close False 'close temporary workbook without saving
    End If
    FileName = Dir() 'set next file's name to FileName variable
    Loop
    aWS.Columns.AutoFit 'autofit columns on last data sheet
    mWB.Sheets(1).Select 'select first data sheet on master workbook
    Application.EnableEvents = True 're-enable events
    Application.ScreenUpdating = True 'turn screen updating back on

    'Clear memory of the object variables
    Set tWB = Nothing
    Set tWS = Nothing
    Set mWB = Nothing
    Set aWS = Nothing
    Set uRange = Nothing
    End Sub
    [/vba]

  16. #16
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I don't see anything obvious. Can you post a workbook with data to test?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  17. #17

    Workbook that I'm trying to use the macro in

    Here ya go....

    Thank you!!

  18. #18
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Works perfectly for me....only thing I changed was the path so there you have it....if your getting a new blank sheet then your path is incorrect....it must be exact and it is capital sensitive....
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •