+ Reply to Thread
Results 1 to 4 of 4

Change function from on button click to when value entered

Hybrid View

  1. #1
    Registered User
    Join Date
    03-12-2015
    Location
    Khobar, Saudi Arabia
    MS-Off Ver
    2013
    Posts
    7

    Change function from on button click to when value entered

    fI seem to always have to start these with I don't know anything about coding so please bare with me.

    I have some code that does a funtion on a button click that looks like below

    Private Sub btn_Add_Click()
    Worksheets("Main").Unprotect
    Dim TargetCell As Range
    If WorksheetFunction.CountIf(Sheets("Main").Columns(4), TextBox1.Value) = 1 Then
        Set TargetCell = Sheets("Main").Columns(4).Find(TextBox1.Value, , xlValues, xlWhole).Offset(0, 1)
        TargetCell.Value = TargetCell.Value + 1
        TextBox1.Value = ""
        TextBox1.SetFocus
    Else
        MsgBox "Item Not Found"
    End If
    Worksheets("Main").Protect
    End Sub
    However, I would rather the function above be achieved when text is entered. I have the following code that functions this way but does something different. I tried to see if I can have the action do what it does above but without a button click.

    Dim IsActive As Boolean
    
    Sub CopyToCell()
    Worksheets("Main").Unprotect
    If UserForm2.TextBox1.Text = "" Then Exit Sub
    
        With Worksheets("Main")
            If Application.CountIf(.Range("D:D"), UserForm2.TextBox1.Text) = 0 Then
            .Range("D" & .Rows.Count).End(xlUp)(2).Value = UserForm2.TextBox1.Text
                Else
                MsgBox "That Item Already Exists"
            End If
        End With
    
    UserForm2.TextBox1.Text = ""
    UserForm2.TextBox1.SetFocus
    IsActive = False
    Worksheets("Main").Protect
    End Sub
    I have a mess on my hands. My new code looks like below but it keeps throwing an error on the If Worksheet.Function line.

    Dim IsActive As Boolean
    
    Private Sub Add_Freestyle()
    Worksheets("Main").Unprotect
    Dim TargetCell As Range
    If UserForm5.TextBox1.Text = "" Then Exit Sub
    
    With Worksheets("Main")
            If WorksheetFunction.CountIf(Sheets("Main").Columns(4), TextBox1.Value) = 1 Then
        Set TargetCell = Sheets("Main").Columns(4).Find(TextBox1.Value, , xlValues, xlWhole).Offset(0, 1)
        TargetCell.Value = TargetCell.Value + 1
        TextBox1.Value = ""
        TextBox1.SetFocus
    Else
        MsgBox "Item Not Found"
    End If
        End With
    
    UserForm5.TextBox1.Text = ""
    UserForm5.TextBox1.SetFocus
    IsActive = False
    Worksheets("Main").Protect
    End Sub
    If you need me to upload the file, I can do that. I give big thanks ahead of time to anyone willing to help this poor slob.
    Last edited by rmc71291; 04-12-2015 at 09:44 AM. Reason: Didn't have the code indicators per rules

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,378

    Re: Change function from on button click to when value entered

    Hi rmc,

    I think you need to learn about VBA Events. Then you need to see how they are triggered. I think you are looking for a worksheet On_Charnge event to trigger your code. Read these:
    http://www.tutorialspoint.com/vba/vba_events.htm and
    http://www.cpearson.com/excel/Events.aspx

    It seems to me you have learned that a button click can trigger some code but haven't yet found worksheet events.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: Change function from on button click to when value entered

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here
    Regards
    sktneer


    Treat people the way you want to be treated. Talk to people the way you want to be talked to.
    Respect is earned NOT given.

  4. #4
    Registered User
    Join Date
    03-12-2015
    Location
    Khobar, Saudi Arabia
    MS-Off Ver
    2013
    Posts
    7

    Re: Change function from on button click to when value entered

    Ok. I tried to learn VBA last night but obviously it will take longer. Can anyone try to help redo the 3rd set of code so that it works like the function in the 1st set of code but triggered on a change in a cell like the 2nd set of code?

+ 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. [SOLVED] Change protection options with a button click?
    By rrbee in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-16-2014, 11:42 AM
  2. How change label value for each click button ??
    By WILKER in forum Excel Programming / VBA / Macros
    Replies: 24
    Last Post: 11-02-2012, 12:25 PM
  3. Command Button to change the subsequent rows with each click
    By sand_electro in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-12-2012, 04:13 PM
  4. VBA to change writeup at the click of button.
    By arnab0711 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-18-2012, 03:19 AM
  5. [SOLVED] Undo Button Click change
    By Kiran in forum Excel General
    Replies: 2
    Last Post: 07-12-2005, 03:05 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