简体   繁体   English

将单元格值复制到一系列单元格

[英]Copy cell value to a range of cells

I'm new to VBA and I am trying to copy values from one cell to multiple cells when its value changes. 我是VBA的新手,我想在其值更改时将值从一个单元格复制到多个单元格。

The value of A2 is constantly changing and when that happens I want that value to be copied to cells C2:C21 (and then eventually to cells D2:D21) A2的值在不断变化,当发生这种情况时,我希望将该值复制到单元格C2:C21(然后最终复制到单元格D2:D21)

Here is an example of what I would like to achieve: 这是我要实现的示例:

http://i.stack.imgur.com/xJZyZ.jpg http://i.stack.imgur.com/xJZyZ.jpg

So far I wrote this code: 到目前为止,我编写了以下代码:

Sub Worksheet_Change(ByVal Target As Range)
   For i = 0 To 19
      If Not Intersect(Target, Range("AS2")) Is Nothing Then
         Cells(Target.Row + i, 58).Value = Cells(Target.Row, 45).Value
      End If
   Next i
End Sub

but this only copies one single value of A2 to all the cells C2 to C22. 但这仅将一个A2值复制到所有单元格C2至C22。

May anyone help me write this code properly? 谁能帮我正确编写此代码?

Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("AS2")) Is Nothing Then
        For CurCol = 3 to 4
            For CurRow = 2 to 21
                If Cells(CurRow, CurCol).Value = "" Then
                    Cells(CurRow, CurCol).Value = Target.Value
                    Exit Sub
                EndIf
            Next CurRow
        Next CurCol
    End If
End Sub

I guess this is what you're after: 我想这就是你所追求的:

Option Explicit

Sub Worksheet_Change(ByVal Target As Range)
    Dim nVals As Long

    If Not Intersect(Target, Range("A2")) Is Nothing Then
        With Range("C2:D21")
            nVals = WorksheetFunction.CountA(.Cells)
            If nVals = .Count Then Exit Sub
            Application.EnableEvents = False
            On Error GoTo exitsub
            .Cells(nVals Mod .Rows.Count + 1, IIf(nVals >= .Rows.Count, 2, 1)).Value = Target.Value
        End With
    End If

exitsub:
Application.EnableEvents = True
End Sub

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

相关问题 将值从工作表中的单元格复制到一系列单元格中 - Copy the value from a cell in a worksheet into a range of cells 在工作表中搜索单元格值,然后将相邻单元格复制到变量范围中 - Search a worksheet for a cell value, then copy the adjacent cells into a variable range 在单元格区域中单击任何单元格时,将其复制并粘贴为值 - Copy and Paste as Value when any cell is clicked within a range of cells 如果范围内只有一个单元格有值,如何将单元格范围内的值复制到另一个单元格? - How to copy the value in a range of cells to another cell if only one cell in the range has a value? 如果区域中只有一个单元格具有值,则将单元格区域中的值复制到另一个单元格 - Copy the value in a range of cells to another cell if only one cell in the range has a value 如果单元格 &lt;= 值,则复制范围 - Copy Range if cell is <= value 如果单元格区域中存在单元格值 - If Cell Value is present in a range of Cells 根据单元格值复制单元格 - Copy cells based on cell value 如果某个范围中的单元格的值为“ x”,则将某个范围的单元格复制并粘贴到另一张工作表中 - If cell in a range has value “x” copy and paste a range of cells into a different sheet 将单元格范围(不包括空格)复制到一个单元格中 - Copy range of cells (excluding blanks) into one cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM