I am trying to take two ranges (one for column headings of a section and the other contains the values within those columns), replace any values in the column of Range 2 with the values in the column of Range 1.
Also, the output will be concatenated together with a delimiter of |. The two ranges should only have one row and both have the same number of columns.
I wrote a custom Excel function that I think should work, but it keeps giving me this error: "A value used in the formula is of the wrong data type". I keep looking over it and it all looks like I have the correct data types referencing one another.
Sample Values:
Function: ConcatenateRangeReplace(HeadingRange, ColumnValueRange, "|")
Headings: Heading 1, Heading 2, Heading 3, Heading 4, Heading 5
Column Values: Null, Null, x, Null, x
Desired Output: Heading 3|Heading 5
Code I have so far:
Any insight into this would be greatly appreciated.
Also, the output will be concatenated together with a delimiter of |. The two ranges should only have one row and both have the same number of columns.
I wrote a custom Excel function that I think should work, but it keeps giving me this error: "A value used in the formula is of the wrong data type". I keep looking over it and it all looks like I have the correct data types referencing one another.
Sample Values:
Function: ConcatenateRangeReplace(HeadingRange, ColumnValueRange, "|")
Headings: Heading 1, Heading 2, Heading 3, Heading 4, Heading 5
Column Values: Null, Null, x, Null, x
Desired Output: Heading 3|Heading 5
Code I have so far:
Code:
Function ConcatenateRangeReplace(Headings As Range, Parts As Range, Separator As String)
' Build a single string from a passed range with a
' passed separator between each value
' Replace values of range items with heading values in range
' (i.e. change an "x" to "Classification") - useful for bulk uploads to Expression Engine CMS Installations
Dim tempString, tempSeparator As String
Dim partsArray, headingsArray As Variant
Dim i As Long
tempString = ""
partsArray = Range(Parts).Value
headingsArray = Range(Headings).Value
' Check that both ranges have the same amount of rows and columns
If Len(partsArray) = Len(headingsArray) Then
' Check each part of the array for a value
For i = 1 To UBound(partsArray)
' Make sure no spaces pass as a null value
If partsArray(1, i) = " " Then
partsArray(1, i) = ""
End If
' Check if part is empty
If partsArray(1, i) = "" Or partsArray(1, i) = 0 Then
tempSeparator = ""
Else
tempSeparator = Separator
End If
If Len(tempString) = 0 Then
' Replace part with heading if part is not empty
If Not partsArray(1, i) = "" Then
tempString = headingsArray(1, i)
Else
tempString = partsArray(1, i)
End If
Else
' Replace part with heading if part is not empty
If Not partsArray(1, i) = "" Then
tempString = tempString & tempSeparator & headingsArray(1, i)
Else
tempString = tempString & tempSeparator & partsArray(1, i)
End If
End If
Next
ConcatenateRangeReplace = tempString
Else
MsgBox ("Both ranges must have only one row & the same number of columns")
End If
End Function