[英]How to copy a value of merged cells to its adjacent cell?
我需要在活动工作表中找到一个合并的单元格,然后将值复制到相邻的单元格中。
我试图在下面写一个宏,但这不是复制单元格值。 我在ActiveSheet.Cells(row, col).Select
收到Error 424
以下是我的表格:
A B
**Merged Cell1**
Value1
Value2
Value3
text4
text5
text6
text7
text8
**Merged Cell3**
Value1
Value2
**Merged Cell4**
text4
text5
text6
text1
**Merged Cell5**
text4
text5
**Merged Cell5**
text
Sub TestMacro5()
Dim rcount As Integer
Dim row As Integer
Dim col As Integer
Dim i As Integer
ActiveSheet.Select
col = 1
row = 1
i = 1
rcount = Application.CountA(Range("A:A"))
For i = 1 To rcount
ActiveSheet.Cells(row, col).Select
If Selection.MergeCells Then
ActiveCell.Offset(1, 5).Value = ActiveCell.Value
row = row + 1
Exit Sub
End If
Next i
End Sub
最好不要使用.Select
来确定要处理的单元格或工作表。
Dim rcount As Long
Dim rw As Long
Dim cl As Long
With ActiveSheet
cl = 1
rw = 1
rcount = .Cells(Rows.Count, 1).End(xlUp).row
For rw = 1 To rcount
If .Cells(rw, cl).MergeCells Then
.Cells(rw, cl).Offset(1, 5) = .Cells(rw, cl).Value '1 row down and 5 columns over ?
Exit For 'why exit here?
End If
Next rw
End With
有关直接处理单元格,单元格范围和工作表的更多方法,请参见如何避免在Excel VBA宏中使用“选择” 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.