VBA Transpose with filldown

时间:2017-12-28 14:05:49

标签: vba excel-vba excel

我想在下面进行数据转置,并保持第一列“连接”到值。

        Q1    Q2   Q3
Shop 1  100  90   110
Shop 2  90   110  130

Shop 1   Q1   100
Shop 1   Q2   90
Shop 1   Q3   110
Shop 2   Q1   90
Shop 2   Q2   110
Shop 2   Q3   130

我正在使用以下代码,它完全适用于最后两列,但我无法执行第一列。请问有人帮忙吗?

Sub test()
Dim r As Range, c As Range, dest As Range

With Worksheets(“Sheet1”)
Set r = Range(.Range(“C2”), .Range(“C2”).End(xlDown))

For Each c in r

‘Sales
Range(c, c.End(xlToRight)).Copy
With Worksheets(“Sheet1”)
Set dest = .Cells(Rows.Count, “O”).End(xlUp).Offset(1, 0)
dest.PasteSpecial Transpose:=True
End With

‘Quarters
Worksheets(“Sheet1”).Range(“C1:E1”).Copy
With Worksheets(“Sheet1”)
Set dest = .Cells(Rows.Count, “N”).End(xlUp).Offset(1, 0)
dest.PasteSpecial Transpose:=True
End With

Next c

End With

End Sub

1 个答案:

答案 0 :(得分:0)

诀窍是将数据视为一个表格,其顶部是标题行,左侧是标题列。然后对于“内部”表中的每一位数据(即没有标题左侧和顶部的位),您想要从左侧打印单元格,上面的单元格然后是数据

raise ValidationError({'yourfield': ['Your message']})