[英]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.