此代码可将列列表转换为带逗号的连接列表,但是当 VBA 在 B! 中循环时,格式没有前导零。
Dim i As Integer
Dim s As String
i = 2
Do Until Cells(i, 1).Value = ""
If (s = "") Then
s = Cells(i, 1).Value
Else
s = s & ", " & Cells(i, 1).Value
End If
i = i + 1
Loop
Cells(1, 2).Value = s
答案1
我们将只使用格式化的值:
Sub qwerty()
Dim i As Integer
Dim s As String
i = 2
Do Until Cells(i, 1).Value = ""
If (s = "") Then
s = Format(Cells(i, 1).Value, "0000")
Else
s = s & ", " & Format(Cells(i, 1).Value, "0000")
End If
i = i + 1
Loop
Cells(1, 2).Value = s
End Sub