+ Reply to Thread
Results 1 to 8 of 8

Adjust existing macro to pick up specific named worksheet and copy to named range

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-29-2013
    Location
    Oshawa
    MS-Off Ver
    Excel 2010
    Posts
    660

    Adjust existing macro to pick up specific named worksheet and copy to named range

    Hi everyone,

    I have an existing macro which works pretty well. I currently run through all workbooks in a folder, identifies worksheet names which match worksheet names in the master workbook and copies the data from the external workbook worksheet to the master workbook worksheet. What I'm trying to do is adjust the macro to do the following:

    1. run through each workbook in a folder
    2. if the workbook name equals a cell value in ThisWorkbook.Sheet1.range("B2:B50") then copy Worksheet("ABC") entire used range into Workbooks(2) with the worksheet that matches the external workbook name.
    3. move to the next cell in the range and repeat as above

    So for example, lets cell B2 in ThisWorkbook.Sheet1.range("B2:B50") = "Stuff"
    The macro will find workbook.name = "Stuff" and copy the used range in Workbooks("Stuff").Worksheets("ABC")
    The master will paste this used range into Workbooks(2).Worksheets("Stuff")
    Then move onto the cell B3 in ThisWorkBook.Sheet1.range("B2:B50")

    Below is the macro as I have it. It doesn't work with the adjustments I've tried to make:

    Option Explicit
    Sub Update_Summary()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim myPath As String, myFile As String, myExtension As String
    Dim FldrPicker As FileDialog
    Dim FilePicker As FileDialog
    Dim rng As Range
    Dim cell As Range
    
      Application.ScreenUpdating = False
      Application.EnableEvents = False
      Application.DisplayAlerts = False
      Application.Calculation = xlCalculationManual
      
        MsgBox ("Please select Summary File")
    
        myFile = Application.GetOpenFilename
    
        Workbooks.Open (myFile), UpdateLinks:=0
        
        Workbooks(1).Worksheets("Data").Range("D2").Value = ActiveWorkbook.Path
        
        Workbooks(1).Worksheets("Data").Range("D3").Value = ActiveWorkbook.Name
    
    rng = ThisWorkbook.Sheet1.Range("B2:B50")  
    Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
    
        With FldrPicker
          .title = "Select A Target Folder"
          .AllowMultiSelect = False
            If .Show <> -1 Then GoTo NextCode
            myPath = .SelectedItems(1) & "\"
        End With
    
    NextCode:
      myPath = myPath
      If myPath = "" Then GoTo ResetSettings
    
      myExtension = "*.xl*"
    
      myFile = Dir(myPath & myExtension)
    
      Do While myFile <> ""
          Set wb = Workbooks.Open(Filename:=myPath & myFile)
          
          For Each cell In rng
            On Error Resume Next
            If ExistBook(wb.Name) Then
                If wb(ws.Name) = "Rollup" Then
                    wb.Sheets("ABC").UsedRange.Copy
                    Workbooks(2).Sheets(cell.Name).Range("A1").PasteSpecial xlValues
                End If
            End If
          On Error GoTo 0
          wb.Close savechanges:=False
          Next cell
    
          
          DoEvents
    
          myFile = Dir
      Loop
    
      MsgBox "Task Complete!"
    
    ResetSettings:
        Application.EnableEvents = True
        Application.Calculation = xlCalculationAutomatic
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
    
    End Sub
    Function ExistBook(sWs$, Optional sWb As Workbook) As Boolean
             If sWb Is Nothing Then Set sWb = ActiveWorkbook
             On Error Resume Next
             ExistBook = IsObject(sWb.Sheets(sWs))
    End Function
    Any ideas?

    Thanks!!
    Last edited by Groovicles; 06-06-2017 at 11:11 AM.

  2. #2
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: Adjust existing macro to pick up specific named worksheet and copy to named range

    You could try this.
    It assumes that the "Master" has been saved in the same folder.
    Not tested so make sure to try with copies of your file(s)
    Sub Maybe()
    Dim file_Open As String, i As Long
    With Application
       .DisplayAlerts = False
       .ScreenUpdating = False
        For i = 2 To Cells(Rows.Count, 2).End(xlUp).Row
        file_Open = Dir(ThisWorkbook.Path & "\" & Cells(i, 2).Value & ".xl*")
            Workbooks.Open ThisWorkbook.Path & "\" & file_Open
               With ActiveWorkbook.Sheets("ABC")
                 UsedRange.Copy ThisWorkbook.Sheets(Cells(i, 2).Value).Cells(Rows.Count, 1).End(xlUp).Offset(1)
               End With
            Workbooks(file_Open).Close False
        file_Open = Dir
        Next i
        .DisplayAlerts = True
        .ScreenUpdating = True
      End With
    End Sub

  3. #3
    Forum Contributor
    Join Date
    07-29-2013
    Location
    Oshawa
    MS-Off Ver
    Excel 2010
    Posts
    660

    Re: Adjust existing macro to pick up specific named worksheet and copy to named range

    Hi jolivanes,

    Thanks for your code. I'm trying my best to understand it. The macro as I currently have it is housed in the ThisWorkbook. It then prompts the user to select the Summary file which becomes Workbooks(2). The macro then prompts the user to identify where the actual external files are located. The results in these external files are supposed to be copied into Workbooks(2). I'm not certain I see how that works in your code? Can you clarify?

  4. #4
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: Adjust existing macro to pick up specific named worksheet and copy to named range

    I thought I answered as per your explanation. I did not look at your code as it does not do what you want it to do.
    I'll have a look later when I have some time again.
    Sorry about that.

  5. #5
    Forum Contributor
    Join Date
    07-29-2013
    Location
    Oshawa
    MS-Off Ver
    Excel 2010
    Posts
    660

    Re: Adjust existing macro to pick up specific named worksheet and copy to named range

    So here's what I have now. Its close but not quite doing what I need.

    What its not doing right:

    1. Its only pulling opening the first workbook in the folder rather than looking for the file indicated in myFile.
    2. its also not looping through the list of cells in I
    3. Its not renaming the copied worksheet to the i value

    What it is doing right:

    1. Copying the correct worksheet from the newly opened workbook to the correct position in Workbooks(2)

    Option Explicit
    Sub Update_Summary()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim myPath As String, myFile As String, myExtension As String
    Dim FldrPicker As FileDialog
    Dim FilePicker As FileDialog
    Dim i As Long
    
      Application.ScreenUpdating = False
      Application.EnableEvents = False
      Application.DisplayAlerts = False
      Application.Calculation = xlCalculationManual
      
        MsgBox ("Please select Summary File")
    
        myFile = Application.GetOpenFilename
    
        Workbooks.Open (myFile), UpdateLinks:=0
        
        Workbooks(1).Worksheets("Data").Range("D2").Value = ActiveWorkbook.Path
        
        Workbooks(1).Worksheets("Data").Range("D3").Value = ActiveWorkbook.Name
    
      Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
    
        With FldrPicker
          .title = "Select A Target Folder"
          .AllowMultiSelect = False
            If .Show <> -1 Then GoTo NextCode
            myPath = .SelectedItems(1) & "\"
        End With
    
    NextCode:
      myPath = myPath
      If myPath = "" Then GoTo ResetSettings
    
      myExtension = "*.xl*"
    
      Do While myFile <> ""
          For i = 2 To Workbooks(1).Worksheets("Hier").Cells(Rows.Count, 5).End(xlUp).Row
          myFile = Dir(myPath & Cells(i, 5).Value & myExtension)
          MsgBox Workbooks(1).Worksheets("Hierarchy").Cells(Rows.Count, 5).End(xlUp).Value
          
          Set wb = Workbooks.Open(Filename:=myPath & myFile)
    
          wb.Worksheets("ABC").Copy after:=Workbooks(2).Sheets("BEN")
          ActiveSheet.Name = i
    
          On Error GoTo 0
          wb.Close savechanges:=False
          Next i
    
          
          DoEvents
    
          myFile = Dir
      Loop
    
      MsgBox "Task Complete!"
    
    ResetSettings:
        Application.EnableEvents = True
        Application.Calculation = xlCalculationAutomatic
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
    
    End Sub
    Any ideas?

    Thanks!

  6. #6
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: Adjust existing macro to pick up specific named worksheet and copy to named range

    I don't get your logic.
    Why would you "run through each workbook in a folder" to see if it's name is the same as in one of the cells in a range. (See Post #1, #1 and #2)
    Why not use the cells in the range to open that particular workbook?
    I can't make head or tails out of the code you have in Post #5 either.

    Do you want to open all the files in a range in your active (call it Master for now) workbook and copy the used range from a sheet named "ABC" in that opened workbook into your Master?
    I am terrible reading/understanding someone's code that does not work.
    Either explain properly what you want to achieve or maybe someone else understands the code.

  7. #7
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: Adjust existing macro to pick up specific named worksheet and copy to named range

    I tried to get your code from Post #5 to work but wasn't able to.
    Could you go through the motions and explain what should, or what you want to, happen at each line.

  8. #8
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: Adjust existing macro to pick up specific named worksheet and copy to named range

    Try this. It might be a start of what you're after.
    You have to change all the references like sheet names ans cells ranges as required.

    Sub Update_Summary()
    Dim wb1 As Workbook, wbArr, myPath As String, wb2 As String, i As Long, file_Open As String
    
    Application.ScreenUpdating = False
    
    Set wb1 = ThisWorkbook    '<----- The workbook with this code in it
    
    wbArr = Range("I2:I" & Cells(Rows.Count, "I").End(xlUp).Row).Value    '<----- Names of the files to be opened.
    
    myPath = ChooseFolder() & "\"
    
    MsgBox ("Please select Summary File")
        
    wb2 = Application.GetOpenFilename()
    Workbooks.Open (wb2), UpdateLinks:=0
    wb2 = Dir(wb2, vbDirectory)    '<----- Need Workbook name without the path
    
    For i = LBound(wbArr) To UBound(wbArr)    '<----- Go through all the workbooks one by one
    
    file_Open = Dir(myPath & wbArr(i, 1) & ".xl*")
        If Dir(myPath & wbArr(i, 1) & ".xl*") <> "" Then
            Workbooks.Open myPath & file_Open
                ActiveWorkbook.Sheets("Sheet1").Copy After:=Workbooks(wb2).Sheets(Sheets.Count)    '<----- Here it copies Sheet1. Needs changing. Also change Sheets.Count
            With ActiveSheet
                .Name = wbArr(i, 1) & " " & i    '<----- Might want to change the naming also
            End With
    
        Workbooks(file_Open).Close False
        file_Open = Dir
    
        End If
    
    Next i
    
    Application.ScreenUpdating = True
    End Sub
    Function ChooseFolder() As String
        Dim fldr As FileDialog
        Dim sItem As String
    
        Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
        With fldr
            .title = "Select a Folder"
            .AllowMultiSelect = False
            .InitialFileName = "C:\"
            If .Show <> -1 Then GoTo NextCode
            sItem = .SelectedItems(1)
        End With
    
    NextCode:
        ChooseFolder = sItem
        Set fldr = Nothing
    End Function

+ 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. [SOLVED] vba code to add modulable no of named worksheets after the last existing named sheet
    By JEAN1972 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-11-2017, 08:48 AM
  2. Copy values of all named range in wb1 to identically named ranges in wb2
    By JAMIAM in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-24-2016, 06:58 PM
  3. Copy specific cell(s) or named range from one workbook to another
    By gcoug in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-23-2012, 06:01 PM
  4. Replies: 3
    Last Post: 06-04-2011, 10:56 AM
  5. Adding a new row into an existing named range in a macro
    By topper in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-20-2009, 10:25 AM
  6. Need to adjust existing named range for dynamic charts
    By tbonejo in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-04-2006, 12:18 PM
  7. create named range specific to worksheet
    By beliavsky@aol.com in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-28-2005, 12:05 PM

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