Delete duplicates in Excel spreadsheet - excel

I'm looking for some sort of macro that deletes duplicate words within cells in a spreadsheet.
For instance, if cell A1 = "John John" I would like my macro to delete the duplicate "John". In other words A1 will become "John".
I have found a set of code that I have tweaked to some degree to fit my needs:
Sub Remove_DupesInString()
'this loops through the specified range and erases duplicates
Dim starval As String
Dim finval As String
Dim strarray() As String
Dim x As Long
Dim y As Long
Dim k As Long
' step through each cell in range
For Each cell In Sheets(5).Range("D2:D6507")
Erase strarray ' erase array
finval = "" ' erase final value"
starval = cell.Value
strarray = Split(starval, " ") 'Seperator is space
'Step through length of string and look for duplicate
For rw = 0 To UBound(strarray)
For k = rw + 1 To UBound(strarray)
If Trim(strarray(k)) = Trim(strarray(rw)) Then
strarray(k) = "" 'if duplicate clear array value
End If
Next k
Next rw
' combine all value in string less duplicate
For x = 0 To UBound(strarray)
If strarray(x) <> "" Then
finval = finval & Trim(strarray(x)) & ", "
End If
Next x
' remove last space and comma
finval = Trim(finval)
finval = Left(finval, Len(finval) - 1)
' Replaces cells with new values
cell.Value = finval
Next cell
End Sub
This set of code is sensitive to blank spaces in each cell. If, in cell D2, I have "John John" and in cell D3 have "Mary" it will produce the following:
D2 = "John", D3 = "Mary"
It does not seem to work, however, if I have blank cells in the column I'm running my macro in. I have worked around this issue by sorting on the cells with data inside them and only running my macro in this range.
I've tried tweaking the code further by adding different If cases with isEmpty(). My initial thought was that the the code above would only be executed If not isEmpty() Then but I've had no luck here. I'm not quite sure what to put inside the isEmpty function. Any ideas?

As you say, your only problem appears to be the handling of cells which are empty. That can easily be overcome by simply not processing any cells where the cell doesn't contain a space:
Sub Remove_DupesInString()
'this loops through the specified range and erases duplicates
Dim starval As String
Dim finval As String
Dim strarray() As String
Dim x As Long
Dim y As Long
Dim k As Long
' step through each cell in range
For Each cell In Sheets(5).Range("D2:D6507")
finval = "" ' erase final value"
starval = cell.Value
strarray = Split(starval, " ") 'Seperator is space
If UBound(strarray) > LBound(strarray) Then 'i.e. there was a space
'Step through length of string and look for duplicate
For rw = 0 To UBound(strarray)
For k = rw + 1 To UBound(strarray)
If Trim(strarray(k)) = Trim(strarray(rw)) Then
strarray(k) = "" 'if duplicate clear array value
End If
Next k
Next rw
' combine all value in string less duplicate
For x = 0 To UBound(strarray)
If strarray(x) <> "" Then
finval = finval & Trim(strarray(x)) & ", "
End If
Next x
' remove last space and comma
finval = Trim(finval)
finval = Left(finval, Len(finval) - 1)
' Replaces cells with new values
cell.Value = finval
End If
Next cell
End Sub

Related

VBA Code to Concatenate strings from column if first integers, or first and third integers, in another column match

