在两个整数之间循环

时间:2017-07-22 10:48:22

标签: excel excel-vba loops vba

请帮我解决这个问题,

要求:

将数据从第1页粘贴到第x页,然后跳到下一页。

问题:

我无法一次在2个整数之间运行循环。 我想每次都在x和y之间运行循环。但是编写的代码首先完成x,然后转到y。

请检查下面的代码并帮助我解决问题。谢谢。

Sub sbCopyValueToAnotherSheet()
Dim x As Integer
Dim y As Integer

For y = 2 To 11
 For x = 2 To 50

Sheets("Sheet1").Cells(x, y).Copy Destination:=ActiveSheet.Range("F6")
 y = y + 1
Sheets("Sheet1").Cells(x, y).Copy Destination:=ActiveSheet.Range("P6")
y = y + 1
Sheets("Sheet1").Cells(x, y).Copy Destination:=ActiveSheet.Range("P7")
y = y + 1
Sheets("Sheet1").Cells(x, y).Copy Destination:=ActiveSheet.Range("F8")
y = y + 1
Sheets("Sheet1").Cells(x, y).Copy Destination:=ActiveSheet.Range("P8")
y = y + 1
Sheets("Sheet1").Cells(x, y).Copy Destination:=ActiveSheet.Range("F9")
y = y + 1
Sheets("Sheet1").Cells(x, y).Copy Destination:=ActiveSheet.Range("P9")
y = y + 1
Sheets("Sheet1").Cells(x, y).Copy 
Destination:=ActiveSheet.Range("F10")
y = y + 1
Sheets("Sheet1").Cells(x, y).Copy 
Destination:=ActiveSheet.Range("P10")
y = y + 1
ActiveSheet.Next.Select

Next x
Next y

End Sub

1 个答案:

答案 0 :(得分:0)

如果您尝试将Sheet1中的50行复制到50张不同工作表上的相同单元格中,请尝试以下操作:

Option Explicit

Public Sub sbCopyValueToAnotherSheet()
    Dim x As Long, wsM As Worksheet, wsCount As Long

    wsCount = ThisWorkbook.Worksheets.Count
    Set wsM = ThisWorkbook.Worksheets("Sheet1")
    For x = 2 To 50
        With ThisWorkbook.Worksheets(x)
            .Range("F6") = wsM.Cells(x, 2)
            .Range("F7") = wsM.Cells(x, 3)
            .Range("F8") = wsM.Cells(x, 4)
            .Range("F9") = wsM.Cells(x, 5)
            .Range("F10") = wsM.Cells(x, 6)
            .Range("P6") = wsM.Cells(x, 7)
            .Range("P7") = wsM.Cells(x, 8)
            .Range("P8") = wsM.Cells(x, 9)
            .Range("P9") = wsM.Cells(x, 10)
            .Range("P10") = wsM.Cells(x, 11)
        End With
        If x = wsCount Then Exit Sub
    Next
End Sub