+ Reply to Thread
Results 1 to 7 of 7

Looping sheets and copying from excel in another server. Please help.

  1. #1
    Registered User
    Join Date
    10-30-2012
    Location
    Iowa
    MS-Off Ver
    Excel 2007
    Posts
    30

    Looping sheets and copying from excel in another server. Please help.

    Hi all-

    I've been browsing this forum for something like this but I haven't come across something similar to mine. I'd like to loop through all the sheets and find words that matches the excel file on the server, and if it matches then it pulls and copies the "Input" sheet in the excel file that is on the server. My current code pulls in only one file. How do I make it so it loops through all sheets and matches the name? I hope this makes sense.
    PHP Code: 
    Sub CopyWIW()
       
       
    Dim WS As Worksheet
       Dim wbTarget            
    As Workbook 'WIW file
       Dim wbThis              As Workbook '
    Observe report
       Dim strName             
    As String   'name of the source sheet/ target workbook
        
       Set wbThis = ActiveWorkbook
        
       '
    get the active sheetname of the book
       strName 
    ActiveSheet.Name
        
       
    'open a workbook that has same name as the sheet name
       Set wbTarget = Workbooks.Open("\\ReliabilityEngineering\WarrantyImprovementWorkbooks\" & strName & ".xlsm")
        
       '
    select cell A1 on the target book
       wbTarget
    .Sheets("Input").Activate
       Range
    ("A5:Q5" Cells(Rows.Count"A").End(xlUp).Row).Copy
       
       wbThis
    .Activate
       
    'Range("A1").Select
       Range("A1").PasteSpecial
       
       '
    close the workbook
       wbTarget
    .Close

       
    'activate the source book again
       wbThis.Activate
        
       '
    clear memory
       Set wbTarget 
    Nothing
       Set wbThis 
    Nothing
        
    End Sub 

  2. #2
    Valued Forum Contributor rollis13's Avatar
    Join Date
    01-26-2012
    Location
    Cordenons
    MS-Off Ver
    Excel 2016 32bit - Win 11
    Posts
    917

    Re: Looping sheets and copying from excel in another server. Please help.

    These minor changes should do. Now you need to decide how to heap all the data.
    Please Login or Register  to view this content.
    Last edited by rollis13; 12-07-2012 at 05:55 PM.

  3. #3
    Registered User
    Join Date
    10-30-2012
    Location
    Iowa
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Looping sheets and copying from excel in another server. Please help.

    It loops through the sheets but it doesn't paste it into the matching sheet. It stays on the same sheet and contines to open a new excel file. What's missing? Below is my revised code. I would like the file ABC that I open to match my worksheet name ABC.

    PHP Code: 
    Option Explicit

    Sub AddSheets
    ()
    Dim SheetCnt As Integer
    Dim WS 
    As Worksheet
    Dim wbTarget            
    As Workbook 'WIW file
    Dim wbThis              As Workbook '
    Observe report
    Dim strName             
    As String   'name of the source sheet/ target workbook

        
        SheetCnt = Worksheets.Count
        
        Sheets.Add After:=Worksheets(SheetCnt)
        ActiveSheet.Name = "RW6000WIW"
        
        Sheets.Add After:=Worksheets(SheetCnt)
        ActiveSheet.Name = "7000MRWIW"
        
        Sheets.Add After:=Worksheets(SheetCnt)
        ActiveSheet.Name = "8000WWIW"
        
        Sheets.Add After:=Worksheets(SheetCnt)
        ActiveSheet.Name = "8000TWIW"
        
        Sheets.Add After:=Worksheets(SheetCnt)
        ActiveSheet.Name = "9000AGWIW"
        
        Sheets.Add After:=Worksheets(SheetCnt)
        ActiveSheet.Name = "9000TWIW"
        
        Sheets.Add After:=Worksheets(SheetCnt)
        ActiveSheet.Name = "9000SCWIW"

           
     Set wbThis = ActiveWorkbook
        
     For Each WS In wbThis.Worksheets        '
    <== added
          With WS

             
    If Right(WS.Name3) = "WIW" Then
            
    'get the active sheetname of the book
             strName = WS.Name                       '
    <== changed
                  
            
    'open a workbook that has same name as the sheet name
            Set wbTarget = Workbooks.Open("\\frxfileserv2\Engrng\ReliabilityEngineering\WarrantyImprovementWorkbooks\" & strName & ".xlsm", ReadOnly:=True)
            
            
            '
    select cell A1 on the target book
            wbTarget
    .Sheets("Input").Activate
            Range
    ("A5:Q5" Cells(Rows.Count"A").End(xlUp).Row).Copy
                 
            wbThis
    .Activate
            
    'Range("A1").Select
            Range("A1").PasteSpecial                '
    <== needs to be changed
                 
            
    'close the workbook
            wbTarget.Close
            End If
       End With
    Next WS                                 '
    <== added
        
    'activate the source book again
    ''wbThis.Activate
          
    ''clear memory
    ''Set wbThis = Nothing


    End Sub 

  4. #4
    Registered User
    Join Date
    10-30-2012
    Location
    Iowa
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Looping sheets and copying from excel in another server. Please help.

    Penny for your thoughts?

  5. #5
    Valued Forum Contributor rollis13's Avatar
    Join Date
    01-26-2012
    Location
    Cordenons
    MS-Off Ver
    Excel 2016 32bit - Win 11
    Posts
    917

    Re: Looping sheets and copying from excel in another server. Please help.

    This should do what you are now asking for:
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    10-30-2012
    Location
    Iowa
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Looping sheets and copying from excel in another server. Please help.

    This works great!!! Thanks!!!

  7. #7
    Valued Forum Contributor rollis13's Avatar
    Join Date
    01-26-2012
    Location
    Cordenons
    MS-Off Ver
    Excel 2016 32bit - Win 11
    Posts
    917

    Re: Looping sheets and copying from excel in another server. Please help.

    Glad being of some help .

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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