+ Reply to Thread
Results 1 to 5 of 5

VBA CountIf logic checking Text Box Input

Hybrid View

  1. #1
    Registered User
    Join Date
    02-06-2014
    Location
    San Francisco, CA
    MS-Off Ver
    Excel 2007
    Posts
    17

    VBA CountIf logic checking Text Box Input

    Hi Everyone,

    Well I didn't think that I would be stopped here, but I'm having issues understanding the logic associated with basic excel functions when their used in VBA code. I'm trying to use the "CountIf" function inside my VBA code to count the total text boxes that have no value entered into them. I need to run this total through a later equation to account for the total number survey questions not answered. The logic should look like this:

    • Count if "textbox A" has no value
    +
    • Count if "textbox B" has no value
    + .....N = 20 text boxes.

    At the moment my attempts look like this:
    Formula: copy to clipboard
    EvalNotScored = (Application.WorksheetFunction.CountIf(PennShoulderEvalQ1TextBox, "") + Application.WorksheetFunction.CountIf(PennShoulderEvalQ2TextBox, "") +....


    The full equation would be: (60 - ((Total number empty text boxes) * 3))

    I've added my worksheet in question in case I'm not explaining clearly enough. The code is under our calculate button for our "Penn Shoulder" user form.

    Thank you Everyone!

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,705

    Re: VBA CountIf logic checking Text Box Input

    I think that you issue may be that line ** code is to long. You will need to amend with line breaks. Try this:

    EvalNotScored = (Application.WorksheetFunction.CountIf(PennShoulderEvalQ1TextBox, "") + _
        Application.WorksheetFunction.CountIf(PennShoulderEvalQ2TextBox, "") + _
        Application.WorksheetFunction.CountIf(PennShoulderEvalQ3TextBox, "") + _
        Application.WorksheetFunction.CountIf(PennShoulderEvalQ4TextBox, "") + _
        Application.WorksheetFunction.CountIf(PennShoulderEvalQ5TextBox, "") + _
        Application.WorksheetFunction.CountIf(PennShoulderEvalQ6TextBox, "") + _
        Application.WorksheetFunction.CountIf(PennShoulderEvalQ7TextBox, "") + _
        Application.WorksheetFunction.CountIf(PennShoulderEvalQ8TextBox, "") + _
        Application.WorksheetFunction.CountIf(PennShoulderEvalQ9TextBox, "") + _
        Application.WorksheetFunction.CountIf(PennShoulderEvalQ10TextBox, "") + _
        Application.WorksheetFunction.CountIf(PennShoulderEvalQ11TextBox, "") + _
        Application.WorksheetFunction.CountIf(PennShoulderEvalQ12TextBox, "") + _
        Application.WorksheetFunction.CountIf(PennShoulderEvalQ13TextBox, "") + _
        Application.WorksheetFunction.CountIf(PennShoulderEvalQ14TextBox, "") + _
        Application.WorksheetFunction.CountIf(PennShoulderEvalQ15TextBox, "") + _
        Application.WorksheetFunction.CountIf(PennShoulderEvalQ16TextBox, "") + _
        Application.WorksheetFunction.CountIf(PennShoulderEvalQ17TextBox, "") + _
        Application.WorksheetFunction.CountIf(PennShoulderEvalQ18TextBox, "") + _
        Application.WorksheetFunction.CountIf(PennShoulderEvalQ19TextBox, "") + _
        Application.WorksheetFunction.CountIf(PennShoulderEvalQ20TextBox, ""))
    VBA code only allows so many characters per line. IN any event, it is easier to read and analyze if the lines are broken. To make them continuous, you place a space and an underscore and then the enter key and the code continues to the next line.

    Alan
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    02-06-2014
    Location
    San Francisco, CA
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: VBA CountIf logic checking Text Box Input

    Hi Alan,

    Thank you for the insight into the underscore and enter key aspect. I had seen and incorporated that before, but I was at a loss as to understanding it. Unfortunately that alone does not seem to be correcting my CountIf function. I'm receiving the following error:

    Runtime Error 13.
    Type mismash.
    I've updated the VBA to reflect a more clearly identifiable codeset in the attachement. I hope this helps which is the same below:

    Formula: copy to clipboard
    EvalNotScored = (Application.WorksheetFunction.CountIf(PennShoulderEvalQ1TextBox, "") + _
    Application.WorksheetFunction.CountIf(PennShoulderEvalQ2TextBox, "") + _
    Application.WorksheetFunction.CountIf(PennShoulderEvalQ3TextBox, "") + _
    Application.WorksheetFunction.CountIf(PennShoulderEvalQ4TextBox, "") + _
    Application.WorksheetFunction.CountIf(PennShoulderEvalQ5TextBox, "") + _
    Application.WorksheetFunction.CountIf(PennShoulderEvalQ6TextBox, "") + _
    Application.WorksheetFunction.CountIf(PennShoulderEvalQ7TextBox, "") + _
    Application.WorksheetFunction.CountIf(PennShoulderEvalQ8TextBox, "") + _
    Application.WorksheetFunction.CountIf(PennShoulderEvalQ9TextBox, "") + _
    Application.WorksheetFunction.CountIf(PennShoulderEvalQ10TextBox, "") + _
    Application.WorksheetFunction.CountIf(PennShoulderEvalQ11TextBox, "") + _
    Application.WorksheetFunction.CountIf(PennShoulderEvalQ12TextBox, "") + _
    Application.WorksheetFunction.CountIf(PennShoulderEvalQ13TextBox, "") + _
    Application.WorksheetFunction.CountIf(PennShoulderEvalQ14TextBox, "") + _
    Application.WorksheetFunction.CountIf(PennShoulderEvalQ15TextBox, "") + _
    Application.WorksheetFunction.CountIf(PennShoulderEvalQ16TextBox, "") + _
    Application.WorksheetFunction.CountIf(PennShoulderEvalQ17TextBox, "") + _
    Application.WorksheetFunction.CountIf(PennShoulderEvalQ18TextBox, "") + _
    Application.WorksheetFunction.CountIf(PennShoulderEvalQ19TextBox, "") + _
    Application.WorksheetFunction.CountIf(PennShoulderEvalQ20TextBox, ""))

    MsgBox EvalNotScored
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    01-03-2013
    Location
    Aberdeen, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    163

    Re: VBA CountIf logic checking Text Box Input

    I think it doesn't work, because the first parameter in CountIf function must be Range

  5. #5
    Registered User
    Join Date
    02-06-2014
    Location
    San Francisco, CA
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: VBA CountIf logic checking Text Box Input

    Hi Everyone,

    I've gone back and cleaned up my code and tried declaring my "EvalNotScored" variable as a range, but to no avail. If anyone has any further suggestions on how to develop a functioning CountIf statement inside of VBA, I would greatly appreciate it. I've included my updated version of this workbook below. Again, the User form in question is the "Penn Shoulder" and the button that will execute the CountIf statement could the the "PennShoulderCalculateButton" button.

    And of course, thank you for the suggestions thus far.
    Attached Files Attached Files

+ 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. Checking if text box input is of desired type
    By Gzdnkh in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-08-2012, 06:14 AM
  2. COUNTIF and Date Logic
    By Blofeld in forum Excel General
    Replies: 3
    Last Post: 11-20-2011, 10:28 AM
  3. if(countif(if...logic help
    By step_one in forum Excel General
    Replies: 5
    Last Post: 09-23-2011, 11:46 AM
  4. Excel countif and logic
    By Kainmc in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-16-2008, 12:43 PM
  5. COUNTIF with Logic?
    By Leonhardtk in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-10-2006, 07:15 PM

Tags for this Thread

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