我的VBA技能很新鲜,因为这是我一直在努力改进的。
我已经搜索并获得了编码的一些部分,但是,我不能做我需要的。 (每个条形代表excel表格上的单元格
我在第1行有一个标题文件,这些标题是动态的;
Example of Raw Data
如您所见,每个标题下面都有一个随机行数。
我知道这并不复杂,但是当我需要根据每个标题的行数来计算,复制和粘贴标题时,我很挣扎。
问候。
答案 0 :(得分:1)
假设您的数据位于Sheet1
,并且您希望结果为Sheet2
,请尝试以下代码:
Sub RangetoColumn()
Dim lastRow As Long, lastColumn As Long
Dim CurrentSheet As Worksheet, TargetSheet As Worksheet
Dim i As Long, j As Long, Count As Long
Dim colHeader As String
Set CurrentSheet = ThisWorkbook.Worksheets("Sheet1") '-->sheet with data
Set TargetSheet = ThisWorkbook.Worksheets("Sheet2") '-->sheet to display result
lastColumn = CurrentSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Count = 1
For i = 1 To lastColumn
lastRow = CurrentSheet.Cells(Rows.Count, i).End(xlUp).Row
If lastRow > 1 Then '-->check for data below header
colHeader = CurrentSheet.Cells(1, i).Value
For j = 1 To lastRow - 1
TargetSheet.Range("A" & Count).Value = colHeader
TargetSheet.Range("B" & Count).Value = CurrentSheet.Cells(j + 1, i).Value
Count = Count + 1
Next j
End If
Next i
End Sub