VBA to Paste heading based on how many subheadings there are?

SleightOfHand

New Member
Joined
Jun 19, 2020
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hello,

I've got a question to the forum who have been ever so helpful whenever I've had a request.

I have a table that looks sort of like this:

Copy of Copy of Future States Input Template_v1.3.xlsm
DEF
5Project identification
6
7Project namea
8Project codeb
9Associated projects (list codes)c
10
11Principal FunderSelect one only
12
13X(y/n)
14Y(y/n)
15Z(y/n)
16XN(y/n)
17YN(y/n)
v2


You can see that under 'project identification' it has three headings; project name code and associated projects.

The next under funder has five sections.

While it is fine to create a macro to copy and paste general data (e.g. in the yellow cells), would there be an easy way to hardpaste those underlined headings into one merged/centred across cell dependant on how many subheadings there are. Sometimes these will be editted etc.

So, something like this

Copy of Copy of Future States Input Template_v1.3.xlsm
ABCDEFGH
1Project identificationPrincipal Funder
2Project nameProject codeAssociated projects (list codes)XYZXNYN
Log



Right now I can just copy and paste them into an individual cell each.

Cheers!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Try this macro. The new data will be placed in Sheet2. Change the sheet names (in red) to suit your needs.
Rich (BB code):
Sub TransposeData()
    Application.ScreenUpdating = False
    Dim LastRow As Long, srcWS As Worksheet, desWS As Worksheet
    Dim fnd1 As Long, fnd2 As Long, FR As Long, cnt As Long, FR2 As Long, cnt2 As Long
    Set srcWS = Sheets("Sheet1")
    Set desWS = Sheets("Sheet2")
    LastRow = srcWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    fnd1 = Range("D:D").Find("Project identification", LookIn:=xlValues, lookat:=xlWhole).Row
    fnd2 = Range("D:D").Find("Principal Funder", LookIn:=xlValues, lookat:=xlWhole).Row
    With srcWS.Range("D" & fnd1 + 2 & ":D" & LastRow).SpecialCells(xlCellTypeConstants).Areas
        FR = .Item(1).Row
        cnt = .Item(1).Cells.Count
    End With
    With srcWS.Range("D" & fnd2 + 2 & ":D" & LastRow).SpecialCells(xlCellTypeConstants).Areas
        FR2 = .Item(1).Row
        cnt2 = .Item(1).Cells.Count
    End With
    With desWS
        With .Range("A1")
            .Value = "Project identification"
            .Resize(, cnt).HorizontalAlignment = xlCenterAcrossSelection
        End With
        .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Resize(, cnt).Value = Application.Transpose(Range("D" & fnd1 + 2).Resize(cnt))
        With .Cells(1, cnt + 1)
            .Value = "Principal Funder"
            .Resize(, cnt2).HorizontalAlignment = xlCenterAcrossSelection
        End With
        .Cells(.Rows.Count, cnt + 1).End(xlUp).Offset(1).Resize(, cnt2).Value = Application.Transpose(Range("D" & fnd2 + 2).Resize(cnt2))
        .Columns.AutoFit
    End With
    Application.ScreenUpdating = True
End Sub
Please note that the macro does not merge any cells. It uses "xlCenterAcrossSelection" which achieves the same visual effect as merging. Excel macros and merged cells don't "get along" with each other most times.
 
Upvote 0
Solution
Try this macro. The new data will be placed in Sheet2. Change the sheet names (in red) to suit your needs.
Rich (BB code):
Sub TransposeData()
    Application.ScreenUpdating = False
    Dim LastRow As Long, srcWS As Worksheet, desWS As Worksheet
    Dim fnd1 As Long, fnd2 As Long, FR As Long, cnt As Long, FR2 As Long, cnt2 As Long
    Set srcWS = Sheets("Sheet1")
    Set desWS = Sheets("Sheet2")
    LastRow = srcWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    fnd1 = Range("D:D").Find("Project identification", LookIn:=xlValues, lookat:=xlWhole).Row
    fnd2 = Range("D:D").Find("Principal Funder", LookIn:=xlValues, lookat:=xlWhole).Row
    With srcWS.Range("D" & fnd1 + 2 & ":D" & LastRow).SpecialCells(xlCellTypeConstants).Areas
        FR = .Item(1).Row
        cnt = .Item(1).Cells.Count
    End With
    With srcWS.Range("D" & fnd2 + 2 & ":D" & LastRow).SpecialCells(xlCellTypeConstants).Areas
        FR2 = .Item(1).Row
        cnt2 = .Item(1).Cells.Count
    End With
    With desWS
        With .Range("A1")
            .Value = "Project identification"
            .Resize(, cnt).HorizontalAlignment = xlCenterAcrossSelection
        End With
        .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Resize(, cnt).Value = Application.Transpose(Range("D" & fnd1 + 2).Resize(cnt))
        With .Cells(1, cnt + 1)
            .Value = "Principal Funder"
            .Resize(, cnt2).HorizontalAlignment = xlCenterAcrossSelection
        End With
        .Cells(.Rows.Count, cnt + 1).End(xlUp).Offset(1).Resize(, cnt2).Value = Application.Transpose(Range("D" & fnd2 + 2).Resize(cnt2))
        .Columns.AutoFit
    End With
    Application.ScreenUpdating = True
End Sub
Please note that the macro does not merge any cells. It uses "xlCenterAcrossSelection" which achieves the same visual effect as merging. Excel macros and merged cells don't "get along" with each other most times.
This is excellent, thank you so much. The fact that you took the time to make this is so good of ya. Cheers!
 
Upvote 0

Forum statistics

Threads
1,215,055
Messages
6,122,902
Members
449,097
Latest member
dbomb1414

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