Alright, this is a very specific question. I have an excel macro written that takes a web URL, delimits it, transposes it, and then adds adjacent columns that describe the information in the originally transposed columns. Now, I need to add something to my macro that will loop through and check if the first character of one cell matches one of the first 4 characters of another cell. If it does, I need to concatenate strings from the descriptive columns to new cells. I'll illustrate this below:
3,435,201,0.5,%22type%25202%2520diabetes%22,0 Node type 2 diabetes
4,165,97,0.5,%22diet%22,0 Node diet
5,149,248,0.5,%22lack%2520of%2520exercise%22,2 Node lack of exercise
6,289,329,0.5,%22genetics%22,3 Node genetics
7,300,71,0.5,%22blood%2520pressure%2520%22,5 Node blood pressure
7,3,-7,1,0 Arrow +
4,3,-21,1,0 Arrow +
5,3,-22,1,0 Arrow +
6,3,-34,1,0 Arrow +
,7%5D Tail
I added color to make the concept of the problem more easily visualized. In row one of the first column, we see a red 3 that corresponds to 'type 2 diabetes'. In the fifth row of the first column, we see a blue 7 that corresponds to 'blood pressure'. These are both node objects, as the adjacent column signifies. In the sixth cell of the first column we see a blue 7 and a red 3. This indicates that an arrow (also signified by adjacent column) is connecting blood pressure to diabetes. In the next column over, we see an orange plus sign, which indicates this is a positive relationship.
The goal is to populate the next column over with "blood pressure + type diabetes", as I demonstrated in the image. So, I need some code to check the first characters in each node cell, and then compare them to the first 4 characters of each arrow cell. When an arrow that matches two of the nodes is found, I need the code to populate the row next to the + signs with a concatenated string comprised of the names of the nodes pertaining to that arrow, as well as the + sign between them (it's possible that it could also be a minus sign, but one isn't present in this example). Any pointers? I can't wrap my head around this. Edited to add Data
Here is the code of my current macro:
Sub Delimit_Transpose()
Cells.Replace What:="],[", Replacement:="#", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
ActiveCell.FormulaR1C1 = "=RIGHT(R[-1]C,LEN(R[-1]C)-36)"
Dim i As Long, strTxt As String
Dim startP As Range
Dim xRg As Range, yRg As Range
On Error Resume Next
Set xRg = Application.InputBox _
(Prompt:="Range Selection...", _
Title:="Delimit Transpose", Type:=8)
i = 1
Application.ScreenUpdating = False
For Each yRg In xRg
If i = 1 Then
strTxt = yRg.Text
i = 2
Else
strTxt = strTxt & "," & yRg.Text
End If
Next
Application.ScreenUpdating = True
Set startP = Application.InputBox _
(Prompt:="Paste Range...", _
Title:="Delimit Transpose", Type:=8)
ary = Split(strTxt, "#")
i = 1
Application.ScreenUpdating = False
For Each a In ary
startP(i, 1).Value = Replace(Replace(a, "[", ""), "]", "")
i = i + 1
Next a
i = 1
For Each a In ary
If Len(a) > 13 Then
startP.Offset(i - 1, 1).Value = "Node"
ElseIf Len(a) < 13 And Len(a) > 6 Then
startP.Offset(i - 1, 1).Value = "Arrow"
Else
startP.Offset(i - 1, 1).Value = "Tail"
End If
i = i + 1
Next a
Dim openPos As Integer
Dim closePos As Integer
Dim midBit As String
i = 1
n = 5
For Each a In ary
openPos = InStr(a, ",%22")
On Error Resume Next
closePos = InStr(a, "%22,")
On Error Resume Next
midBit = Mid(a, openPos + 1, closePos - openPos - 1)
On Error Resume Next
If openPos <> 0 And Len(midBit) > 0 Then
startP.Offset(i - 1, 2).Value = Replace(Replace(midBit, "%22", ""), "%2520", " ")
ElseIf Len(a) < 13 And InStr(a, "-") = 4 Then
startP.Offset(i - 1, 2).Value = "'-"
ElseIf Len(a) < 7 Then
startP.Offset(i - 1, 2).Value = " "
Else
startP.Offset(i - 1, 2).Value = "+"
End If
i = i + 1
n = n + 1
Next a
Application.ScreenUpdating = True
End Sub
This is my approach.
There's room for a lot of improvements, but is a rough code that should get you started.
Read the code's comments and adapt it to fit your needs.
EDIT: I updated the code to match the sample worksheet you uploaded, build the first column range dinamically, validate if commas appear in the first column cell so no error is raised.
As I said in the comments, it's better easier to debug if you call one procedure from the other, instead of merging them.
Code:
Option Explicit
Public Sub StoreConcatenate()
' Basic error handling
On Error GoTo CleanFail
' Define general parameters
Dim targetSheetName As String
targetSheetName = "Test space" ' Sheet holding the data
Dim firstColumnLetter As String
firstColumnLetter = "C" ' First column holding the numbers
Dim firstColumnStartRow As Long
firstColumnStartRow = 7
' With these three parameters we'll build the range address holding the first column dynamically
' Set reference to worksheet
Dim targetSheet As Worksheet
Set targetSheet = ThisWorkbook.Worksheets(targetSheetName)
' Find last row in column (Modify on what column)
Dim firstColumnlastRow As Long
firstColumnlastRow = targetSheet.Cells(targetSheet.Rows.Count, firstColumnLetter).End(xlUp).Row
' Build range of first column dinamically
Dim firstColumnRange As Range
Set firstColumnRange = targetSheet.Range(firstColumnLetter & firstColumnStartRow & ":" & firstColumnLetter & firstColumnlastRow)
' Loop through first column range cells
Dim valueCell As Range
For Each valueCell In firstColumnRange
' Check if cell contains "," in the second position in string
If InStr(valueCell.Value, ",") = 2 Then
' Store first digit of cell before ","
Dim firstDigit As Integer
firstDigit = Split(valueCell.Value, ",")(0)
' Check if cell contains "," in the fourth position in string
If InStr(3, valueCell.Value, ",") = 4 Then
' Store second digit of cell after ","
Dim secondDigit As Integer
secondDigit = Split(valueCell.Value, ",")(1)
End If
' Store second colum type
Dim secondColumnType As String
secondColumnType = valueCell.Offset(, 1).Value
' Store third column value
Dim thirdColumnValue As String
thirdColumnValue = valueCell.Offset(, 2).Value
' Store nodes values (first digit and second column type)
Select Case secondColumnType
Case "Node"
Dim nodeValues() As Variant
Dim nodeCounter As Long
ReDim Preserve nodeValues(nodeCounter)
nodeValues(nodeCounter) = Array(firstDigit, thirdColumnValue)
nodeCounter = nodeCounter + 1
Case "Arrow"
Dim matchedNodeFirstValue As String
Dim matchedNodeSecondValue As String
matchedNodeFirstValue = IsInArrayReturnItem(firstDigit, nodeValues)(1)
matchedNodeSecondValue = IsInArrayReturnItem(secondDigit, nodeValues)(1)
If matchedNodeFirstValue <> vbNullString And matchedNodeSecondValue <> vbNullString Then
valueCell.Offset(, 3).Value = matchedNodeFirstValue & Space(1) & thirdColumnValue & Space(1) & matchedNodeSecondValue
End If
End Select
End If
Next valueCell
CleanExit:
Exit Sub
CleanFail:
Debug.Print "Something went wrong: " & Err.Description
Resume CleanExit
End Sub
' Credits: https://stackoverflow.com/a/38268261/1521579
Public Function IsInArrayReturnItem(stringToBeFound As Integer, arr As Variant) As Variant
Dim i
For i = LBound(arr) To UBound(arr)
If arr(i)(0) = stringToBeFound Then
IsInArrayReturnItem = arr(i)
Exit Function
End If
Next i
IsInArrayReturnItem = Array(vbNullString, vbNullString)
End Function
Let me know if it works
It appears that you are concatenating the lookups based on the
first and second integers,
where the second column = "Arrow"
If that is the case, I suggest:
Read the data table into a VBA array for faster processing
I am assuming your data is ordered as you show it, with all the Node entries at the start.
if that is not the case, then loop twice -- once to find the Nodes, and second time to concatenate the Arrow data.
Read the diagnoses into a dictionary for fact lookup.
if column2 = "Arrow" then concatenate the lookups of the first and second integers
Write back the data
Note: As written, this will overwrite the original table destroying any formulas that might be there. If needed, you could easily modify it to only overwrite the necessary area.
Note2 Be sure to set a reference (under Tools/References) to Microsoft Scripting Runtime, or change the Dictionary declaration to late-binding.
Regular Module
'set reference to Microsoft Scripting Runtime
Option Explicit
Sub Dx()
Dim WS As Worksheet
Dim rngData As Range, c As Range, vData As Variant
Dim dDx As Dictionary
Dim I As Long, sKey As String, dxKeys As Variant
'Get the data range
Set WS = ThisWorkbook.Worksheets("sheet1")
With WS
'assume table starts in A1 and is three columns wide
Set rngData = .Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp)).Resize(columnsize:=3)
'read into variant array for faster processing
vData = rngData
End With
'create dictionsry for dx lookups
Set dDx = New Dictionary
For I = 2 To UBound(vData, 1)
Select Case vData(I, 2)
Case "Node"
sKey = Split(vData(I, 1), ",")(0) 'first comma-separated number
If dDx.Exists(sKey) Then
MsgBox "duplicate diagnostic key. Please correct the data"
Exit Sub
End If
dDx.Add Key:=sKey, Item:=vData(I, 3)
Case "Arrow"
dxKeys = Split(vData(I, 1), ",")
vData(I, 3) = dDx(dxKeys(0)) & " + " & dDx(dxKeys(1))
End Select
Next I
'reWrite the table
Application.ScreenUpdating = False
rngData = vData
End Sub

