Results 1 to 8 of 8

Thread: 2 Excel Workbooks Match, and Copy

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2005
    Posts
    4

    2 Excel Workbooks Match, and Copy

    Currently, I have 2 excel workbooks.

    Workbook 1: contains 1 tab (worksheet) generated by a macro to formatt data and name the tab as the month (ie 1,2,3,).

    Workbook 2: contains summary tabs (worksheets) that draw on information from monthly data which are in tabs named by month (ie 1,2,3).

    Currently, I am copying and pasting from Workbook 1 into workbook 2 to correctly match the months.

    I would like a macro to be able to match the name of the tab in workbook 1 to one of the 12 months listed in workbook 2 and paste the information from workbook 1 to 2, into the matching tab.

    I would be greatful if someone could figure this out for me. I am still learning VBA. Thanks!

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: 2 Excel Workbooks Match, and Copy

    Moved.

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2005
    Posts
    4

    Re: 2 Excel Workbooks Match, and Copy

    May I ask what that means?

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: 2 Excel Workbooks Match, and Copy

    You posted this thread somewhere that wasn't really appropriate, Hack moved it here so that you should get more help.

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2005
    Posts
    4

    Re: 2 Excel Workbooks Match, and Copy

    Oh, ok, thank you. Still would enjoy some help on this problem.

  6. #6
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: 2 Excel Workbooks Match, and Copy

    Welcome to the forums.

    You should record a macro to do what you want, and then edit it. Copy and paste it into your VB program, and it will be easy to modify to suit your needs.

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2005
    Posts
    4

    Re: 2 Excel Workbooks Match, and Copy

    Thanks for the suggestion, I already tried to do that. The problem is I want the tab in the first workbook to match one of 12 tabs in the second, and when that match is made, have the data transfer over into the correct tab. The recorded macro only selects a particular tab, and does not provide any sort of if tab 1 = tab 2 then, but if not then look at tab 3, and if not that tab, look at 4 and so on to copy the data.

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: 2 Excel Workbooks Match, and Copy

    Right then, first of all sorry for the delay in posting a "proper" reply (I've been away from the forums for a few hours).

    I think something like this should work for you (I wasn't sure how you want the data copied, so I just put in a couple of examples):
    VB Code:
    1. Dim oTargetWorkBook As Workbook
    2.  
    3. Dim iCount As Integer
    4. Dim iTargetSheet As Integer
    5. Dim sSourceSheetName As String
    6.  
    7.   sSourceSheetName = UCase(Trim(ThisWorkbook.Sheets(1).Name))
    8.  
    9.        'Set an object to the target workbook
    10.        '(change [i]Book2[/i] here to the name of your target workbook)
    11.   Set oTargetWorkBook = Application.Workbooks("Book2")
    12.  
    13.   With oTargetWorkBook.Sheets
    14.  
    15.          'Loop through the sheets to to find the right one
    16.     iTargetSheet = -1
    17.     For iCount = 1 To .Count
    18.       If UCase(Trim(.Item(iCount).Name)) = sSourceSheetName Then
    19.         iTargetSheet = iCount
    20.         Exit For
    21.       End If
    22.     Next iCount
    23.    
    24.     If iTargetSheet = -1 Then
    25.          'Couldn't find it, show appropriate message
    26.       MsgBox "Couldnt find the target sheet!", vbCritical
    27.     Else
    28.       With .Item(iTargetSheet)
    29.          'We did find it, so do your pasting!
    30.         'example 1: set a single cell
    31.         .Range("D8") = "hello"
    32.    
    33.         'example 2: copy the all of source sheet to the bottom of target sheet
    34.         ThisWorkbook.Sheets(1).UsedRange.Copy _
    35.            .Range ("A" & (UsedRange.Rows.Count + 1))
    36.  
    37.       End With
    38.     End If
    39.  
    40.   End With
    41.  
    42.       'close reference to target workbook
    43.   Set oTargetWorkBook = Nothing
    If you don't understand any of it (or need help to copy as you want), feel free to ask

Posting Permissions

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



Click Here to Expand Forum to Full Width