VBA: How to combine string from two different cells into one?

mtbthepro

Board Regular
Joined
Feb 22, 2017
Messages
91
Hi there,
I would like to know if there is a way to combine two strings together into one cell. I am talking about putting numbers together, there is a set of Account#which are on different lines and they are displayed like |Account#:12542-|5651256|" The pipe (|) is there to show the column line that runs in between them. I would like that line to disappear and the number after the "-" to be in the same cell as "Account#: " so end result should look something like "|Account#:12542-5651256|"
I know there is a way to do it which is called concatenate and I have tried it a couple of different ways but it doesn't work.

If you can help me with this I would greatly appreciate it.




example one
Code:
      For N = nlast To 1 Step -1
                        If ws.Cells(N, 1).Value Like "*ACCOUNT*" Then
                            ws.Cells(N, 1).Value = str1
                            ws.Rows(N).EntireRow.Font.Bold = True
                            ws.Rows(N).Offset(0, 1) = str2
                            ws.Rows(N) = CONCATENATE(str1 & str2)  
                        End If
                    Next N
example two
Code:
        st= .Cells(.Rows.Count, "A")
                    For i = i To 1 Step -1
                If (ws.Cells(i, 1).Value Like "*ACCOUNT*") Then
                    st1= RTrim(.Cells(i, "A") & .Cells(i, "B"))
                        End If
                    Next i
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Please show a screen shot or specify the exact data in each column. I believe they can be combined with formulas.
 
Upvote 0
if i understand this can be done with a formula your example in A1
|Account#:12542-|5651256|"

<colgroup><col></colgroup><tbody>
</tbody>

in B1

Code:
=SUBSTITUTE(A1,"-|","-",1)

returns

|Account#:12542-5651256|"

<colgroup><col></colgroup><tbody>
</tbody>
 
Upvote 0
if you want to do it in VBA with a macro

Code:
Option Explicit
Sub RemoveDelimit()

Dim c As Range
        For Each c In ActiveSheet.UsedRange.Columns("A").Cells
            c.Value = WorksheetFunction.Substitute(c.Value, "-|", "-", 1)

        Next c

End Sub
 
Upvote 0
Department 14 Youth Sports
MEMBER NUMBERMEMBER NAMETRN DATE0 -30 DAYS31-60 DAYS61-90 DAYSOVER 90 DAYSPAST DUE
ACCOUNT#: 1-026-14-00000-3345
026-000180284-02example, example1/21/201715.6200015.62
ACCOUNT TOT15.6200015.62
Department 52 Phys ,(Excluding Aquatics
MEMBER NUMBERMEMBER NAMETRN DATE0 -30 DAYS31-60 DAYS61-90 DAYSOVER 90 DAYSPAST DUE
ACCOUNT#: 1-026-52-00000-3346
020-000066765-01example, example1/6/2017123000123
ACCOUNT TOT123000123

<tbody>
</tbody>
 
Upvote 0
ok ignore my offerings i totally misunderstood .. sorry i will leave this for others to ponder
 
Last edited:
Upvote 0
Department 14 Youth Sports
MEMBER NUMBERMEMBER NAMETRN DATE0 -30 DAYS31-60 DAYS61-90 DAYSOVER 90 DAYSPAST DUE
ACCOUNT#: 1-026-14-00000-3345
026-000180284-02example, example1/21/201715.6200015.62
ACCOUNT TOT15.6200015.62
Department 52 Phys ,(Excluding Aquatics
MEMBER NUMBERMEMBER NAMETRN DATE0 -30 DAYS31-60 DAYS61-90 DAYSOVER 90 DAYSPAST DUE
ACCOUNT#: 1-026-52-00000-3346
020-000066765-01example, example1/6/2017123000123
ACCOUNT TOT123000123

<tbody>
</tbody>
So here Account number is at A3 and then next Account number is at A7. So can appear at any row. I would like account number to be in one cell. Like
ACCOUNT#: 1-026-52-00000-3346

<tbody>
</tbody>
 
Upvote 0
Okay I got it!

A2: ACCOUNT#: 1-026-5
B2: 2-00000-3346
C2: =CONCATENATE(A2,B2)

Just match the columns with the cells and copy down the range.
 
Upvote 0
See if this macro does what you want...
Code:
[table="width: 500"]
[tr]
	[td]Sub CombineAccountNumberParts()
  Dim R As Long, Data As Variant
  Data = Range("A1:B" & Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row)
  For R = 1 To UBound(Data)
    If Data(R, 1) Like "ACCOUNT[#]:*" Then
      Data(R, 1) = Data(R, 1) & Data(R, 2)
      Data(R, 2) = ""
    End If
  Next
  Range("A1").Resize(UBound(Data), 2) = Data
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,877
Messages
6,127,500
Members
449,385
Latest member
KMGLarson

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