Concatenate macro based on merged cell value

agr22

New Member
Joined
Sep 20, 2017
Messages
2
Thanks for any help!

I am trying to speed up a process for my job where instead of copying and pasting all of the values into one field, I can run a macro to put it all into one field for me.

How the field comes to me is with a merged cell of "Responsibility / Duty:" and all the fields to the right need to be included. Then there could be a few rows in between and needs to have all of those values included as well.

[TABLE="width: 175"]
<tbody>[TR]
[TD][/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]Responsibility / Duty:[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]Other field[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]Responsibility / Duty:[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]3[/TD]
[/TR]
</tbody>[/TABLE]



I've pulled a concatinate all cell values function, but it's selecting the range of cells that I'm unsure the best method of doing. Any help is appreciated!

PHP:
Function ConcatinateAllCellValuesInRange(sourceRange As Excel.Range) As String    
    Dim finalValue As String
    Dim cell As Excel.Range
    
    For Each cell In sourceRange.Cells        
        finalValue = finalValue + CStr(cell.Value) + Chr(10)            
    Next cell

    ConcatinateAllCellValuesInRange = finalValue
End Function
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
For any wondering how I did this... or for any tips on cleaning this up

Code:
Function ConcatinateAllCellValuesInRange(sourceRange As Excel.Range) As String    
    Dim finalValue As String
    Dim cell As Excel.Range


    For Each cell In sourceRange.Cells
        finalValue = finalValue + CStr(cell.Value) + Chr(10)
    Next cell


    ConcatinateAllCellValuesInRange = finalValue
    
End Function


Sub Combine()
'
' Combine Macro
'
    Dim myRange As Range        'testing to add a range
    
    Dim posWS As Worksheet      ' Position Description worksheet
    Dim resp As String          ' to set the "Responsibility / Duty:" field
    Dim respTotal As Integer    ' this value is the total number of times resp occurs
    Dim textToInput As String
    Dim offsetRange As Range
    
    Dim cell
    Dim c As Range
    


    ' setting the worksheet to the Position Description and finding all the occurances of "Responsibility / Duty:"
    Set posWS = ThisWorkbook.Sheets("Position Description")
    resp = "Responsibility / Duty:"
    respTotal = Application.CountIf(posWS.Range("A:A"), resp)




    'combines all of the fields with "Responsibility / Duty:" into one field
    If respTotal <> 0 Then ' only if there are responsibilites listed will it combine the values
        
        Dim iCount As Integer
        iCount = 0
        textToInput = ""
        
        
        With posWS.Range("A:A")
            Set c = .Find(resp, LookIn:=xlValues)
            
            If Not c Is Nothing Then
                Do  'the loop that looks for each of the resp cells
                    iCount = iCount + 1 'loops through until respTotal


                    'takes the merged cell and then offsetting them to the cells to the right (including each additional row)
                    Set offsetRange = posWS.Range(posWS.Range(c.MergeArea.Address).Offset(0, 1).Address & ":" & _
                        posWS.Range(c.MergeArea.Cells(c.MergeArea.Rows.Count, c.MergeArea.Columns.Count).Address).Offset(0, 1).Address)
                    textToInput = textToInput & ConcatinateAllCellValuesInRange(offsetRange)


                    'MsgBox "icount:" & iCount & Chr(10) & _
                        "range address: " & posWS.Range(c.MergeArea.Address).Address & Chr(10) & _
                        "offset address: " & offsetRange.Address & Chr(10) & _
                        "String: " & textToInput


                    Set c = .FindNext(c)
                    Loop While iCount <> respTotal
            End If
        End With


    End If
    
    'setting the value in the spreadsheet
    ActiveSheet.Range("B21").Value = textToInput


   
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,022
Latest member
RobertV1609

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