|
-
Sep 7th, 2005, 09:21 AM
#1
Thread Starter
New Member
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!
-
Sep 7th, 2005, 09:26 AM
#2
Re: 2 Excel Workbooks Match, and Copy
-
Sep 7th, 2005, 10:01 AM
#3
Thread Starter
New Member
Re: 2 Excel Workbooks Match, and Copy
May I ask what that means?
-
Sep 7th, 2005, 10:25 AM
#4
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.
-
Sep 7th, 2005, 10:42 AM
#5
Thread Starter
New Member
Re: 2 Excel Workbooks Match, and Copy
Oh, ok, thank you. Still would enjoy some help on this problem.
-
Sep 7th, 2005, 12:12 PM
#6
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.
-
Sep 7th, 2005, 12:30 PM
#7
Thread Starter
New Member
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.
-
Sep 7th, 2005, 08:28 PM
#8
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:
Dim oTargetWorkBook As Workbook
Dim iCount As Integer
Dim iTargetSheet As Integer
Dim sSourceSheetName As String
sSourceSheetName = UCase(Trim(ThisWorkbook.Sheets(1).Name))
'Set an object to the target workbook
'(change [i]Book2[/i] here to the name of your target workbook)
Set oTargetWorkBook = Application.Workbooks("Book2")
With oTargetWorkBook.Sheets
'Loop through the sheets to to find the right one
iTargetSheet = -1
For iCount = 1 To .Count
If UCase(Trim(.Item(iCount).Name)) = sSourceSheetName Then
iTargetSheet = iCount
Exit For
End If
Next iCount
If iTargetSheet = -1 Then
'Couldn't find it, show appropriate message
MsgBox "Couldnt find the target sheet!", vbCritical
Else
With .Item(iTargetSheet)
'We did find it, so do your pasting!
'example 1: set a single cell
.Range("D8") = "hello"
'example 2: copy the all of source sheet to the bottom of target sheet
ThisWorkbook.Sheets(1).UsedRange.Copy _
.Range ("A" & (UsedRange.Rows.Count + 1))
End With
End If
End With
'close reference to target workbook
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|