Increase all cells value that contains number and text

Hi I'm trying to build a macro that can search for cells with any value in and increase the numbers inside them by one.
all my cells have a text and numbers for e.g. ( Movie 1 , Movie 2 , Car )
each cell contains a name and a number .. the name might be one or two words or more.. the number is not always at the end and it's usually from 0 to 200 but not all of the cells have numbers.
Those cells are all over the sheet and I want the macro to search for anything that has value in it and separate the numbers from texts then increase the numbers by one.
after hours of trial and error I reached to this code :
Sub IncreaseCellValue()
Dim value As Variant
'Add 1 to the existing cell value
If IsNumeric(Range("A1").value) Then
Range("A1").value = Range("A1") + 1
Else
value = Split(Range("A1").value, " ")
Range("A1").value = value(0) & " " & (CInt(value(1)) + 1)
End If
End Sub
The problem now is this code can only be applied to one specified cell.
Since working directly with excel cells would slow down your executing time (when there are large number of cells to check), working with an array would be the key:
Option Explicit
Sub IncreaseCellValue()
Dim arr As Variant
'This will hold your whole worksheet. Change the sheet name
arr = ThisWorkbook.Sheets("SheetName").UsedRange.Value
Dim i As Long, j As Long
For i = 1 To UBound(arr) 'for every row
For j = 1 To UBound(arr, 2) 'for every column
Select Case True
Case arr(i, j) = vbNullString
Case arr(i, j) Like "*MyWord*" 'beware Like is Case Sensitive
Case Else
arr(i, j) = AddOne(arr(i, j))
End Select
Next j
Next i
'Paste you array back to the worksheet
ThisWorkbook.Sheets("SheetName").UsedRange.Value = arr
'Note this will paste only values, so if you have formulas they will disappear
End Sub
Private Function AddOne(Value As Variant) As Variant
Dim MySplit As Variant
MySplit = Split(Value, " ")
Dim i As Long
For i = LBound(MySplit) To UBound(MySplit)
If IsNumeric(MySplit(i)) Then
AddOne = AddOne & " " & MySplit(i) + 1
Else
AddOne = AddOne & " " & MySplit(i)
End If
Next i
AddOne = Trim(AddOne)
End Function

