Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7618817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:30:09+00:00 2026-05-31T03:30:09+00:00

I have the following code that loops trough a range and formats some PivotItems

  • 0

I have the following code that loops trough a range and formats some PivotItems, according to the value of the range. The code jumps from one sheet (with the students name and answers) to the other (with the pivot table with two RowLabels, QuestionNo and StudentName), and when it finds the correct PivotItem, change its format.

Sub Macro1()

    Dim student As String, q1 As String
    Dim pt as PivotTable

    Worksheets("SheetPivot").Activate
    Set pt = ActiveSheet.PivotTables(1)

    'Jumps to the Sheet with the range to loop
    Worksheets("QuizState").Activate
    'Goes to the first value
    Range("A1").Select

    'Sarts the loop
    Do Until IsEmpty(ActiveCell.Value)
        'Determines what type of value is.
        student = ActiveCell.Value 
        q1 = ActiveCell.Offset(0,1).Value
        If q1 = "WRONG" then
             'Jumps to Sheet with PivotTable
              Worksheets("SheetPivot").Activate
              pt.PivotFields("StudentName").PivotItems(student).LabelRange.Select
              Selection.Interior = 65535  'Yellow
              Worksheets("QuizState").Activate
        End If
        ActiveCell.Offset(1,0).Select
    Loop
 End Sub

Now, for some reason it works sometimes, sometimes it returns a Run-time error '1004': Unable to get the PivotItems property of the PivotField Class which I guess is due to not finding the student item, but it sure is there.

Can someone help me to debbug the code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:30:11+00:00Added an answer on May 31, 2026 at 3:30 am

    Can someone help me to debbug the code?

    I can help you debug it if you can upload a sample of the workbook in wikisend.com and share the link here?

    which I guess is due to not finding the student item, but it sure is there.

    Try this

    student = Trim(ActiveCell.Value)
    

    Also you can rewrite your code without using .Select and .Activate. Your code will run much faster 🙂 For example (THE BELOW CODE IS UNTESTED)

    Sub Macro1()
        Dim student As String, q1 As String
        Dim pt As PivotTable
        Dim ws1 As Worksheet, ws2 As Worksheet
        Dim ws2LastRow As Long, i As Long
    
        Set ws1 = Worksheets("SheetPivot")
        Set pt = ws1.PivotTables(1)
    
        Set ws2 = Worksheets("QuizState")
    
        With ws2
            ws2LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
            For i = 1 To ws2LastRow
                'Determines what type of value is.
                student = Trim(.Range("A" & i).Value)
                q1 = UCase(Trim(.Range("B" & i).Value))
                If q1 = "WRONG" Then
                    'Jumps to Sheet with PivotTable
                    pt.PivotFields("StudentName").PivotItems(student).LabelRange.Interior = 65535  'Yellow
                End If
            Next
        End With
     End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code snippet from my PowerShell script that... Loops through a
I have following code that does not work due to a being a value
I have the following snippet of code that loops through the jQuery animate function
I have some image processing code that loops through 2 multi-dimensional byte arrays (of
I have a timer that loops through following code every second. this code reads
I have code that looks like the following: //unrelated code snipped resolver.reset(new tcp::resolver(iosvc)); tcp::resolver::query
I have code that looks like the following, which works fine for displaying the
I have code that looks like the following: <form id=MyForm name=MyForm method=post action=index.php> <input
I have following code snippet that i use to compile class at the run
I have the following code that shows either a bug or a misunderstanding on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.