VBA to merge rows, below and above an specific cell value within the same column

krsk92

New Member
Joined
Jun 30, 2019
Messages
2
Hi there :)

I am new with VBA and I am trying to develop an excel macro for this topic, but I haven´t been able to find something that would meet my needs as a starting point, hence I am looking for some help to develop an excel macro that will merge rows, below and above an specific cell value within the same column

For example, I have the following set of data in column A:

A1-->Position name
A2-->Company name
A3-->Requeriments
A4-->AAAAA
A5-->BBBBB
A6-->CCCCC
A7-->Skills
A8-->XXXX
A9-->YYYY

The macro should look for the values in column A, and merge starting with the cell that contains "Requirements" and would finish above the cell that contains "Skills", as shown below:
A1-->Position name
A2-->Company name
A3-->Requeriments
AAAAA
BBBBB
CCCCC
A4-->Skills
XXXX
YYYY

Then the macro would loop through the other columns and do the same. Note that the other columns can have more rows of data that would need to be merged under requirements and skills.

Please let me know if you have any doubts, any help would be greatly appreciated!!

Regards,
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Test on a COPY of your worksheet
- VBA finds Requirements (not Requeriments) which I assume was a typo in your OP
- code below assumes Requirements and Skills can be found in EVERY column and that Requirements is ALWAYS in row 3


Excel 2016 (Windows) 32 bit
A
B
C
1
DirectorConsultantMarketeer
2
Company ACompany BCompany C
3
RequirementsRequirementsRequirements
4
AAA
5
BBB
6
CCC
7
SkillsDD
8
XEE
9
YFSkills
10
GX
11
HY
12
SkillsZ
13
X
14
Y
Sheet: Before

Excel 2016 (Windows) 32 bit
A
B
C
1
DirectorConsultantMarketeer
2
Company ACompany BCompany C
3
Requirements
A
B
C
Requirements
A
B
C
D
E
F
G
H
Requirements
A
B
C
D
E
4
Skills
X
Y
Skills
X
Y
Skills
X
Y
Z
Sheet: After

Code:
Sub MergeCellValues()
    Dim lastCell As Range, Require As Range, Skills As Range, cel As Range
    Dim R As String, S As String, c As Long
    Application.ScreenUpdating = False
    ActiveSheet.UsedRange.VerticalAlignment = xlTop

    For c = 1 To Cells(1, Columns.Count).End(xlToLeft).Column
        Set lastCell = Cells(Rows.Count, c).End(xlUp)
        Set Require = Columns(c).Find("Requirements", lookat:=xlWhole)
        Set Skills = Columns(c).Find("Skills", lookat:=xlWhole)
    
[COLOR=#006400]'string for requirements[/COLOR]
        For Each cel In Range(Require, Skills.Offset(-1))
            R = R & cel & Chr(10)
        Next
        Require = Left(R, Len(R) - 1)
[COLOR=#006400]'string for skills[/COLOR]
        For Each cel In Range(Skills, lastCell)
            S = S & cel & Chr(10)
        Next
        Require.Offset(1) = Left(S, Len(S) - 1)
        
        R = "": S = ""
    Next c

[COLOR=#006400]'clear rows 5 onwards[/COLOR]
    Cells(5, 1).Resize(Rows.Count - 4).EntireRow.Delete

End Sub
 
Upvote 0

Forum statistics

Threads
1,216,192
Messages
6,129,434
Members
449,509
Latest member
ajbooisen

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