Excel VBA – Pivot table/chart 1 – Row fields control

Switching Row Fields using VBA code

Sample file downloadhttps://github.com/dannyyun/pivot-vba.git

  1. Switching row fields

Sub RollUpTimeBy()

    With ActiveSheet.Shapes("Drop Down 1").ControlFormat
        Select Case .ListIndex
            Case Is = 1
                YearView
            Case Is = 2
                MonthView
            Case Is = 3
                WeekView
            Case Is = 4
                DayView
        End Select
    End With
    
End Sub

2. Add ‘column’ to Row field. Note that ‘ClearRow’ sub is at the first of the line before adding a ‘column’ to Row field.

Sub YearView()
    
    ClearRow
    
    Dim pt As PivotTable
    Set pt = ActiveSheet.PivotTables(1)
    With pt.PivotFields("Year")
        .Orientation = xlRowField
        .Position = 1
    End With

End Sub

Sub MonthView()
    
    ClearRow
    
    Dim pt As PivotTable
    Set pt = ActiveSheet.PivotTables(1)
    With pt.PivotFields("Month")
        .Orientation = xlRowField
        .Position = 1
    End With

End Sub

Sub WeekView()
    
    ClearRow
    
    Dim pt As PivotTable
    Set pt = ActiveSheet.PivotTables(1)
    With pt.PivotFields("Week")
        .Orientation = xlRowField
        .Position = 1
    End With

End Sub

Sub DayView()
    
    ClearRow
    
    Dim pt As PivotTable
    Set pt = ActiveSheet.PivotTables(1)
    With pt.PivotFields("Date")
        .Orientation = xlRowField
        .Position = 1
    End With

End Sub

3. Clear Row field

Sub ClearRow()
    
    Dim pt As PivotTable
    Dim fld As Object
    Set pt = ActiveSheet.PivotTables(1)
    For Each fld In pt.RowFields
        Debug.Print fld.Name
        fld.Orientation = xlHidden
    Next fld

End Sub

Useful Excel Keyboard short cuts

Ctrl + F1Hide/Show the riboon
Ctrl + Pagedownmove to next worksheet
Ctrl + Pageupmove to prev. worksheet
Arrow keysmove from a cell to next cell, direction of arrow
Ctrl + Arrow keysmove to end of the cell from current position, direction of arrow
Shift + Arrow keysSelect the sells, direction of arrow
Ctrl + Shift + Arrow keysSelect multiple sells at once, direction fo arrow
Ctrl + Spcace barSelect the entire column
Shift + Spcae barSelect the entire row
Ctrl  + (-)Delete column or row
Ctrl + Shift + (+)Insert column or row

VLOOKUP4: MATCH to find column index number

Useful Excel Keyboard short cuts

Ctrl + F1 Hide/Show the riboon Ctrl + Pagedown move to next worksheet Ctrl + Pageup move to prev. worksheet Arrow keys move from a cell to next cell, direction of arrow Ctrl + Arrow keys move to end of the cell from current position, direction of arrow Shift + Arrow keys Select the sells,…

VLOOKUP3: Multiple columns with Array

Ctrl+Shift+Enter – Enter function with Array

Useful Excel Keyboard short cuts

Ctrl + F1 Hide/Show the riboon Ctrl + Pagedown move to next worksheet Ctrl + Pageup move to prev. worksheet Arrow keys move from a cell to next cell, direction of arrow Ctrl + Arrow keys move to end of the cell from current position, direction of arrow Shift + Arrow keys Select the sells,…

VLOOKUP2: Increment column index number with COLUMNS function

F2 – Edit mode in a cell Shift + Arrow keys – Select texts in a cell edit mode F9 – Evaluate function, so to display the result of the function

Useful Excel Keyboard short cuts

Ctrl + F1 Hide/Show the riboon Ctrl + Pagedown move to next worksheet Ctrl + Pageup move to prev. worksheet Arrow keys move from a cell to next cell, direction of arrow Ctrl + Arrow keys move to end of the cell from current position, direction of arrow Shift + Arrow keys Select the sells,…