在excel

时间:2017-11-20 22:09:35

标签: excel vba excel-vba

我想在工作表1中循环8列(AH),在工作表2中创建一个新列。然后再次循环8列(IP)并在工作表2中创建B列。我已经做了很多数据并认为这是最好的方法

这是我的代码

Range("E5").Select
Range(Selection, Selection.End(xlDown)).Select
Range("E5").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWindow.SmallScroll Down:=-96
Range("E5:E110").Select
Selection.Copy
Sheets("56 J").Select
Range("A2").Select
ActiveSheet.Paste
Sheets("56 g").Select

Range("F5").Select
Range(Selection, Selection.End(xlDown)).Select
Range("F5").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWindow.SmallScroll Down:=-96
Range("F5:F110").Select
Selection.Copy
Sheets("56 J").Select
Range("A110").Select
ActiveSheet.Paste
Sheets("56 g").Select

任何想法我怎么能把它放在循环通过列的东西?

enter image description here

这是我想要做的一个例子。我还希望时间和字母在循环时保留相应的数据。但我现在主要关注的是将数据放到一个列中。

2 个答案:

答案 0 :(得分:0)

您应该能够通过一些数学循环遍历源列和目标列。

Dim c As Long, n As Long, tws As Worksheet

Set tws = Worksheets("56 j")

With Worksheets("56 g")
    For n = 1 To 2
        For c = 1 To 8
            With .Range(.Cells(5, c + (n - 1) * 8), .Cells(.Rows.Count, c + (n - 1) * 8).End(xlUp))
                tws.Cells(tws.Rows.Count, n).End(xlUp).Offset(1, 0).Resize(.Rows.Count, .Columns.Count) = .Value
            End With
        Next c
    Next n
End With

答案 1 :(得分:0)

Option Explicit
Sub copydata()
Dim WS1, WS2 As Worksheet
Dim lastrow As Long
Dim ws1Row, ws2Row As Long
Dim mycol As Integer

Set WS1 = Worksheets("sheet1")
Set WS2 = Worksheets("sheet2")
lastrow = WS1.Cells(WS1.Rows.Count, "A").End(xlUp).Row
ws2Row = 2
For ws1Row = 5 To lastrow
    For mycol = 1 To 8
        WS2.Cells(ws2Row, 1) = WS1.Cells(ws1Row, mycol)
        WS2.Cells(ws2Row, 2) = WS1.Cells(ws1Row, mycol + 8)
        ws2Row = ws2Row + 1
    Next mycol
Next ws1Row
End Sub