问题
This is a follow on question from my original question which has helped me solve the first part of my script: Autofill unknown amount of cells.
The second part of my script is to concatenate an unknown number of cells.
Using the logic from the first question I have tried to alter my script for to this myconcatenate = ActiveCell.FormulaArray = "=Concatenate("A5", mylastcell_4)"
But it is resulting in a syntax error.
Originally I was given this piece of script which worked fine on its own but doesn't work when added to the bottom of my script
Do
myconcatenate = myconcatenate & ActiveCell.Value
ActiveCell.Offset(0, 1).Select
Loop Until ActiveCell.Address = mylastcell_5
Range("A5").Select
ActiveCell.Value = myconcatenate
This gives a run time error '1004' Application-defined or object-defined error and will go to the very last cell on row 5 (even though I only have 5 currently occupied in Row 1) and does nothing while it passes through all the cells. Sometimes it will do the first bit of the script (adds '', to the cell values) other times it doesn't.
Here is my full script for context: Screenshot of full script
Ultimately I am trying to get from a list of abcdedf to 'a','b','c','d','e','f', into one cell.
Ultimate goal picture:

row 5 cell D gives the ultimate answer I'm after, E5 would be mylastcell_5
This is my first day on VBA, and I have spent the whole day googling bits to try and answer questions but this hasn't resulted in anything for me.
UPDATE: I've declared mylastcell_5 which I hadn't done previously. I'm now getting no error in the script and it is just hanging out at A5 rather than going to the very end of the page, but it's not actually concatenating.
So I think that somehow I need to change my loop so that it is writing to row 5 but reading from row 4 ... but I don't know how to do this.
Option Explicit
Sub concat()
'
' concat Macro
' 'text','text'
'
' Keyboard Shortcut: Ctrl+Shift+C
'
With ThisWorkbook.Worksheets("Sheet1")
Dim mylastcell_1 As String, mylastcell_2 As String, mylastcell_3 As String, mylastcell_4 As String, mylastcell_5 As String, myconcatenate As String, myconcatenate1 As String
Range("A1").Select
Selection.End(xlToRight).Select
mylastcell_1 = ActiveCell.Address
Range(mylastcell_1).Select
ActiveCell.Offset(1, 0).Select
mylastcell_2 = ActiveCell.Address
Range(mylastcell_2).Select
ActiveCell.Offset(1, 0).Select
mylastcell_3 = ActiveCell.Address
Range(mylastcell_3).Select
ActiveCell.Offset(1, 0).Select
mylastcell_4 = ActiveCell.Address
Range(mylastcell_4).Select
ActiveCell.Offset(1, 0).Select
mylastcell_5 = ActiveCell.Address
myconcatenate = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
Range("A2").Select
Selection.End(xlToRight).Select
Range("A2").Select
Range("A2", mylastcell_2).Select
Selection.FormulaArray = "''"
Range("A3").Select
Range("A3", mylastcell_3).Select
Selection.FormulaArray = "'',"
Range("A4").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(R[-2]C,R[-3]C,R[-1]C)"
Range("A4").Select
.Range(.Cells(4, "A"), .Cells(1, .Columns.Count).End(xlToLeft).Offset(3, 0)).Formula = "=concatenate(a2, a1, a3)"
Range("A5").Select
Do
myconcatenate = myconcatenate & ActiveCell.Value
ActiveCell.Offset(0, 1).Select
Loop Until ActiveCell.Address = mylastcell_5
Range("A5").Select
ActiveCell.Value = myconcatenate
End With
End Sub
回答1:
I don't know if you always plan to concatenate elements into a single-quote enclosed comma-delimited string, but if you do here are a couple ways to do that (I've found it useful for quickly inserting things into SQL statements, for example). One joins vertical strings (single column) and one joins horizontal strings (single row). This would be a standard worksheet function where you would enter something like =hjoin(a1:h1)
or =vjoin(a1:a7)
Function hJoin(inRng As Range) As String
Dim c As Range
Dim outStr As String
Dim rng As Range
Set rng = Range(inRng.Address)
For Each c In rng.Columns
If c.Address = rng.Cells(rng.Cells.Count).Address Then
outStr = outStr & " '" & c.Value & "'"
Else
outStr = outStr & " '" & c.Value & "',"
End If
Next c
hJoin = outStr
End Function
Function vJoin(inRng As Range) As String
Dim c As Range
Dim outStr As String
Dim rng As Range
Set rng = Range(inRng.Address)
For Each c In rng.Rows
If c.Address = rng.Cells(rng.Cells.Count).Address Then
outStr = outStr & " '" & c.Value & "'"
Else
outStr = outStr & " '" & c.Value & "',"
End If
Next c
vJoin = outStr
End Function
来源:https://stackoverflow.com/questions/52960214/concatenate-an-unknown-number-of-cells