是否可以遍历数据工作表?

时间:2016-09-28 15:18:34

标签: excel vba excel-vba

我想知道是否有办法循环我的数据工作表,而不是将其从A2编码到AQ2?

Worksheets("FORM TEMPLATE").Range("D9").Copy
Worksheets("Data").Range("A2").PasteSpecial xlPasteValues

Worksheets("FORM TEMPLATE").Range("D10").Copy
Worksheets("Data").Range("B2").PasteSpecial xlPasteValues

Worksheets("FORM TEMPLATE").Range("J9").Copy
Worksheets("Data").Range("C2").PasteSpecial xlPasteValues

Worksheets("FORM TEMPLATE").Range("J10").Copy
Worksheets("Data").Range("D2").PasteSpecial xlPasteValues

Worksheets("FORM TEMPLATE").Range("J11").Copy
Worksheets("Data").Range("E2").PasteSpecial xlPasteValues

5 个答案:

答案 0 :(得分:2)

将值加载到数组中并将值赋给数组。以有序的方式加载数组后,立即将它全部转储回Data工作表。

Sub wqewtry()
    Dim a As Long, arr As Variant

    'preload and shape the array
    With Worksheets("Data")
        arr = .Range("A2:AQ2").Value2
    End With

    With Worksheets("FORM TEMPLATE")
        'use .Value2 for text or numbers; .Value for dates
        a = a + 1: arr(1, a) = .Range("D9").Value2
        a = a + 1: arr(1, a) = .Range("D10").Value2
        a = a + 1: arr(1, a) = .Range("J9").Value2
        a = a + 1: arr(1, a) = .Range("J10").Value2
        a = a + 1: arr(1, a) = .Range("J11").Value2
    End With

    'dump back into the data worksheet
    With Worksheets("Data")
        .Range("A2:AQ2") = arr
    End With

End Sub

这样可以更容易地编写代码,同时保持代码有序并加快速度。

答案 1 :(得分:1)

是。以下可能不是最好的代码,但似乎您的源单元格不是连续可循环的...

Dim SomeArray(X) As String      'Define "X" suitably for your input cells

SomeArray(0) = "D9"
SomeArray(1) = "D10"
SomeArray(2) = "J9"
SomeArray(3) = "J10"
etc...

然后循环遍历源单元格数组并复制每一个:

curOffset=0
For i = LBound(SomeArray) To UBound(SomeArray)
    Worksheets("FORM TEMPLATE").Range(SomeArray(i)).Copy
    Worksheets("Data").Range("A2").Offset(0,curOffset).PasteSpecial xlPasteValues
    curOffset = curOffset + 1
Next i

答案 2 :(得分:0)

正如JetSetJim所说 - 你的源细胞不易识别。

Sub Test()

    Dim rToCopy As Range
    Dim rCell As Range
    Dim lColNum As Long

    lColNum = 1
    With ThisWorkbook
        Set rToCopy = .Worksheets("FORM TEMPLATE").Range("D9:D10,J9:J11")
        With .Worksheets("Data")
            For Each rCell In rToCopy
                .Cells(2, lColNum) = rCell.Value
                lColNum = lColNum + 1
            Next rCell
        End With
    End With

End Sub

答案 3 :(得分:0)

当我使用一组无法自动确定的随机值时,我喜欢使用的方法是从常量创建一个数组。在你的情况下,像:

Const csSource as string = "D9|D10|J9|J10|J11"

Dim varSource as Variant, i as integer
Dim rngTarget as range

varsource = split(cssource,"|")
set rngTarget = worksheets("Data").range("A2")

for i = 0 to ubound(varSource)
    rngtarget.value = worksheets("Form Template").range(varsource(i)).value
    set rngTarget = rngTarget.offset(0, 1)
next i

您可以根据需要在顶部的常量中添加新的源值。这比其他方法更重要,因为它使用了变体,但是编码和维护起来要容易得多。

答案 4 :(得分:0)

  • 创建一个数组来保存数据
  • 调整数组大小以匹配范围
  • 中的单元格数
  • 循环
  • 范围内的单元格
  • 递增计数器
  • 将当前单元格的值添加到数组
  • 调整目标范围的大小以适合数据并将数组指定给此范围
Sub LoopData()
    Dim Data()
    Dim x As Long
    Dim c As Range

    With Worksheets("FORM TEMPLATE").Range("D9,D10,J9:J11")
        ReDim Data(1 To .Cells.Count)

        For Each c In .Cells
            x = x + 1
            Data(x) = c.Value
        Next
        Worksheets("Data").Range("B2").Resize(1, UBound(Data)) = Data
    End With

End Sub