【问题标题】:Excel Vba Dynamic number of rows transpose to columnsExcel Vba 动态转置列的行数
【发布时间】:2014-11-09 19:25:41
【问题描述】:

我对 vba 比较陌生,并且发现很难为这个动态范围编写代码。输入数据的日期从 2014 年 11 月 7 日到 14 年 11 月 14 日,从 00:00 到 23:00 小时。我需要以这样的方式转置小时数,即对于每个日期,我每个小时都有值(在列中)。如果我不清楚这个问题,请告诉我。提前一百万谢谢

输入数据

Date        Hours      Name          %Value
11/07/14    00:00    P4127C11       20   
11/07/14    01:00    P4127C11       30
   .                                
   .                          
11/07/14    23:00    P4127C11       24     
11/08/14    00:00    P4127C11       15    
    .   
    .    
11/11/14    00:00    P4127C11       25      
    .        .         .            .    
    .        .         .            .     
11/11/14    23:00    P4127C11       31 

输出数据

Date       Name      00:00   01:00   02:00 . . .  .   23:00      
11/07/14  P4127C11    20       30      .      . ..  .  24    
11/08/14  P4127C11    15        .      .    .. . . .    .    
   .      
   .     
11/11/14  P4127C11    25        .     .    .          31     

【问题讨论】:

  • 感谢@Tmdean 我正在尝试格式化数据

标签: excel vba dynamic transpose


【解决方案1】:

这只是一个数据透视表。您可以通过创建一个数据透视表来解决此问题,其中行值中包含日期和名称,列值中包含时间,值中包含 %value 的最大值。

【讨论】:

  • 实际上我想为此编写一个 excelvba 代码,以便进一步操作输出
【解决方案2】:

@Nadeem,用 vba 解决它的可能方法之一是将数据加载到定义类型的数组中。看看这段代码,它应该提供一些你可以使用的想法。

Private Type tPivotValues
    sDate As String
    sName As String
    aHour() As String
End Type

Sub q26832297()

Dim i, j As Long
Dim aPivotValues() As tPivotValues
Dim iArrIndex As Integer


    ReDim aPivotValues(0 To 1)

    iArrIndex = 0
    'reading first date of first element
    aPivotValues(iArrIndex).sDate = Sheets(1).Cells(2, 1).Value
    aPivotValues(iArrIndex).sName = Sheets(1).Cells(2, 3).Value
    j = 0
    ReDim aPivotValues(iArrIndex).aHour(0 To 23)

    For i = 2 To Sheets(1).Cells(1, 1).End(xlDown).Row
        'if date in row different than in array element
        If aPivotValues(iArrIndex).sDate <> Sheets(1).Cells(i, 1).Value Then
            iArrIndex = iArrIndex + 1
            aPivotValues(iArrIndex).sDate = Sheets(1).Cells(i, 1).Value
            aPivotValues(iArrIndex).sName = Sheets(1).Cells(i, 3).Value
            'setting hours counter to 0
            j = 0
            ReDim aPivotValues(iArrIndex).aHour(0 To 23)
        End If
        If aPivotValues(iArrIndex).sDate = Sheets(1).Cells(i, 1).Value Then
            aPivotValues(iArrIndex).aHour(j) = Sheets(1).Cells(i, 4).Value
            j = j + 1
        End If
    Next i

    'printing data to different sheet
    For i = LBound(aPivotValues) To UBound(aPivotValues)
        Sheets(2).Cells(i + 1, 1).Value = aPivotValues(i).sDate
        Sheets(2).Cells(i + 1, 2).Value = aPivotValues(i).sName
        For j = LBound(aPivotValues(i).aHour) To UBound(aPivotValues(i).aHour)
            Sheets(2).Cells(i + 1, j + 3).Value = aPivotValues(i).aHour(j)
        Next j
    Next i

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    • 2016-11-06
    相关资源
    最近更新 更多