Deleting columns depending on position of characters in a string

I'm still new to VBA and couldn't find any solution using the search-function so far.
Problem:
I have (in this case ten sheets) a row with over 500 cells containing strings with 5-7 words.
Now I need to delete all columns where the searched word is not the last word,but the word is in all cells (in the row) at different positions in the string.
I tried this:
Dim examinee As Variant
Dim cell As Range
Dim word As String
Dim myCell As String
examinee = InputBox("How many sheets?")
word = InputBox("Looking for?")
For A = 1 To examinee
Sheets("sheet" & A).Select
On Error Resume Next
Range("A3", Range("A3").End(xlToRight)).Select
For Each Cell In Selection.Cells
myCell = Right(Cell, Len(Cell) - (InStrRev(Cell, " ") - 1))
MsgBox myCell ' just to be sure the word is found
If myCell Like word Then
Selection.Cells.Bold = False
Else
Delete.Column
End If
Next Cell
Next
I can find&identify the word and "If" works fine so far, just nothing happens to the selected cell and the column wasn't deleted.
With some changes I can only delete the entire row but it isn't what I need.
Any help appriciated.
Thx in advance.
This should work, but I'd recommend cleaning up the syntax. I've removed the code where you're selecting ranges (there's lots of info online about why you shouldn't do this).
An array is created to find the last word and that's tested against the search value.
Sub Test()
Dim examinee As Variant
Dim cell As Range
Dim word As String
Dim myCell As String
Dim arr() As String
Dim strLastWord As String
'How Many Sheets Should We Loop?
examinee = InputBox("How many sheets?")
'What Word Are We Searching For?
word = InputBox("Looking for?")
'Loop Sheets
For A = 1 To examinee
'Loop Cells In Row 3
For Each cell In Range("A3", Range("A3").End(xlToRight))
'Get The Value Of The Current Cell
myCell = Right(cell, Len(cell) - (InStrRev(cell, " ") - 1))
'Is It A Single Word?
If InStr(1, myCell, " ") Then
'Several Words. Create An Array Of Individual Words In The Cell
arr() = Split(myCell, " ")
'Get The Number Of The Last Word
strLastWord = arr(UBound(arr))
Else
'Single Word. Get The Word
strLastWord = myCell
End If
'Is The Last Word = The Search Word?
If strLastWord = word Then
'Yes. Make It Bold
cell.Font.Bold = True
Else
'No. Delete The Column
Columns(cell.Column).Delete
End If
Next cell
Next
End Sub
I think this will help you
Sub findtext()
Dim wsAkt As Worksheet
Dim i As Integer
Dim k As Integer
Dim x As Integer
Dim strWord As String
Dim intWord As Integer
'get the word
strWord = Application.InputBox("Looking for?")
'length of word
intWord = Len(strWord)
'loop through all worksheets
For i = 1 To ThisWorkbook.Worksheets.Count
'variable for selected worksheet
Set wsAkt = ThisWorkbook.Worksheets(i)
'get how many columns are in row 3
x = wsAkt.Cells(3, wsAkt.Columns.Count).End(xlToLeft).Column
'loop through row 3 columns
For k = 1 To x
'if last Word in cell = the word then it has to have the same length
If Right(wsAkt.Cells(3, k), intWord) <> strWord Then
'delete selected column
wsAkt.Columns(k).Delete
End If
Next k
Next i
End Sub
(Its not tested)

