在表格中加入不相关的数据

时间:2016-12-02 07:38:08

标签: jquery excel excel-vba excel-2013 vba

我正在寻找一种自动创建表的解决方案,该表根据预定义的月份和结果列重复我们的客户名称。

我试图在vba中循环,但结果很慢且无法使用。我也尝试使用Microsoft查询并添加两个表,而不是加入表。这将一直有效,直到在基础数据中进行更新,这会导致第二个表消失。我正在寻找一种能够保持结构的稳定解决方案。

结果表用于构建连接公式,用于计算其他工作表中的匹配条目。

以下是数据的一个例子;

Customer names:
Adam
Anne

Month    Result
Jan      Pass
Jan      Fail
Jan      Exclude
Feb      Pass
Feb      Fail
Feb      Exclude

期望的输出:

Name  Month    Result
Adam  Jan      Pass
Adam  Jan      Fail
Adam  Jan      Exclude
Adam  Feb      Pass
Adam  Feb      Fail
Adam  Feb      Exclude
Anne  Jan      Pass
Anne  Jan      Fail
Anne  Jan      Exclude
Etc

非常感谢任何帮助!!

2 个答案:

答案 0 :(得分:0)

试试这个,希望你能找出细胞参考

Sub tabulate()
Dim custNames As Range
Dim data As Range

Set custNames = Range("A2:A3")
Set data = Range("A6:B11")

For i = 1 To custNames.Rows.Count
    For j = 1 To data.Rows.Count
        Range("E1").Offset((i - 1) * data.Rows.Count + (j - 1), 0) = custNames(i, 1)
        Range("F1").Offset((i - 1) * data.Rows.Count + (j - 1), 0) = data(j, 1)
        Range("G1").Offset((i - 1) * data.Rows.Count + (j - 1), 0) = data(j, 2)
    Next
Next
End Sub

答案 1 :(得分:0)

这将添加所有不同的组合:

Sub compare()
    Dim CustNames()
    Dim MonthName, count, row
    ReDim CustNames(1)
    CustNames(0) = Range("A2").Value
    CustNames(1) = Range("A3").Value
    row = 0
    Range("C1").Select
    For j = 1 To 12 Step 1
        count = 0
        For i = 0 To (UBound(CustNames) + 0.5) * 3 Step 3
            Select Case j
                Case 1
                    MonthName = "Jan"
                Case 2
                    MonthName = "Feb"
                Case 3
                    MonthName = "Mar"
                Case 4
                    MonthName = "Apr"
                Case 5
                    MonthName = "May"
                Case 6
                    MonthName = "Jun"
                Case 7
                    MonthName = "Jul"
                Case 8
                    MonthName = "Aug"
                Case 9
                    MonthName = "Sep"
                Case 10
                    MonthName = "Oct"
                Case 11
                    MonthName = "Nov"
                Case 12
                    MonthName = "Dec"
            End Select
            ActiveCell.Offset(row, 0).Range("A1").Value = CustNames(count)
            ActiveCell.Offset(row + 1, 0).Range("A1").Value = CustNames(count)
            ActiveCell.Offset(row + 2, 0).Range("A1").Value = CustNames(count)
            ActiveCell.Offset(row, 1).Range("A1").Value = MonthName
            ActiveCell.Offset(row + 1, 1).Range("A1").Value = MonthName
            ActiveCell.Offset(row + 2, 1).Range("A1").Value = MonthName
            ActiveCell.Offset(row, 2).Range("A1").Value = "Pass"
            ActiveCell.Offset(row + 1, 2).Range("A1").Value = "Fail"
            ActiveCell.Offset(row + 2, 2).Range("A1").Value = "Exclude"
            count = count + 1
            row = row + 3
        Next i
    Next j
End Sub