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 8380599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:29:54+00:00 2026-06-09T16:29:54+00:00

I am using Active Sheet object of excel to loop through rows and columns.

  • 0

I am using Active Sheet object of excel to loop through rows and columns.

I need to change the background color of a cell but getting Application defined or object- defined error on this line

ws.Cells(rw, 4).Interior.ColorIndex = 0

This is the code I am using

Dim ws As Worksheet

Set ws = ActiveSheet

With ws


For rw = 7 To ws.Rows.Count

   For col = 2 To 12

    'Check the first column and if null then exit

    If ws.Cells(rw, 2) = "" Then
        Exit Sub
    End If

   'Check if article code is less than eight digits
    If Len(ws.Cells(rw, 4)) < 8 Then
        ws.Cells(rw, 4).Interior.ColorIndex = 3
    Else
        ws.Cells(rw, 4).Interior.ColorIndex = 0
    End If


  Next col

Next rw

End With

Any thoughts?

  • 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-06-09T16:29:55+00:00Added an answer on June 9, 2026 at 4:29 pm

    There is nothing wrong with the code. You are getting that error because your worksheet is protected. You need to unprotect the sheet and then change the cell colors. When you have finished changing the colors, you can then reprotect it. See this code.

    Sub Sample()
        Dim ws As Worksheet
        Dim Mypassword As String
    
        '~~> Change password here
        Mypassword = "Blah Blah"
    
        Set ws = ActiveSheet
    
        With ws
            '~~> Unprotect sheet
            .Unprotect Mypassword
            For rw = 7 To .Rows.Count
                For col = 2 To 12
                    'Check the first column and if null then exit
                    If .Cells(rw, 2).Value = "" Then Exit Sub
    
                    'Check if article code is less than eight digits
                    If Len(.Cells(rw, 4)) < 8 Then
                        .Cells(rw, 4).Interior.ColorIndex = 3
                    Else
                        .Cells(rw, 4).Interior.ColorIndex = 0
                    End If
                Next col
            Next rw
            '~~> Reprotect sheet
            .Protect Mypassword
        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 want to get only active sheet name from excel file using c#,but now
I am using excel to draw charts from c#, but i need the chart
Hi I'm using VBA in Excel and need to pass in the values from
Hi all am creating a excel using Microsoft office interop.and it creates files successfully.But
I am using active accessibility framework to enumerate all the controls for a given
I am using Active Record on CodeIgniter. I am confused on which approach I
I have a website which using active directory authorization on a windows server in
In WPF and SubSonic 2 using Active Record, I was able to bind to
I have a 3.0.7 app using active scaffold from github vhochstein/master. I am using
I’m trying to authenticate users onto a MVC3 application using Active Directory (without login

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.