简体   繁体   中英

VBA copy entire row of List

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 would go through the range in A6 to AXX and create a worksheets for different names. I somehow can't figure out however how to copy the content of every row into every worksheet created.

在此输入图像描述

So I want all the Ticker changes being copied into the new created worksheet ticker changes.

I know there is some way with the following:

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

But I don't know how to paste those to different worksheet. Can someone please advice or guide. Thanks

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

That name is already taken try a different one.

However there is no worksheet with that name.

You only need to reference/specify the sheet that you want to use.

Try this (I included an inputbox to correct the name of the sheet if it is already taken :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM