Compare a list with sheetnames and on match do copy-transpose operations

Raghuveer20

New Member
Joined
Jan 26, 2014
Messages
16
Hi!

I have a problem. I have a Workbook named 'SalesData.xlsx' which contains two sheets 'Demand' and 'Availability'. Column A of these worksheets contains CUSTOMER names and remaining thirteen columns contain month-year data.

The following needs to be done with both sheets i.e. 'Demand' and 'Availability'.
I want to select a CUSTOMER name and check if it is available as a sheet in another workbook 'Summary.xlsm'. When it is available, I want to transpose-copy the month-year data of the CUSTOMER to a defined range (corresponding to month-year) in 'Summary.xlsm' as illustrated below.

Hope! someone has a solution.

Regards


'SalesData.xlsx': Contains Sheets 'Demand' and 'Availability'


Excel 2007
ABCDEFGHIJKLMN
1CUSTOMERSApr-14May-14Jun-14Jul-14Aug-14Sep-14Oct-14Nov-14Dec-14Jan-15Feb-15Mar-15Annual
2AAAA638736755629683834748850614933810
3BBBB922944538901869963892936931609557
4CCCC758918799676753840637563517660500
5DDDD536955775705692973557974533601700
6EEEE774592650842512843753848689954917
7FFFF724911594602869819693832942665900
8GGGG640902548908796892965501503810687
Demand



'Summary.xlsm': Contains Sheets 'AAAA', 'BBBB', 'CCCC' and so on


Excel 2007
ABCDE
1Month wise sales during the year 2014-15
2AAAA
3MonthSales
4DemandAvaila- bilitySurplus(+)/ Deficit(-)
5(Units)(Units)(Units)(%)
6Apr-14638
7May-14736
8Jun-14755
9Jul-14629
10Aug-14683
11Sep-14834
12Oct-14748
13Nov-14850
14Dec-14614
15Jan-15933
16Feb-15810
17Mar-15
18Annual9330-933-100.0
AAAA
 
Last edited:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
As illustrated above, I want to copy columns to right of AAAA from Demand sheet in SalesData.xlsx workbook to Demand Column in sheet AAAA of summary.xlsm workbook
 
Upvote 0
Hi! I have made the following code with help from some old posts on this site. It is working fine. But, is the code efficient enough? Any suggestions please.

Regards

Code:
Sub Summary()
Dim wb1 As Workbook, wb2 As Workbook, sh1 As Worksheet, sh2 As Worksheet
Dim ws As Worksheet
Dim lr As Long, c As Range, fLoc As Range, rng As Range, tLoc As Range


Set wb1 = Workbooks("SalesData.xlsx")
Set wb2 = Workbooks("Summary.xlsm")
Set sh1 = wb1.Sheets("Demand")
Set sh2 = wb1.Sheets("Availability")


lr = sh1.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = sh1.Range("A2:A" & lr)
For Each ws In wb2.Worksheets
  Set Found = rng.Find(ws.Name, , xlValues, xlWhole, , , False)
        If Not Found Is Nothing Then
            With ws
                Found.Offset(0, 1).Resize(1, 13).Copy
                ws.Range("B6:B18").Resize(13, 1).PasteSpecial Transpose:=True
            End With
            Set Found = Nothing
        End If
Next ws


lr = sh2.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = sh2.Range("A2:A" & lr)
For Each ws In wb2.Worksheets
  Set Found = rng.Find(ws.Name, , xlValues, xlWhole, , , False)
        If Not Found Is Nothing Then
            With ws
                Found.Offset(0, 1).Resize(1, 13).Copy
                ws.Range("C6:C18").Resize(13, 1).PasteSpecial Transpose:=True
            End With
            Set Found = Nothing
        End If
Next ws
End Sub
 
Upvote 0
I want to put the following code in a new sub and invoke the sub when needed. How do we do that.
Note: Here ws, sh1, wb2, A2:A, B6:B18 are the variables

Code:
lr = sh1.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = sh1.Range("A2:A" & lr)
For Each ws In wb2.Worksheets
  Set Found = rng.Find(ws.Name, , xlValues, xlWhole, , , False)
        If Not Found Is Nothing Then
            With ws
                Found.Offset(0, 1).Resize(1, 13).Copy
                ws.Range("B6:B18").Resize(13, 1).PasteSpecial Transpose:=True
            End With
            Set Found = Nothing
        End If
Next ws
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,720
Members
448,986
Latest member
andreguerra

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top