将两个柱子组合成两个柱子
我有列A:ALC
填充数据,每列有不同数量的行。 如果可能的话,我需要一个将成对堆叠两列的macros。 例如,直接在列A
下的列B
和直接在列B
下的列B
以及对于所有列A:ALC
等等。
COLUMN A COLUMN B COLUMN C COLUMN D ROW 1 2598 F800 2599 F800 ROW 2 2598 K1300 2599 K1300 ROW 3 2598 S1000RR 2599 R900 ROW 4 2598 G650 2599 G650 ROW 5 2598 R1200 2599 K1600 ROW 6 2599 S1000 ROW 7 2599 HP2 ROW 8 2599 R1200
每对列有相同数量的数据(例如A
列和B
列有8行, C
列和D
列有5行,依此类推),但在许多列对之间行数显然不同。 数据中没有空白。
当我运行macros你给Excel显示这个:
运行时错误“13”:types不匹配
可能是什么问题?
注 :有一些只有一对数据的列,即只有第一行的数据。
这就是我需要输出的样子:
COLUMN A COLUMN B ROW 1 2598 F800 ROW 2 2598 K1300 ROW 3 2598 S1000RR ROW 4 2598 G650 ROW 5 2598 R1200 ROW 6 2599 S1000 ROW 7 2599 HP2 ROW 8 2599 R1200 ROW 9 2599 F800 ROW 10 2599 K1300 ROW 11 2599 R900 ROW 12 2599 G650 ROW 13 2599 K1600
如果A:ALC的数据范围已满,那么这个变体数组代码将很快在A和B列中形成新的范围
注意警告重新填满,如果遇到空白或单个单元格列的代码将失败,因为不能创build变体数组。 如果是这种情况,那么我将需要添加范围testing,所以请指教。
[已更新以处理空白区域和/或单个单元格]
Sub Combine() Dim OrigA Dim OrigB Dim strA As String Dim strB As String Dim strDelim As String Dim lngCol As Long strDelim = "||" strA = Join(Application.Transpose(Range([a1], Cells(Rows.Count, "A").End(xlUp))), strDelim) strB = Join(Application.Transpose(Range([b1], Cells(Rows.Count, "b").End(xlUp))), strDelim) For lngCol = Columns("C").Column To Columns("ALC").Column - 2 Step 2 If Application.CountA(Columns(lngCol)) > 1 Then 'handle odd column range strA = strA & (strDelim & Join(Application.Transpose(Range(Cells(1, lngCol), Cells(Rows.Count, lngCol).End(xlUp))), strDelim)) Else 'handle odd column single cell If Len(Cells(1, lngCol)) > 0 Then strA = strA & (strDelim & Cells(1, lngCol).Value) End If If Application.CountA(Columns(lngCol + 1)) > 1 Then 'handle even column range strB = strB & (strDelim & Join(Application.Transpose(Range(Cells(1, lngCol + 1), Cells(Rows.Count, lngCol + 1).End(xlUp))), strDelim)) Else 'handle even column single cell If Len(Cells(1, lngCol + 1)) > 0 Then strB = strB & (strDelim & Cells(1, lngCol + 1).Value) End If Next OrigA = Application.Transpose(Split(strA, strDelim)) OrigB = Application.Transpose(Split(strB, strDelim)) [a1].Resize(UBound(OrigA, 1), 1) = OrigA [b1].Resize(UBound(OrigB, 1), 1) = OrigB End Sub