VBA newbie

Lindsey Saddler 1 Reputation point
2021-09-29T20:02:15.167+00:00

I am trying to create an invoice from a pivot table and I watched a YouTube video using a VBA. However, when I wrote the code it doesn't work and I have no idea why. Please help!!!!!

0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Lindsey Saddler 1 Reputation point
    2021-09-29T21:20:21.763+00:00

    @John Korchok Here is the code

    Private Sub CommandButton1_Click()

    Dim wks As Worksheet
    Dim pvt As PivotTable

    For Each wks In ThisWorkbook.Worksheets
    For Each pvt In wks.PivotTables
    pvt.RefreshTable
    Next pvt
    Next wks

    End Sub

    Private Sub Contractor_Change()

    End Sub

    Private Sub Refresh_Click()

    End Sub

    Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)

    Dim rng As Range
    Dim wks As Worksheet
    Set wks = Sheets("Contractor")
    Set rng = wks.Range("A4")

    ComboBox1.List = wks.Range(rng.Address, rng.End(xlDown).Address).Value

    End Sub

    Private Sub ComboBox1_Change()

    Application.ScreenUpdating = False  
    Application.DisplayAlerts = False  
    
    Dim objPT As PivotTable, strPitem As String  
    Dim x As Integer  
    
    'Set String equal to ComboBox  
    strPitem = ComboBox1.Text  
    

    With ActiveSheet
    Set objPT = .PivotTables(1)
    ' specify with field
    With objPT.PivotFields("Contractor")
    .AutoSort xlManual, "Contractor"

        'loop through specified field's items & hide them all  
    For x = 0 To .PivotItems.Count  
        On Error Resume Next 'error trap in case of no data  
        If .PivotItems(x).Name = strPitem Then  
        .PivotItems(strPitem).Visible = True  
        Else  
        .PivotItems(x).Visible = False  
        End If  
    Next x  
    
        On Error GoTo 0  
    
    End With  
    

    End With

    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

    End Sub

    0 comments No comments

  2. Lindsey Saddler 1 Reputation point
    2021-09-30T15:04:20.187+00:00
    0 comments No comments