从重复表中提取特定数据

时间:2019-10-25 10:42:04

标签: excel vba

A test export showing 2 sample points as they come from the software我将100多个样本点中的数据导出到excel中。我想将特定信息从这些信息中提取到汇总表中。 每个采样点都作为离散表导出,并且这些表每11行重复一次。即,如果b4包含样品1的分析物(A)值,则b15包含样品2的(A)值
我希望能够提取所有A值以及其他值。在BD行3-7列中总共发现6种被分析物,然后向下重复11个单元格以创建一个表(将11个单元格中的数据向下复制到新表的第二行中,依此类推)This is an example from a previous report where I manually pulled all of the data and created a table

我已经成功创建了一个宏,可以将这些数据从第一个样本转移到第二个表中,我相信我需要重复下一个函数,但是我无法解决

Private Sub CommandButton1_Click()
Sheets("Sheet1").Range("D3").Copy Destination:=Sheets("Sheet2").Range("B2")
Sheets("Sheet1").Range("B4").Copy Destination:=Sheets("Sheet2").Range("C2")
Sheets("Sheet1").Range("B5").Copy Destination:=Sheets("Sheet2").Range("D2")
Sheets("Sheet1").Range("B6").Copy Destination:=Sheets("Sheet2").Range("E2")
Sheets("Sheet1").Range("B7").Copy Destination:=Sheets("Sheet2").Range("F2")
End Sub

这是我目前拥有的宏

1 个答案:

答案 0 :(得分:0)

根据要求,应执行以下操作:

Private Sub CommandButton1_Click()

    Dim sourcesheet As Worksheet
    Dim destsheet As Worksheet
    Dim repeat_x_times As Long, loopcounter As Long
    Dim onelocation As Long, spread As Long
    Dim locations As Variant
    Dim locationlist As String

    '---------------------------------------
    '     edit these as you see fit
    '---------------------------------------
    Set sourcesheet = Sheets("Sheet1")
    Set destsheet = Sheets("Sheet2")
    locationlist = "D3,B4,B5,B6,B7"
    spread = 11
    repeat_x_times = 6
    '---------------------------------------

    locations = Split(locationlist, ",")

    With sourcesheet

        For loopcounter = 1 To repeat_x_times
            For onelocation = 0 To UBound(locations)
                .Range(locations(onelocation)).offset((loopcounter - 1) * spread, 0).Copy destsheet.Cells(loopcounter, 2 + onelocation)
                'or if you don't need formatting, just values (much quicker):
                'destsheet.Cells(loopcounter, 2 + onelocation).Value = .Range(locations(onelocation)).offset((loopcounter - 1) * spread, 0).Value
            Next
        Next

    End With
End Sub

我已根据您的需要对其进行了轻松配置,只需编辑变量即可:

locationlist是您要提取的单元格列表,以它们在sheet2上的输出顺序为准。

spread是重复之间的偏移量。

repeat_x_times是要输出的行数。

我已经使用了与现有代码相似的copy语句,但是如果您不想复制格式等,则可以使用值开关。我已经注释掉了代码,但是如果您想使用它,请取消注释它,并注释掉copy行。

相关问题