我能想到的最好方法是将参数传递给ConcatenateAll
过程,然后提及要连接的所有范围。例如,我ConcatenateAll()
从Sample()
我还冒昧地稍微修改了您的代码。
Sub Sample()
With ActiveSheet
ConcatenateAll .Range("AT1:CB20"), .Range("AK1")
ConcatenateAll .Range("AT31:CB50"), .Range("AK2")
End With
End Sub
Sub ConcatenateAll(rngInput As Range, rngOutput As Range)
Dim x As String, cell As Range
For Each cell In rngInput
If Len(Trim(cell.Value)) <> 0 Then
If x = "" Then
x = cell.Value
Else
x = x & "; " & cell.Value
End If
End If
Next
rngOutput.Value = x
End Sub
跟进
AT1:CB20, AT21:CB40, AT41:CB60, AT61:CB80 我希望结果显示在 AK1, AK2, AK3, AK4 等直到范围结束 – user2668126 4 分钟前
试试这个代码。由于存在趋势,因此循环遍历范围变得更容易。
Option Explicit
Sub Sample()
Dim r1 As Long, r2 As Long, i As Long
r2 = 20: i = 1
With ActiveSheet
For r1 = 1 To 2001 Step 20
ConcatenateAll .Range("AT" & r1 & ":CB" & r2), .Range("AK" & i)
r2 = r2 + 20: i = i + 1
Next
End With
End Sub
Sub ConcatenateAll(rngInput As Range, rngOutput As Range)
Dim x As String, cell As Range
For Each cell In rngInput
If Len(Trim(cell.Value)) <> 0 Then
If x = "" Then
x = cell.Value
Else
x = x & "; " & cell.Value
End If
End If
Next
rngOutput.Value = x
End Sub