[英]merge cells based on value of another cell with VBA
I've been working on a project and I'm trying to make things go smoother :) 我一直在从事一个项目,我正在努力使事情顺利进行:)
I have an excel sheet with several columns and as you can see it below, Column C is the importance of the topic(based on information typed in that row) and Column D is whether the information typed is a new information or an update regarding the previous (upper) row. 我有一个包含几列的excel工作表,正如您在下面看到的那样,C列是主题的重要性(基于该行中键入的信息),D列是键入的信息是有关该主题的新信息还是更新信息前(上)行。 Soo: o
if I type "update" on column D, row 3; 如果我在D列第3行输入“ update”; I want it to automatically merge the cells C2 and C3. 我希望它自动合并单元格C2和C3。
C D
1 LOW new
2 HIGH new
3 update
4 Low new
5 update
6 update
I don't know how to write VBA codes but I can mostly understand the codes enough to adopt what I find on internet to what I want to achieve. 我不知道如何编写VBA代码,但我大部分都能理解这些代码,足以将我在互联网上找到的内容应用到想要实现的目标上。 I have checked so many websites to find whatever I needed but I had no luck so I would really appreciate if you could help me :) 我已经检查了很多网站来找到我需要的东西,但是我没有运气,所以如果您能帮助我,我将不胜感激:)
You can try something like this add this Macro in the Current Sheet. 您可以尝试执行类似操作,在“当前工作表”中添加此宏。
Private Sub Worksheet_Change(ByVal Target As Range)
IF Target.value = "Update" then
Range("A" & Target.row - 1 & ":A" & Target.row).Merge
End If
End Sub
Try this : 尝试这个 :
Sub Merge_Priority()
Dim RgToMerge As String
For i = 1 To ActiveSheet.Cells(Rows.Count, 4).End(xlUp).Row
RgToMerge = ""
If LCase(Cells(i, 4)) <> "update" Or (LCase(Cells(i + 1, 4)) <> "new" And Cells(i + 1, 4) <> "") Then
Else
RgToMerge = "$C$" & Cells(i, 3).End(xlUp).Row & ":$C$" & i
With Range(RgToMerge)
.Merge
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
End If
Next i
End Sub
Do you know how to add a macro on an event? 您知道如何在事件上添加宏吗?
Go to Visual Studio, select ThisWorkBook
on the left and create a macro with this : 转到Visual Studio,选择左侧的ThisWorkBook
并使用以下命令创建一个宏:
Private Sub Worksheet_Change()
And paste the code right above 并在上面粘贴代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.