+ Reply to Thread
Results 1 to 13 of 13

VBA script not running with blocked cells (not the ones the vba script is using)

Hybrid View

  1. #1
    Registered User
    Join Date
    03-16-2021
    Location
    Germany
    MS-Off Ver
    O365
    Posts
    26

    Question VBA script not running with blocked cells (not the ones the vba script is using)

    Hi,

    so I'm running a vba script which should change the color of a cell after the user has been modifying the sheet.
    The cells the script is coloring in is not protected, but other cells in the same row. (7+)

    Without protection the script is running fine, with I'm running into an error (request to debug).
    I have no clue why.

    Option Explicit
    
    Public Const FirstRowNumber As Long = 16
    
    Sub ColorData(SheetName As String)
       If SheetName = vbNullString Then Exit Sub
       Dim ws As Worksheet, SheetExist As Boolean, I As Long, LastUsedRow, DayString As String
       
       For Each ws In Worksheets
          If ws.Name = SheetName Then
             SheetExist = True
             Exit For
          End If
       Next ws
       If SheetExist = False Then Exit Sub
       
       LastUsedRow = ws.Cells(ws.Rows.Count, 11).End(xlUp).Row
    
       'Schleife zum Zellen entf?rben
       For I = FirstRowNumber To LastUsedRow
           ws.Range(ws.Cells(I, 3), ws.Cells(I, 6)).Interior.ColorIndex = 2
           ws.Range(ws.Cells(I, 3), ws.Cells(I, 6)).Interior.Pattern = xlSolid
           DayString = ws.Cells(I, 2).Value
           
           Select Case DayString
               Case "":    ws.Range(ws.Cells(I, 3), ws.Cells(I, 6)).Interior.ColorIndex = 36
               Case "HO":  ws.Range(ws.Cells(I, 3), ws.Cells(I, 6)).Interior.ColorIndex = 36
               Case "ho":  ws.Range(ws.Cells(I, 3), ws.Cells(I, 6)).Interior.ColorIndex = 36
               Case "DA":  ws.Range(ws.Cells(I, 3), ws.Cells(I, 6)).Interior.ColorIndex = 36
               Case "da":  ws.Range(ws.Cells(I, 3), ws.Cells(I, 6)).Interior.ColorIndex = 36
               Case "U":   ws.Range(ws.Cells(I, 3), ws.Cells(I, 6)).Interior.Pattern = xlLightUp
               Case "K":   ws.Range(ws.Cells(I, 3), ws.Cells(I, 6)).Interior.Pattern = xlLightUp
               Case "S":   ws.Range(ws.Cells(I, 3), ws.Cells(I, 6)).Interior.Pattern = xlLightUp
               Case "GT":  ws.Range(ws.Cells(I, 3), ws.Cells(I, 6)).Interior.Pattern = xlLightUp
               Case "Sa":  ws.Range(ws.Cells(I, 3), ws.Cells(I, 6)).Interior.Pattern = xlLightUp
               Case "So":  ws.Range(ws.Cells(I, 3), ws.Cells(I, 6)).Interior.Pattern = xlLightUp
               Case Else:  ws.Range(ws.Cells(I, 3), ws.Cells(I, 6)).Interior.Pattern = xlLightUp
          End Select
       Next I
    End Sub
    I can and will provide any necessary data anybody needs to solve this puzzle.
    Hope somebody can help me,
    Edward

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,485

    Re: VBA script not running with blocked cells (not the ones the vba script is using)

    Unprotect the sheet at the beginning of your code, then protect it again at the end of the code.

  3. #3
    Registered User
    Join Date
    03-16-2021
    Location
    Germany
    MS-Off Ver
    O365
    Posts
    26

    Re: VBA script not running with blocked cells (not the ones the vba script is using)

    Quote Originally Posted by davesexcel View Post
    Unprotect the sheet at the beginning of your code, then protect it again at the end of the code.
    Changed it to
      For I = FirstRowNumber To LastUsedRow
           ws.Range(ws.Cells(I, 7), ws.Cells(I, 16)).Locked = False
           ws.Range(ws.Cells(I, 3), ws.Cells(I, 6)).Interior.ColorIndex = 2
           ws.Range(ws.Cells(I, 3), ws.Cells(I, 6)).Interior.Pattern = xlSolid
           DayString = ws.Cells(I, 2).Value
           ws.Range(ws.Cells(I, 7), ws.Cells(I, 16)).Locked = True
    , but still getting the same error.

    Attachment 814919

  4. #4
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,485

    Re: VBA script not running with blocked cells (not the ones the vba script is using)

    Where are you unprotecting the sheet?

    Search for "excel vba unprotect sheet"

  5. #5
    Registered User
    Join Date
    03-16-2021
    Location
    Germany
    MS-Off Ver
    O365
    Posts
    26

    Re: VBA script not running with blocked cells (not the ones the vba script is using)

    Quote Originally Posted by davesexcel View Post
    Where are you unprotecting the sheet?

    Search for "excel vba unprotect sheet"
    I thought it was:
    ws.Range(ws.Cells(I, 7), ws.Cells(I, 16)).Locked = False
    But after using your searchterm I found this:
    Sheets("Sheet1").Unprotect Password:="ADSBP"
    Do I need only the 2nd one, or both?

  6. #6
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,485

    Re: VBA script not running with blocked cells (not the ones the vba script is using)

    Just the one to unprotect the sheet

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,274

    Re: VBA script not running with blocked cells (not the ones the vba script is using)

    Did you select the option to allow formatting cells when protecting the sheet?
    Remember what the dormouse said
    Feed your head

  8. #8
    Registered User
    Join Date
    03-16-2021
    Location
    Germany
    MS-Off Ver
    O365
    Posts
    26

    Re: VBA script not running with blocked cells (not the ones the vba script is using)

    Quote Originally Posted by rorya View Post
    Did you select the option to allow formatting cells when protecting the sheet?
    How do I do that? I'm quite a noob with vba and just clobbering this together.

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,274

    Re: VBA script not running with blocked cells (not the ones the vba script is using)

    Did you protect the sheet in code originally? If not, just check that option when protecting the sheet in the UI. If you did do it in code, it's like this:

    activesheet.protect password:="whatever", allowformattingcells:=true

  10. #10
    Registered User
    Join Date
    03-16-2021
    Location
    Germany
    MS-Off Ver
    O365
    Posts
    26

    Re: VBA script not running with blocked cells (not the ones the vba script is using)

    Quote Originally Posted by rorya View Post
    Did you protect the sheet in code originally? If not, just check that option when protecting the sheet in the UI. If you did do it in code, it's like this:

    activesheet.protect password:="whatever", allowformattingcells:=true
    I did it using the GUI.
    Might that be the problem? Problem is, I need only to protect some cells, so nobody is overwriting the formulas.

  11. #11
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,274

    Re: VBA script not running with blocked cells (not the ones the vba script is using)

    When you protect the sheet in the UI, one of the options you can set is to allow formatting cells. Then your code should just work as is.

  12. #12
    Registered User
    Join Date
    03-16-2021
    Location
    Germany
    MS-Off Ver
    O365
    Posts
    26

    Re: VBA script not running with blocked cells (not the ones the vba script is using)

    Quote Originally Posted by rorya View Post
    When you protect the sheet in the UI, one of the options you can set is to allow formatting cells. Then your code should just work as is.
    And that was the fast solution
    Thank you very much.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. script to run the macro from Vb script with out opening the excel file
    By chandanp in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-27-2021, 03:33 AM
  2. Need A Fast Script To Copy 80000 Cells But My Current Script Is Too Slow
    By Genus Max in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-20-2019, 09:54 AM
  3. calling the vb script from the shell script(unix platform)
    By chandanp in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-21-2014, 06:21 AM
  4. Integrating Mainframe emulator script with excel's vba script
    By Himanshu Mishra in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-07-2012, 10:30 AM
  5. Create VBA script to short, dedicated button in excel for script?
    By realized in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-01-2009, 11:54 PM
  6. Script that edits the text printed on the button that runs the script
    By petalred in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-18-2008, 02:41 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1