Two sheets- trying to do a transpose from Rows to Columns, there is a twist to this.

Dak2

New Member
Joined
Jun 28, 2020
Messages
9
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
VBA Code:
Sub headerLookup()

Dim ShtONE As Worksheet, ShtTWO As Worksheet
Dim shtONEHead As Range, shtTWOHead As Range
Dim headerONE As Range, headerTWO As Range

Set ShtONE = Sheets("Sheet1")
Set ShtTWO = Sheets("Sheet2")


Dim lr As Long
Dim lc As Long

'get all of the headers in the first sheet, in Column 1(Horizantal) to get 57 something rows

lr = ShtONE.Cells(Rows.Count, 1).End(xlUp).Row

Set shtONEHead = ShtONE.Range("A1", ShtONE.Cells(lr, 1))

'get all of the headers in second sheet, assuming in row 1
lc = ShtTWO.Cells(1, Columns.Count).End(xlToLeft).Column
Set shtTWOHead = ShtTWO.Range("A1", ShtTWO.Cells(1, lc))

'loop through Rows and find matching values on Columns then copy the value of the adjacent  cell and paste it on sheet2
For Each headerTWO In shtTWOHead
    For Each headerONE In shtONEHead
        If headerTWO.Value = headerONE.Value Then
            headerONE.Offset(0, 1).Copy
            headerTWO.Offset(1, 0).PasteSpecial xlPasteAll
            Application.CutCopyMode = False
  
        End If

        Next headerONE
        
Next headerTWO


End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Match the header on sheet2(Vertical Header-Columns) to header(Horizontal header - Rows), if a match is found then the adjacent cell value is copied and pasted on sheet2 at the correct/corresponding header. I have this code that does the job but only for the first 5 headers on sheet2. I don't know, or actually
not sure why this happens.

Any help is appreciated.
 
Upvote 0
I am not including all the headers here but I have about 57 Rows on the (Horizontal Headers) on sheet1, and it is reduced to 16 (Vertical Headers) on sheet2. If the two headers match then the data, on sheet1, to the left column of that matched cell to be copied to sheet2 to the corresponding vertical header. There will probably be a duplicate headers somewhere, but I don't think we need to worry about that for now.
Here is sheet1 Data:

Ta
DKPNAA
MfB
MfPNC
DpD
M-Time1 W
DDA - P
Ta
TADff
CsE
APC-
MfB
SeriesFPCAP, RHS
PackagingCut Tape (CT)
PStF
TyG
CaH
TolI
V-RJ
ESRD25
LiP2000
OTK
Ra-

And Here is what needs to look like at the end on sheet2:
DKPNAMfMfPNDpCsPStTyCaTolV-ROTS / DH-S(Max)MLSiMTP/c
ABCDEFGHIJKLMNOP

ANy help is much appreciated.
 
Upvote 0

Forum statistics

Threads
1,215,459
Messages
6,124,945
Members
449,198
Latest member
MhammadishaqKhan

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