Results 1 to 4 of 4

Change function from on button click to when value entered

Threaded 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

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