在工作表中循环时在列中循环

时间:2018-12-03 21:09:14

标签: excel vba

我正在尝试从工作表1的A列提取数据并将数据粘贴到工作表2。然后循环到工作表1的B列并将数据粘贴到工作表3。继续,直到没有更多工作表为止。

我的代码遍历各列而不是工作表。

Sub Copy()
    Dim ws As Worksheet
    Dim WS_Count As Integer
    Dim i As Integer
    Dim J As Integer
    WS_Count = 10

    ' Begin the loop.
    For J = 1 To WS_Count
        Sheets("Bob").Select
        For i = 1 To 2
            Cells(10, i).Select
            Selection.Copy
            Set ws = ThisWorkbook.Worksheets(J)
            Range("A1:A5").Select
            ActiveSheet.Paste
        Next i
    Next J
End Sub

3 个答案:

答案 0 :(得分:0)

要循环工作表,请尝试:

Sub LoopWs()

    Dim ws As Worksheet

    For Each ws In ActiveWorkbook.Worksheets
        'Enter code
    Next

End Sub

答案 1 :(得分:0)

这假设工作表集合中的Sheet1也是Index 1

这将循环遍历所有工作表(不包括第一张工作表),并以这种模式复制列。该模式将重复进行,直到您用完工作表为止。

Sheet1 Column A Row 10 ---> Sheet2 Range C10
Sheet1 Column B Row 10 ---> Sheet3 Range C10
Sheet1 Column C Row 10 ---> Sheet4 Range C10


Option Explicit

Sub Strange_Loop()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets(1)
Dim i As Long

For i = 2 To ThisWorkbook.Sheets.Count
    Sheets(i).Cells(10, 3).Value = ws.Cells(10, i -1).Value
Next i

End Sub

答案 2 :(得分:0)

我建议一种更简单的方法:

 Sub CopyColumnA()

 Dim a As Integer

 For a = 2 To Sheets.Count
    Sheets(a - 1).Columns(1).Copy Destination:=Sheets(a).Columns(1)
 Next

 End Sub