excel - 将单元格组合在同一列中
问题描述
好的,我已经解决了很多以前的问题,但找不到适合我想要做的问题。
我正在尝试构建一个宏或 VBA,它将列中第 1 组中的单元格与同一列中第 2 组中的单元格合并到同一列中的其他单元格中。
我需要能够为工作表上的每一列完成宏。要为一列激活它,我希望它是在第 72 行的该列的单元格中放入“x”时。
例如:
I enter "x" in Cell 72 on Column MP.
I need Cell 78 in the MP column to merge cell 10 and 129 together with a " / " between them.
我知道公式=MP78 & " / " &MP129
有效并且可以复制和粘贴它,但是当信息可以覆盖在单元格中时,公式将不起作用。
任何帮助都会很棒。
我还附上了要显示的组的屏幕截图。
第 1 组
第 2 组
**在哪里合并单元格
解决方案
这是我昨天为你的问题准备的回复。请将代码粘贴到您想要操作的工作表的代码模块中。它是由 Excel 创建的模块,而不是您需要插入的模块。位置很重要。
Option Explicit
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
' Variatus @STO 26 Jan 2020
' list the cells that should trigger joining actions
' in their respective columns
Const Triggers As String = "A4,B16, D15,MP72"
' list all cell you wish to merge in the Trigger column.
' Like, 30 = 6 & 23 to mean, merge the values of cell
' 6 & 23 in cell 30
' (separating commas are imporant, spaces are not)
Const Mergers As String = "78=10&129, 30=6&23, 31=14 & 12, 32=30 & 18"
Dim Trigger() As String
Dim t As Integer
Dim R As Long, C As Long
Dim Merger() As String, Sp() As String
Dim i As Integer
Trigger = Split(Triggers, ",")
For t = 0 To UBound(Trigger)
If Target.Address = Range(Trim(Trigger(t))).Address Then
Application.ScreenUpdating = False
C = Target.Column
Merger = Split(Mergers, ",")
For i = 0 To UBound(Merger)
Sp = Split(Merger(i), "=")
R = Val(Sp(0))
Sp = Split(Sp(1), "&")
Cells(R, C).Value = Cells(Val(Sp(0)), C).Value & _
" / " & _
Cells(Val(Sp(1)), C).Value
Next i
Application.ScreenUpdating = True
Cancel = True
Exit For
End If
Next t
End Sub
该过程将响应双击而不是输入“x”,但只有在双击特定单元格时才会执行操作。Const Triggers
列出了单元格。我已将MP72
用于测试的列表和其他单元格包括在内。删除它们。您可以只指定一个或多个单元格。该代码从单击的单元格中获取列。一次双击只会执行其中一个选项。
Const Mergers
允许您指定要合并的单元格 - 始终在单击的列中。格式很简单:“78 = 10 & 129”表示“将第 10 行和第 129 行的单元格中的值合并到第 78 行。您可以指定任意数量的此类合并。我的示例有四个,只是为了展示如何输入规格。
推荐阅读
- c - 算法图深度 - 首次搜索
- android - Android Studio 使用空文件夹恢复已删除和删除的模块
- ios - 如何使用 NSExtensionPrincipalClass 显示 Storyboard UI 以共享扩展
- javascript - 两次 Axios 调用后如何设置 SetState?
- typo3 - 在typo3中将数据写入go_maps_ext
- c# - WCF 元数据读取异常
- python - 从文本文件返回值作为布尔值
- c# - 如何使用 OpenXml 将 Excel 转换为 Html
- hadoop - AWS Athena 在分区加载后创建缩进并将值移动到错误的列中
- java - 欢迎留言 JSP JAVA