Split multiple cells into column

I have multiple cells with string like
for. ex cel A1
m2_10cm[0.10],m2_20cm[0.20],m2_5cm[0.05],m3[1.9]
and cell A2
m3_22[2.2],m3_19[1.9]
Hov can i split it to one column like
Column B
Cell 1 m2_10cm[0.10]
Cell 2 m2_20cm[0.20]
Cell 3 m2_5cm[0.05]
Cell 4 m3[1.9]
Cell 5 m3_22[2.2]
Cell 6 m3_19[1.9]
I will be apriciate for any help
Using VBA code in Excel:
note: google how to a command button on the sheet and paste this as the code.
Option Explicit
' note: vbNullString is the same as "" (empty string)
Const START_ROW = 1
Const SRC_COL = 1 ' column A
Const DST_COL = 2 ' column B
' this gets triggered when the button is pressed
Private Sub CommandButton1_Click()
' call the routine
Call Go
End Sub
Function Go()
' assume the button is on the sheet to be processed
Dim ws As Excel.Worksheet
Set ws = Excel.ActiveSheet
Dim srcRow As Integer ' current row being processed
Dim dstRow As Integer ' current row to put result in
srcRow = START_ROW: dstRow = START_ROW
' keep going while column 'A' is not blank
While ws.Cells(srcRow, SRC_COL) <> vbNullString
Call Split(ws, ws.Cells(srcRow, SRC_COL), dstRow)
srcRow = srcRow + 1
Wend
End Function
Sub Split(ws As Excel.Worksheet, srcStr As String, ByRef dstRow As Integer)
If (srcStr = vbNullString) Then
'remove comment if you want blanks at the end
' ex. Apple,Banana,
' will create 3 entries, notice the comma at the end
'ws.Cells(dstRow, DST_COL) = vbNullString
'dstRow = dstRow + 1
Exit Sub
endif
' find ","
Dim pos As Integer
pos = InStr(1, srcStr, ",")
If (pos = 0) Then
' no "," - put the whole string
ws.Cells(dstRow, DST_COL) = Trim(srcStr)
dstRow = dstRow + 1
Else
' has "," - put the left part of the string
' ex: apple,banana,carrot
' put "apple"
' continue processing "banana,carrot"
ws.Cells(dstRow, DST_COL) = Trim(Mid(srcStr, 1, pos - 1))
' move to next row and process the right of the string
dstRow = dstRow + 1
Call Split(ws, Mid(srcStr, pos + 1), dstRow)
End If
End Sub
Here is a solution where you need to have every other column hidden:
Say that you have the value in A1 then put in the following formulas:
B1: =IF(ISERR(FIND(",";A1;1));A1;LEFT(A1;FIND(",";A1;1)-1))
C1: =IF(ISERR(FIND(",";A1;1));"";RIGHT(A1;LEN(A1)-FIND(",";A1;1)))
B1 will then contain the first value in the list, C1 will contain the list minus the first value. Now you can copy these formulas to D1 and E1 and they will now look like
D1: =IF(ISERR(FIND(",";C1;1));C1;LEFT(C1;FIND(",";C1;1)-1))
E1: =IF(ISERR(FIND(",";C1;1));"";RIGHT(C1;LEN(C1)-FIND(",";C1;1)))
Now keep on copying this formulas for as long to the right as you need.
Once this is done you can hide all columns that contain the shortened list, starting with C, then E, etc.

