3

我如何将选定单元格中的单元格合并为一个单元格,选定的单元格数量会有所不同,它可能是 5 个或更多单元格,但所有单元格都会像 A1、A2、A3 等一样继续。我已经阅读了文章将 多个单元格合并为一个在excel中使用宏? 但是我如何将上述链接答案用于选定的单元格。我正在使用 Excel 2007,所以我希望它是可压缩的代码。

4

3 回答 3

2

我认为这应该可以解决问题。它基本上使用范围变量来处理选择。然后它用所选单元格的值填充一个数组。CSV 是包含您的结果的变量。

请注意,selectedCells这里是一个变量,而不是 Excel 中的一些特殊函数。

[此代码在 Excel 2002 中有效 - 在其他版本中不确定。]

Dim selectedCells As Range
Dim rng As Range
Dim i As Integer
Dim values() As String
Dim CSV As String

' you may need some error handling here in case your selection
' isn't a range
Set selectedCells = Selection

ReDim values(selectedCells.Count - 1)

i = 0
For Each rng In selectedCells
  ' you may want some error handling here when populating the array
  values(i) = CStr(rng.Value)
  i = i + 1
Next rng

CSV = Join(values, ",")
于 2012-04-07T21:16:12.520 回答
1

只需尝试以下代码复制,粘贴并保存即可。||||||||||||

Required Data   Result     Code
1            1,2,3,4,5,    =darksider_con(A1:A5)
2       
3       
4       
5       

Function darksider_con(rng As range) As String
Dim cell As range
Dim result As String
For Each cell In rng
result = result & cell.Value & ","
Next
darksider_con = result
End Function
于 2020-05-23T10:10:57.750 回答
0

对于 1D 或 2D 范围。可应用于包含空白单元格的范围。我们也可以指定一个快捷键Ctrl+Shift+M,比如 MS word 中用于合并表格中的单元格的快捷键是Alt+M

注意.. 宏结果无法撤消。所以,首先在样本数据上尝试这个宏。但是,如果在同一合并范围上运行,而:的值未更改,则此MergeCellsWithComma宏的结果可以通过第二个过程撤消。restoreMergedCellsPublic variablevalRng

确保在两个过程中都相同rowDelimitercellDelimiter

Public valRng As String
'______________________________________________________________

Sub MergeCellsWithComma()
If Selection.MergeCells = True Then Exit Sub
Application.DisplayAlerts = False

rowDelimiter = "}"
cellDelimiter = "|"

Dim rng As Range: Set rng = Selection
If rng Is Nothing Then Exit Sub
On Error GoTo 0
Dim cL As Range
valRng = ""
For i = 1 To rng.Rows.Count
    For Each cL In Application.Index(rng, i, 0)
    'If cL <> "" Then valRng = valRng & cL & cellDelimiter
    'We can remove this if condition and keep only
    valRng = valRng & cL & cellDelimiter
    'in case we want the resulting string to show blank cells as well.
    'It would be better to do so if 'the original data needs to be restored
    Next
    'Following is a separate delimiter for rows in the selection.
    'If not needed comment it
    valRng = Left(valRng, Len(valRng) - 1) & rowDelimiter
    ' instead of rowDelimiter one can use Chr(10) for line break
Next

valRng = Left(valRng, Len(valRng) - 1)
rng.MergeCells = True
Range(rng(1, 1).Address) = valRng
Application.DisplayAlerts = True

End Sub

为行创建不同的分隔符背后的想法:如果出现任何问题,我们应该能够使用以下过程恢复合并的单元格。如果在运行此过程之前更改了Public variable:的值,则将其分配为。valRngvalRng = Selection.Cells(1, 1).Value

Sub restoreMergedCells()
If Selection.MergeCells = False Then Exit Sub
rowDelimiter = "}"
cellDelimiter = "|"

Selection.MergeCells = False
Dim valRngRowsCells

'If value of Public variable: valRng is changed before running this procedure
valRng = Selection.Cells(1, 1).Value
valRngRows = Split(valRng, rowDelimiter)
' rowDelimiter or Chr(10) whatever applied in above macro
a = LBound(valRngRows): x = UBound(valRngRows)

If InStr(1, valRngRows(1), cellDelimiter, 1) > 0 Then
valRngRowsCells = Split(valRngRows(1), cellDelimiter)
b = LBound(valRngRowsCells): y = UBound(valRngRowsCells)

ReDim valRngRowsCells(0 To x, 0 To y)

For i = a To x
    For j = b To y
    valRngRowsCells(i, j) = Split(valRngRows(i), cellDelimiter)(j)
    Next
Next

'Range(Cells(1, 1), Cells(x + 1, y + 1)) = valRngRowsCells
Selection.Value = valRngRowsCells
Else
Selection.Value = Application.Transpose(valRngRows)
End If

End Sub
于 2020-05-23T11:40:40.433 回答