How can I copy columns from one sheet to another with VBA in Excel?

前端 未结 5 1789
抹茶落季
抹茶落季 2020-12-09 17:39

I\'m trying to write a macro that copies the content of column 1 from sheet 1 to column 2 on sheet 2. This is how the module looks like but, when I run it, I get

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-09 18:23

    Private Sub Worksheet_Change(ByVal Target As Range)
      Dim rng As Range, r As Range
      Set rng = Intersect(Target, Range("a2:a" & Rows.Count))
      If rng Is Nothing Then Exit Sub
        For Each r In rng
          If Not IsEmpty(r.Value) Then
            r.Copy Destination:=Sheets("sheet2").Range("a2")
          End If
        Next
      Set rng = Nothing
    End Sub
    

提交回复
热议问题