+ Reply to Thread
Results 1 to 11 of 11

How to activate a code only on certain sheets

Hybrid View

  1. #1
    Registered User
    Join Date
    05-31-2013
    Location
    Budapest, Hungary
    MS-Off Ver
    Excel 2007
    Posts
    5

    How to activate a code only on certain sheets

    Hi,
    I am totally new to the forum and to VBA as well. I am preparing an Excel file and for certain functions I need macros, so I looked up one on the forums modified it a bit, but I still need to do something that exceeds my knowledge (converging to zero anyway). I have decided to learn a bit of VBA later on but as this matter is urgent I don't have enough time to do it all by myself.
    Here's the thing.

    It's basically a form with fields to fill in. Some of these are mandatory ones, so I entered a macro for that (see below). What I need is a little addition. There are 4 sheets in the form and the client will only fill in one of them in most of the cases. However, the macro entered, which pops up upon saving (saying that all mandatory cells need to be filled in) watches the whole file with all 4 sheets. I would like something, with which this macro with the pop-up is only activated on the sheets that are being used/filled in. Something like "if at least one cell is filled in on sheet x, activate macro). Or something that fits the purpose. Here's the code so far. I appreciate all the help.

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Cellcontents = Sheets(1).Range("B13,G9,G13,B18,B20,B27,B30,B32,F27,B37,B39,F38").Value
    If Cellcontents = "" Then
        Cancel = False
        MsgBox "Input required in mandatory cells! Saisie requise dans les cellules obligatoires!", vbOKOnly, "Attention!"
        Exit Sub
    End If
    
    Cellcontents = Sheets(2).Range("B13,G9,G13,B20,B22,F30,B33,F33,B35,E35,H35,B43,B45,F44").Value
    If Cellcontents = "" Then
        Cancel = False
        MsgBox "Input required in mandatory cells! Saisie requise dans les cellules obligatoires!", vbOKOnly, "Attention!"
        Exit Sub
    End If
    
    Cellcontents = Sheets(3).Range("B13,G9,G13,B20,B22,B29,B31,B34,F31,G29,F34,B43,B45,F44").Value
    If Cellcontents = "" Then
        Cancel = False
        MsgBox "Input required in mandatory cells! Saisie requise dans les cellules obligatoires!", vbOKOnly, "Attention!"
        Exit Sub
    End If
    
    Cellcontents = Sheets(4).Range("B13,G9,G13,B17,B21,B23,B26,B28,F21,F23,F24,E26,F28,D31,B33,E33,B35,B37,B39,F39,B42,G42,B45,F45,B47,G47,B50,G49,G51,B53,G53,B55,G55,B57,G57,B59,G59,B62,G62,B64,B66,G66,B84,B86,F85").Value
    If Cellcontents = "" Then
        Cancel = False
        MsgBox "Input required in mandatory cells! Saisie requise dans les cellules obligatoires! ", vbOKOnly, "Attention!"
        Exit Sub
    End If
    
    End Sub

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: How to activate a code only on certain sheets

    probably something like
    if not sheets(1).usedrange = "" or sheets(1).usedrange.cells.count >1 then
       'do code
    end if

  3. #3
    Registered User
    Join Date
    05-31-2013
    Location
    Budapest, Hungary
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: How to activate a code only on certain sheets

    Hi yudlugar,

    Thanks for the help. I tried what you suggested but it doesn't seem to work. A dialogue window pops up saying "Runtime error 438, object doesn't support this property or method." What might I have done wrong?

  4. #4
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: How to activate a code only on certain sheets

    No idea, I just double checked the code and I don't get an error, can you upload your workbook and/or post the code you tried?

    To add an attachment, when replying click go advanced and then attachments.

  5. #5
    Registered User
    Join Date
    05-31-2013
    Location
    Budapest, Hungary
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: How to activate a code only on certain sheets

    The file is confidential but here is the code for the first sheet. As I am a total noob to this, I tried the whole line you suggested and the two conditions separately. Really need to learn this stuff later...
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Cellcontents = Sheets(1).Range("B13,G9,G13,B18,B20,B27,B30,B32,F27,B37,B39,F38").Value
    If Sheets(1).UsedRangeCells.Count > 1 Then
        If Cellcontents = "" Then
            Cancel = False
            MsgBox "Input required in mandatory cells! Saisie requise dans les cellules obligatoires!", vbOKOnly, "Attention!"
            Exit Sub
        End If
    End If
    End Sub
    edited: I have deleted an extra "End If" inserted here by accident, it is not like that in VBA.
    Last edited by mastero87; 06-04-2013 at 05:00 AM.

  6. #6
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: How to activate a code only on certain sheets

    Note, I don't think what the code you are using will do what you want. Cellcontents will be equal to the value of B13 only, the values of the other cells are irrelevant

  7. #7
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: How to activate a code only on certain sheets

    May be try like this... (Untested Code...)

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim vMyRng As Variant, vRes As Variant
    
    With Application
        Set vMyRng(1) = Sheets(1).Range("B13,G9,G13,B18,B20,B27,B30,B32,F27,B37,B39,F38")
            vRes(1) = .CountA(vMyRng)
        Set vMyRng(2) = Sheets(2).Range("B13,G9,G13,B20,B22,F30,B33,F33,B35,E35,H35,B43,B45,F44")
            vRes(2) = .CountA(vMyRng)
        Set vMyRng(3) = Sheets(3).Range("B13,G9,G13,B20,B22,B29,B31,B34,F31,G29,F34,B43,B45,F44")
            vRes(3) = .CountA(vMyRng)
        Set vMyRng(4) = Sheets(4).Range("B13,G9,G13,B17,B21,B23,B26,B28,F21,F23,F24,E26,F28,D31,B33,E33,B35,B37,B39,F39,B42,G42,B45,F45,B47,G47,B50,G49,G51,B53,G53,B55,G55,B57,G57,B59,G59,B62,G62,B64,B66,G66,B84,B86,F85")
            vRes(4) = .CountA(vMyRng)
    End With
    
    For i = 1 To 4
        If vRes(1) <> vMyRng(1).Cells.Count Then GoTo EndOfCode
    Next i
    
    Exit Sub
        
    EndOfCode:
        MsgBox "Input required in mandatory cells! Saisie requise dans les cellules obligatoires!", vbOKOnly, "Attention!"
    
    End Sub


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  8. #8
    Registered User
    Join Date
    05-31-2013
    Location
    Budapest, Hungary
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: How to activate a code only on certain sheets

    Hi Sixthsense

    Thanks for the suggestion. I've copy-pasted your code but it doesn't seem to work, upon trying to save the file, it says "Run-time error 13: Type mismatch" and it highlights the line Set vMyRng(1)... as wrong.

  9. #9
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: How to activate a code only on certain sheets

    Try this revised and tested one....

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim vMyRng() As Range, vRes() As Long
    
    ReDim vMyRng(1 To 4)
    ReDim vRes(1 To 4)
    
    With Application
        Set vMyRng(1) = Sheets(1).Range("B13,G9,G13,B18,B20,B27,B30,B32,F27,B37,B39,F38")
            vRes(1) = .CountA(vMyRng(1))
        Set vMyRng(2) = Sheets(2).Range("B13,G9,G13,B20,B22,F30,B33,F33,B35,E35,H35,B43,B45,F44")
            vRes(2) = .CountA(vMyRng(2))
        Set vMyRng(3) = Sheets(3).Range("B13,G9,G13,B20,B22,B29,B31,B34,F31,G29,F34,B43,B45,F44")
            vRes(3) = .CountA(vMyRng(3))
        Set vMyRng(4) = Sheets(4).Range("B13,G9,G13,B17,B21,B23,B26,B28,F21,F23,F24,E26,F28,D31,B33,E33,B35,B37,B39,F39,B42,G42,B45,F45,B47,G47,B50,G49,G51,B53,G53,B55,G55,B57,G57,B59,G59,B62,G62,B64,B66,G66,B84,B86,F85")
            vRes(4) = .CountA(vMyRng(4))
    End With
    
    For i = 1 To 4
        If vRes(1) And vRes(1) <> vMyRng(1).Cells.Count Then GoTo EndOfCode
    Next i
    
    Exit Sub
        
    EndOfCode:
        MsgBox "Input required in mandatory cells! Saisie requise dans les cellules obligatoires!", vbOKOnly, "Attention!"
    
    End Sub

  10. #10
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: How to activate a code only on certain sheets

    You need UsedRange.Cells not UsedRangeCells
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Cellcontents = Sheets(1).Range("B13,G9,G13,B18,B20,B27,B30,B32,F27,B37,B39,F38").Value
    if not sheets(1).usedrange = "" or sheets(1).usedrange.cells.count >1 then
        If Cellcontents = "" Then
            Cancel = False
            MsgBox "Input required in mandatory cells! Saisie requise dans les cellules obligatoires!", vbOKOnly, "Attention!"
            Exit Sub
        End If
    End If
    End Sub

  11. #11
    Registered User
    Join Date
    05-31-2013
    Location
    Budapest, Hungary
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: How to activate a code only on certain sheets

    Ouch, forgot that. Thanks, I'll try again.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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