VBA loop and Concatenate a range of cells

NichoD

Board Regular
Joined
Jul 31, 2022
Messages
54
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I am trying to concatenate all cells with a value in a range and combine them into a single string with "*" and "-" as delimiters.
Cells in column D are either blank or filled with numbers and letters. Cells in column C have a value if cells in column D has a value, and have the value "0" otherwise. Cells in collumn B always have a value. I want to loop through the range from first existing value in column D to last, and retrive the cells in all rows where column D have a value. The order of the string should be; Cell B value, Cell C value, Cell D value, todays date, Next row (repeat).

So far I have this code:

Sub test()

Dim lRow As Long
Dim fRow As Long
Dim s As String
Dim r As Long
Dim dt As Date

With ActiveWorkbook.Worksheets("Sheet1").Range("D4:D18")

fRow = Cells(Rows.Count, 1).End(xlDown).Row
lRow = Cells(Rows.Count, 1).End(xlUp).Row


For r = fRow To lRow

If cell.Value <> "" Then
s = cell.Value.Offset(0, -2).Value_
& "*" & cell.Value.Offset(0, -1).Value & "*" & cell.Value & "*" & dt & "-"

Range("Q4") = s

End If

Next r

End With


End Sub

 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Is it?
VBA Code:
Option Explicit
Sub Textstring()
Dim lr&, i&, rng, s As String, arr()
lr = Cells(Rows.Count, "D").End(xlUp).Row
rng = Range("B1:D" & lr).Value
ReDim arr(1 To lr, 1 To 1)
For i = 1 To lr
    If IsNumeric(rng(i, 3)) And rng(i, 3) <> "" Then
        arr(i, 1) = rng(i, 1) & "*" & rng(i, 2) & "*" & rng(i, 3) & "*" & Format(Date, "dd-mm-yyyy") & "-"
    End If
Next
Range("Q1").Resize(UBound(arr), 1).Value = arr
End Sub
 
Upvote 0
Hmm, Nothing is triggered at all actually. Ive looked through your code, and even tried running it as it it!
 
Upvote 0
Do you want just a single string or a string on every row ?
The idea is to get one single string containing cell values from all rows.

One example for the string could be; B10,C10,D10,date,B12,C12,D12,date,B15,C15,D15

Hope this clarifies my question!
 
Upvote 0
See if this is what you are after:
(Change the date format to suit)

VBA Code:
Sub test()

    Dim lRow As Long
    Dim fRow As Long
    Dim s As String
    Dim r As Long
    Dim dt As Date
 
    Dim ws As Worksheet
    Dim rng As Range
    Dim arr As Variant
 
    Set ws = ActiveWorkbook.Worksheets("Sheet1")
 
    With ws
        fRow = 1
        If .Range("D1") = "" Then
            fRow = .Range("D1").End(xlDown).Row
        End If
        lRow = .Cells(Rows.Count, "D").End(xlUp).Row
    
        Set rng = ws.Range(.Cells(fRow, "A"), .Cells(lRow, "D"))
        arr = rng
    
        For r = 1 To UBound(arr)
            If arr(r, 4) <> "" Then
                s = s & _
                    arr(r, 2) & "*" & _
                    arr(r, 3) & "*" & _
                    arr(r, 4) & "*" & _
                    Format(Date, "mm/dd/yyyy") & "-"
            End If    
        Next r
        .Range("Q4") = Left(s, Len(s) - 1)
    End With

End Sub
 
Upvote 0
That worked! thank you so much! have a great day!
 
Upvote 0

Forum statistics

Threads
1,213,561
Messages
6,114,312
Members
448,564
Latest member
ED38

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