gab*_*ngh 2 excel vba excel-vba
假设我有2列:
第1列(包含1000行):
U-0001
U-0002
Run Code Online (Sandbox Code Playgroud)
第二列(包含10行):
B01
B02
B03
Run Code Online (Sandbox Code Playgroud)
现在,我想生成两个这样的列(10*1000 = 10000行):
U-0001 B01
U-0001 B02
U-0001 B03
U-0002 B01
U-0002 B02
U-0002 B03
Run Code Online (Sandbox Code Playgroud)
这应该这样做:
Sub combineTwoCol()
' i,j,k are counters for first col, second col and the answer col in order.
k = 1
For i = 1 To 1000
For j = 1 To 10
Cells(k, "C").Value = Cells(i, "A").Value
Cells(k, "D").Value = Cells(j, "B").Value
k = k + 1
Next j
Next i
End Sub
Run Code Online (Sandbox Code Playgroud)
编辑:您应该注意到我假设您只有那两列是文件,如果没有将"A"和"B"更改为您需要的相应列.和"C"和"D"到两个输出列的位置.
此外,如果10和1000只是示例而不是真正的值,您总是可以像这样动态地找到它们:
'this return to the variable the last row in column A
LastRowColA = Range("A65536").End(xlUp).Row
Run Code Online (Sandbox Code Playgroud)
并替换1000为LastRowColA
这是一个公式版本:-
=IF(ROW()-ROW($A$1)<COUNTA(A:A)*COUNTA(B:B),OFFSET($A$1,(ROW()-ROW($A$1))/COUNTA(B:B),0)&" "&OFFSET($B$1,MOD(ROW()-ROW($A$1),COUNTA(B:B)),0),"")
Run Code Online (Sandbox Code Playgroud)
将其输入(例如)C1 并向下复制。
| 归档时间: |
|
| 查看次数: |
4297 次 |
| 最近记录: |