Excelmacros连接结果给出错误的结果

我有这个macros连接所选单元格的值,并将其放入一个input框中。

我无法删除input框中的结果括号之前的最后一个逗号。

感谢帮助!

Private Sub CommandButton2_Click() Dim celCurrentCell As Range Dim celFirstCell As Range Dim i As Integer Dim txtTempText As String i = 0 txtTempText = "" For Each celCurrentCell In Selection If i = 0 Then i = 1 Set celFirstCell = celCurrentCell End If txtTempText = txtTempText & celCurrentCell.Value & "','" Next txtTempText = Left(txtTempText, Len(txtTempText) - 1) InputBox "Copier/coller le texte", "Concatenator", "('" + txtTempText + ")" End Sub 

假设您select单个行中的多个单元格或单个列中的多个单元格,则会删除该循环。

 Option Explicit Private Sub CommandButton2_Click() Dim strTemp As String, sep As String sep = "','" If Selection.Rows.Count > Selection.Columns.Count Then strTemp = "('" & Join(Application.Transpose(Selection.Columns(1).Value2), sep) & "')" Else strTemp = "('" & Join(Application.Transpose(Application.Transpose(Selection.Rows(1).Value2)), sep) & "')" End If Debug.Print strTemp InputBox "Copier/coller le texte", "Concatenator", strTemp End Sub 
 Private Sub CommandButton2_Click() Dim celCurrentCell As Range Dim celFirstCell As Range Dim i As Integer Dim txtTempText As String, sep as string i = 0 txtTempText = "" For Each celCurrentCell In Selection If i = 0 Then i = 1 Set celFirstCell = celCurrentCell End If txtTempText = txtTempText & sep & celCurrentCell.Value sep = "','" Next InputBox "Copier/coller le texte", "Concatenator", "('" + txtTempText + "')" End Sub