我是VBA的新手。如何使用VBA根据另一个表中的数据生成表?例如:从table1生成table2,在那里我将获得Color键的所有可能组合;
表1包括:颜色,型号,年份。
在表2中,我想为color + model + year生成所有可能的组合
我设法编写了这段代码
Sub generatedinput()
Dim listA As Range
Dim listB As Range
Dim listC As Range
Range("D11:F999").Clear
Set listA = Range("I11", Range("I11").End(xlDown))
Set listB = Range("J11", Range("J11").End(xlDown))
Set listC = Range("K11", Range("K11").End(xlDown))
y = 11
For Each cellA In listA
For Each cellB In listB
For Each cellC In listC
Cells(y, 4).Value = cellA.Value
Cells(y, 5).Value = cellB.Value
Cells(y, 6).Value = cellC.Value
y = y + 1
Next
Next
Next
End Sub
但是也会生成用于清空的单元格。如何跳过空白单元格?
答案 0 :(得分:0)
这有点丑陋,但可以完成工作。您的代码已经关闭,您只需要检查空单元格即可。
Public Sub MakeComboTable()
Dim rColor As Range
Dim rModel As Range
Dim rYear As Range
Dim lCnt As Long
For Each rColor In Sheet1.Range("G4:G11").Cells
If Not IsEmpty(rColor.Value) Then
For Each rModel In Sheet1.Range("H4:H11").Cells
If Not IsEmpty(rModel.Value) Then
For Each rYear In Sheet1.Range("I4:I11").Cells
If Not IsEmpty(rYear.Value) Then
lCnt = lCnt + 1
Sheet1.Range("B1").Offset(lCnt, 0).Value = rColor.Value
Sheet1.Range("B1").Offset(lCnt, 1).Value = rModel.Value
Sheet1.Range("B1").Offset(lCnt, 2).Value = rYear.Value
End If
Next rYear
End If
Next rModel
End If
Next rColor
End Sub