VBA copies the entire list line

I have the following code:

Sub test()
Dim r As Range, rng As Range
Set r = Range("a6", Range("a6").End(xlDown))
    For Each rng In r
        If rng <> rng.Offset(-1) Then 'if range is not
            Dim ws As Worksheet
            Set ws = Worksheets.Add
            ws.Name = rng
        Else
        End If
    Next rng
End Sub

This will go through the range from A6 to AXX and create worksheets for different names. I somehow cannot figure out how to copy the contents of each row into each worksheet created.

enter image description here

So, I want all Ticker changes to be copied to the new ticker of the added worksheet.

I know there is a way:

   Range(Cells(rng, 1), Cells(rng, 10)).Copy

But I do not know how to insert them into another worksheet. Can anyone ask for advice or guidance. thanks

Also, when I try to run this macro, it sometimes says:

This name is already accepted, try another.

However, there is no worksheet with this name.

+4
source share
1 answer

/ , .

( , :

Sub test_Nant()
Dim r As Range, rng As Range, ws As Worksheet, aWs As Worksheet
Set aWs = ActiveSheet
Set ws = Worksheets.Add
            On Error GoTo SheetRename
            ws.Name = "Changes list"
            GoTo KeepLooping
SheetRename:
            ws.Name = InputBox("Choose another name for that sheet : ", , rng.Value)
            Resume Next
KeepLooping:
With aWs
    Set r = .Range(.Range("a6"), .Range("a6").End(xlDown))
    For Each rng In r
        If rng <> rng.Offset(-1) Then 'if range is not
            .Range(.Cells(rng.Row, 1), .Cells(rng.Row, 10)).Copy Destination:=ws.Range("A1")
        Else
        End If
    Next rng
End With
End Sub
+2

Source: https://habr.com/ru/post/1613174/


All Articles