+ Reply to Thread
Results 1 to 4 of 4

vba in commandbutton_click event to only allow to occur in certain cells question

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-22-2009
    Location
    Manitoba
    MS-Off Ver
    Office 2010
    Posts
    524

    vba in commandbutton_click event to only allow to occur in certain cells question

    I think this will be a simple answer, least I hope, but I am drawing a blank at the moment! With this very simple code below, how do I prevent this from occurring in a certain range of cells? I've tried "If Not Intersect(Target, Range("sheet1UV")) Is Nothing Then" but it did not work. Thanks for any help!
    Private Sub CommandButton1_Click()
    Application.EnableEvents = False
    For Each c In Selection
    c.Activate
    ActiveCell.Formula = "P"
    Next c
    Application.EnableEvents = True
    End Sub

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: vba in commandbutton_click event to only allow to occur in certain cells question

    Hi, thecdnmole,

    maybe like this (although I would like to have more control over the range the code has to work on as using Selection):

    Private Sub CommandButton1_Click()
    Dim c As Range
    
    If ActiveSheet.Name <> "Sheet1" Then Exit Sub
    Application.EnableEvents = False
    For Each c In Selection
      If Not Intersect(c, Range("UV:UV")) Is Nothing Then c.Value = "P"
    Next c
    Application.EnableEvents = True
    
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

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

    Re: vba in commandbutton_click event to only allow to occur in certain cells question

    Are you able to use code to determine what cells to change, or do you have to use selection?

  4. #4
    Forum Contributor
    Join Date
    08-22-2009
    Location
    Manitoba
    MS-Off Ver
    Office 2010
    Posts
    524

    Re: vba in commandbutton_click event to only allow to occur in certain cells question

    Thanks HaHoBe! I tweaked it a bit to suit and works great!
    Private Sub CommandButton1_Click()
    Application.EnableEvents = False
    For Each c In Selection
    If Intersect(c, Range("sheet1UV")) Is Nothing Then c.Value = "P"
    Next c
    Application.EnableEvents = True
    End Sub
    Davesexcel, I need to use selection, as the person would select one or more cells and then click on the commandbutton to input the P.

+ 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. program a formula to count number of times event occur before a specific time.
    By yinxzon in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-17-2014, 04:15 AM
  2. Determining how many times an event occur
    By DrStrangelove in forum Excel General
    Replies: 2
    Last Post: 09-18-2009, 03:46 PM
  3. commandbutton_click based on cell contents
    By ShredDude in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-21-2007, 02:56 AM
  4. When does the Workbook_Open event occur?
    By Conan Kelly in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-09-2006, 06:49 PM
  5. commandbutton_click problem
    By Daniel R. Young in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-09-2005, 12:05 AM

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