VBA script to Concatenate data with a defined first text string and end string.

FlyingSchmidt

New Member
Joined
Mar 1, 2022
Messages
4
Office Version
  1. 2021
Platform
  1. Windows
Looking for help to create a script that will automatically concatenate data when pasted into my workbook.
Unfortunately the data I am working with can only be copied from a webpage and not exported into a .csv.
The below screen capture shows how the data gets pasted into excel from the website.
Each string begins with character an exclamation mark "!" and ends with a 10 digit time group.
There are empty cells between each single set of data. The data strings that need to be concatenated are not separated by empty cells.
Any help would be greatly appreciated.
1646157466245.png


 

Attachments

  • 1646156922777.png
    1646156922777.png
    182.4 KB · Views: 8

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Welcome to the Board!

Can you post what your expected results should look like?
Do you still want the blanks left between items?
 
Upvote 0
Welcome to the Board!

Can you post what your expected results should look like?
Do you still want the blanks left between items?
Hi Joe,
The raw data I receive on the left and the format I require on the right. I do not want/need the blank cells between the data.
Thanks for the help.
1646161442355.png
1646161465441.png
 
Upvote 0
This should work

VBA Code:
Sub jec()
 Dim ar As Variant, i As Long, x As Long
 Dim sq() As Variant
 ar = Range("A1", Range("A" & Rows.Count).End(xlUp))
 
 For i = 1 To UBound(ar)
     ReDim Preserve sq(x)
     If i = 1 Then
       sq(x) = ar(i, 1)
     ElseIf Len(ar(i - 1, 1)) = 0 Or Len(ar(i, 1)) = 0 Then
       sq(x) = ar(i, 1)
     Else
       sq(x - 1) = sq(x - 1) & " " & ar(i, 1)
       x = x - 1
     End If
     x = x + 1
 Next
 
 With Range("A1", Range("A" & Rows.Count).End(xlUp))
   .ClearContents
   .Resize(x) = Application.Transpose(sq)
 End With
End Sub
 
Upvote 0
This should work

VBA Code:
Sub jec()
 Dim ar As Variant, i As Long, x As Long
 Dim sq() As Variant
 ar = Range("A1", Range("A" & Rows.Count).End(xlUp))
 
 For i = 1 To UBound(ar)
     ReDim Preserve sq(x)
     If i = 1 Then
       sq(x) = ar(i, 1)
     ElseIf Len(ar(i - 1, 1)) = 0 Or Len(ar(i, 1)) = 0 Then
       sq(x) = ar(i, 1)
     Else
       sq(x - 1) = sq(x - 1) & " " & ar(i, 1)
       x = x - 1
     End If
     x = x + 1
 Next
 
 With Range("A1", Range("A" & Rows.Count).End(xlUp))
   .ClearContents
   .Resize(x) = Application.Transpose(sq)
 End With
End Sub
Could not get this one to work
1646175636188.png




1646175679709.png
 
Upvote 0
Welcome to the MrExcel board!

For the future, consider this so that helpers do have to amnually type data to test with:
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.


Try this with a copy of your data.

If you want to remove all the blank lines from the results, uncomment the line near the end of the code

VBA Code:
Sub Join_Data()
  Dim rA As Range
  
  Application.ScreenUpdating = False
  For Each rA In Range("A1", Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlConstants).Areas
    With rA
      If .Rows.Count > 1 Then
        .Cells(1).Value = Join(Application.Transpose(.Value))
        .Offset(1).ClearContents
      End If
    End With
  Next rA
'  Columns("A").SpecialCells(xlBlanks).Delete Shift:=xlUp
  Application.ScreenUpdating = True
End Sub

Before:

FlyingSchmidt.xlsm
A
1chsaf s g fhg
2
3czCZC
4
5zczCv cvbvcbnvb
6vcbnmvbmvn
7
8vbnmbvm
9aaaaaa
10mhmhm
11
12
13sfdsdf
Sheet1


After:

FlyingSchmidt.xlsm
A
1chsaf s g fhg
2
3czCZC
4
5zczCv cvbvcbnvb vcbnmvbmvn
6
7
8vbnmbvm aaaaaa mhmhm
9
10
11
12
13sfdsdf
Sheet1
 
Upvote 0
Solution
Thank you Peter!
This works for my needs very well.
I will try to get the XL2BB Add-in next time. I was having issues with admin rights on my work computer.

Again, many thanks
 
Upvote 0
You're welcome. Thanks for the follow-up. :)

Hope that you can get XL2BB up and running in due course. ?
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,429
Members
448,961
Latest member
nzskater

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