Concatenate multiple ranges using vba

I have a number of ranges to concatenate independently and put the values of the concatenated ranges into different cells.
I want to:
concatenate values in Range A1:A10 and put the result in F1
then concatenate the Range B1:B10 and put the result in F2
then concatenate the Range C1:C10 and put the result in F3 etc.
The following macro concatenates range A1:A10 and then puts the results into F1 (which is what I want). However it also stores the information from the first concatenation into memory so that when it does the next concatenation, in cell F2 I get the concatenated results of F1 and F2 joined.
Sub concatenate()
Dim x As String
Dim Y As String
For m = 2 To 5
Y = Worksheets("Variables").Cells(m, 5).Value
'Above essentially has the range information e.g. a1:a10 in sheet variables
For Each Cell In Range("" & Y & "") 'i.e. range A1:A10
If Cell.Value = "" Then GoTo Line1 'this tells the macro to continue until a blank cell is reached
x = x & Cell.Value & "," 'this provides the concatenated cell value
Next
Line1:
ActiveCell.Value = x
ActiveCell.Offset(1, 0).Select
Next m
End Sub
Here is my ConcatenateRange. It allows you to add a seperator if you please. It is optimized to handle large ranges since it works by dumping the data in a variant array and working with it within VBA.
You would use it like this:
=ConcatenateRange(A1:A10)
The code:
Function ConcatenateRange(ByVal cell_range As range, _
Optional ByVal seperator As String) As String
Dim newString As String
Dim cellArray As Variant
Dim i As Long, j As Long
cellArray = cell_range.Value
For i = 1 To UBound(cellArray, 1)
For j = 1 To UBound(cellArray, 2)
If Len(cellArray(i, j)) <> 0 Then
newString = newString & (seperator & cellArray(i, j))
End If
Next
Next
If Len(newString) <> 0 Then
newString = Right$(newString, (Len(newString) - Len(seperator)))
End If
ConcatenateRange = newString
End Function
... I would do this very differently... Why not create a function along the lines of:
Function ConcatMe(Rng As Range) As String
Dim cl As Range
ConcatMe = ""
For Each cl In Rng
ConcatMe = ConcatMe & cl.Text
Next cl
End Function
And then just, for example, set F1 = ConcatMe(A1:A10) or, then write code to assign the function to the cells you want...
Or, as #KazJaw mentioned in his comment, just set x="" before re-looping.
Hope this helps
it is similar to the idea posted here already. However, I use a for each loop instead of an array setup with nested for loops.
Function ConcRange(ByRef myRange As Range, Optional ByVal Seperator As String = "")
ConcRange = vbNullString
Dim rngCell As Range
For Each rngCell In myRange
If ConcRange = vbNullString Then
If Not rngCell.Value = vbNullString Then
ConcRange = CStr(rngCell.Value)
End If
Else
If Not rngCell.Value = vbNullString Then
ConcRange = ConcRange & Seperator & CStr(rngCell.Value)
End If
End If
Next rngCell
End Function
This, I suppose would be faster than the array set up, as a new array is not created each time this function runs.
Right before Next m insert simple statement: x="" – KazimierzJawor Apr 8 '13 at 20:43
took me several minutes to notice this answer was under comments :p
Thanks for everything guys, for my purpose I have modified your suggestions and amended my code as it didn't quite fit into a neat function as I needed it to be more dynamic. See my code below. It does exactly what I need.
Sub concatenate()
Dim x As String
Dim Y As String
For Q = 1 To 10 'This provides a column reference to concatenate - Outer For statement
For T = 1 To 10 'This provides a rows reference to concatenate - Inner for statement
For Each Cell In Cells(T, Q) 'provides rows and column reference
If Cell.Value = "" Then GoTo Line1 'this tells the macro to continue until a blank cell is reached
x = x & Cell.Value & "," 'This provides the concatenated cell value and comma separator
Next ' this loops the range
Next T 'This is the inner loop which dynamically changes the number of rows to loop until a blank cell is reached
Line1:
On Error GoTo Terminate 'Terminates if there are less columns (max 10) to concatenate
ActiveCell.Value = Mid(x, 1, Len(x) - 1) 'This basically removes the last comma from the last concatenated cell e.g. you might get for a range 2,3,4, << this formula removes the last comma to
'give 2,3,4
ActiveCell.Offset(1, 0).Select 'Once the concatenated result is pasted into the cell this moves down to the next cell, e.g. from F1 to F2
x = "" 'The all important, clears x value after finishing concatenation for a range before moving on to another column and range
Next Q 'After one range is done the second column loop kicks in to tell the macro to move to the next column and begin concatenation range again
Terminate: 'error handler
End Sub
#Issun's solution doesn't accept output from a worksheet array formula as the argument for the 'cell_range' parameter. But a slight modification to #Issun's code fixes this. I also added a check that ignores each cell whose value is FALSE.
Function ConcatenateRange( _
ByVal cellArray As Variant, _
Optional ByVal seperator As String _
) As String
Dim cell As Range
Dim newString As String
Dim i As Long, j As Long
For i = 1 To UBound(cellArray, 1)
For j = 1 To UBound(cellArray, 2)
If Len(cellArray(i, j)) <> 0 Then
If (cellArray(i, j) <> False) Then
newString = newString & (seperator & cellArray(i, j))
End If
End If
Next
Next
If Len(newString) <> 0 Then
newString = Right$(newString, (Len(newString) - Len(seperator)))
End If
ConcatenateRange = newString
End Function
For example:
A B (<COL vROW)
------ ------ -----------------
one 1 3
two 1 4
three 2 5
four 2 6
Enter into cell C1 the formula below and press CTRL+ENTER to store the formula as an array formula:
{=ConcatenateRange(IF(B3:B6=1,A3:A6),CHAR(10))}
I was looking further to see if there is a better way of writing concatenate function and found this. It seems that we all have the same working principle for the function. So its ok.
But my function is different that it can take multiple parameters, in combination of ranges, texts and numbers.
I assume that a delimiter is mandatory, so if i don't need it i just put "" as the last parameter).
I also assume that blank cells are not to be skipped. That's the reason why i want the function to take multiple parameters, so i can easily omit those that that i don't want in the concatenation.
Example of use:
=JoinText(A1:D2,F1:I2,K1:L1,";")
You can also use together text and number among the parameters:
=JoinText(A1:D2,123,F1:I2,K1:L1,"PQR",";")
I'd love to hear any comments or suggestions where it can be improved.
Here is the code.
Public Function JoinText(ParamArray Parameters() As Variant) As String
Dim p As Integer, c As Integer, Delim As String
Delim = Parameters(UBound(Parameters))
For p = 0 To UBound(Parameters) - 1
If TypeName(Parameters(p)) = "Range" Then
For c = 1 To Parameters(p).Count
JoinText = JoinText & Delim & Parameters(p)(c)
Next c
Else
JoinText = JoinText & Delim & Parameters(p)
End If
Next p
JoinText = Replace(JoinText, Delim, "", , 1, vbBinaryCompare)
End Function
Function ConcatenateRange to concatenate all cells in range if they are not empty and empty "" string.
Function ConcatenateRange(cellRange As Range, Optional Delimiter As String) As String
Dim cel As Range, conStr As String
conStr = ""
If Delimiter <> "" Then
For Each cel In cellRange
If VarType(cel) <> vbEmpty And Trim(cel) <> "" Then conStr = conStr & cel & Delimiter
Next
ConcatenateRange = Left(conStr, Len(conStr) - Len(Delimiter))
Else
For Each cel In cellRange
If VarType(cel) <> vbEmpty And Trim(cel) <> "" Then conStr = conStr & cel
Next
ConcatenateRange = conStr
End If
End Function
Its very simple brother, Look out of the Excel. No need for all cumbersome formula or VBA.
Just copy all the cells that you need to concatenate and paste it in the notepad. Now just select the space between the lines/columns (it's a TAB space actually) and find and replace it.. Done.. All cells are concatenated. Now just copy and paste it in the column and just verify.. Thats it :) Enjoy.
I suggest you to use Notepad++ for this :) Koodos
Vimarsh
Ph. D. Plant Biotech.
/

Resources