简体   繁体   English

如何将合并单元格的值复制到其相邻单元格?

[英]How to copy a value of merged cells to its adjacent cell?

I need to find a merged cell in the active sheet and copy the value to the adjacent cell. 我需要在活动工作表中找到一个合并的单元格,然后将值复制到相邻的单元格中。
I tried to write a macro below but this is not copying the cell value. 我试图在下面写一个宏,但这不是复制单元格值。 I am getting Error 424 in the line ActiveSheet.Cells(row, col).Select 我在ActiveSheet.Cells(row, col).Select收到Error 424

Below is my table: 以下是我的表格:

    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

It is best to stay away from using .Select to deterine the cell or worksheet that you want to deal with. 最好不要使用.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

See How to avoid using Select in Excel VBA macros for more methods on dealng with cells, cell ranges and worksheets directly. 有关直接处理单元格,单元格范围和工作表的更多方法,请参见如何避免在Excel VBA宏中使用“选择”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅将相邻单元格合并到范围中的合并单元格 - Merge adjacent cell only to merged cells in a range 循环通过合并单元格下的相邻单元格 - Looping through Adjacent Cells Under a Merged Cell 如何保持每个单元格中合并单元格的值? - How to keep value of merged cells in each cell? 在工作表中搜索单元格值,然后将相邻单元格复制到变量范围中 - Search a worksheet for a cell value, then copy the adjacent cells into a variable range 检查合并的单元格并比较相邻的单元格以从比较的单元格值中设置唯一值 - Check merged cell and compare adjacent to set unique value from compared cells values VBA - 如何从单个单元格复制数据并粘贴到合并的单元格中 - VBA - How to copy data from a single cell and paste into merged cells Excel-如何根据其颜色和相邻单元格的颜色对单元格进行计数? - Excel - How to count cells depending on its color and the color of an adjacent cell? VBA如何匹配单元格与相邻单元格的范围和返回值? - vba how to match cell with range and return value in adjacent cells? Excel的VBA代码-在另一个工作簿中查找一个单元格值,并将相邻的单元格复制到第一个工作簿中 - VBA codes for Excel- Find a cells value in another workbook and copy adjacent cell to the first workbook 根据相邻单元格中的值填充单元格 - Fill cells based on value in adjacent cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM