将单元格复制到具有可变行号的anoter工作表

时间:2017-04-20 10:29:52

标签: excel-vba vba excel

如何将单元格内容复制到另一个带有可变行号的工作表中的单元格?

我搜索了这个网站,并提出了以下代码,但目标工作表中没有任何内容。 工作表“Koersen”会自动更新。 “Koersen”中的单元格A19应复制到“ASML”中的单元格A3,当“Koersen”中的单元格C7更改时,应将其复制到“ASML”中的A4,然后复制到A5,A6等等。

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim x As Integer
    For x = 3 To 1500
        If Target.Address = "$C$7" Then
            Sheets("Koersen").Cells(19, 1).Copy
            Sheets("ASML").Cells(x, 1).Paste
        End If
    Next x
End Sub

正如您可能已经猜到的那样,我是VBA的全新手 提前谢谢。

2 个答案:

答案 0 :(得分:0)

尝试以下方法,但我无法清楚地看到你想要达到的目标......

Private Sub Worksheet_Change(ByVal Target As Range) 'if something change in worksheet...
    Dim x As Integer
    Worksheets("ASML").Cells(3, 1) = Worksheets("Koersen").Cells(19, 1) 'make value of ASML A3 equal to Koersen A19
    If Target.Address = "$C$7" Then 'if what have changed is cell C7...
        For x = 3 To 1500
            Worksheets("ASML").Cells(x, 1) = Target.Value '...copy value of Koersen C7 to ASML column A, from row 3 to 1500
        Next x
    End If
End Sub

答案 1 :(得分:0)

我相信您尝试做的是每次更改单元格Koersen!A19时,将ASML复制到C7中的新行。如果是这样,以下代码应该起作用:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim x As Long
    If Target.Address = "$C$7" Then
        With Sheets("ASML") ' use a With block to save typing
            x = .Cells(.Rows.Count, "A").End(xlUp).Row + 1    
            Sheets("Koersen").Cells(19, "A").Copy .Cells(x, "A")
        End With
    End If
End Sub