+ Reply to Thread
Results 1 to 6 of 6

build If statement condition dynamically

Hybrid View

  1. #1
    Registered User
    Join Date
    10-13-2008
    Location
    USA
    Posts
    3

    build If statement condition dynamically

    Hello,
    This is my first post and hope this is not a silly question.

    Is it possible to create a criteria for an if or case statement dynamically?

    For example, if the string variable 'criteria' is literally "Sheet1.Cells.Range(A1).value = "A" AND Sheet1.Cells.Range(B1) <> "B", I would like to put:


    Option #1:
    If criteria then
    Sheet1.Rows.EntireRow.Delete 
    End if
    actual result: Type mismatch error
    desired result: delete row based on criteria passed

    Option #2:
    use a case statement?
    Select Case criteria
    Case criteria
    MsgBox ("deleting...")
    Sheet1.Rows.EntireRow.Delete
    Case Else
    MsgBox ("Row not deleted.")
    End Select
    actual result: deletes row regardless of criteria being passed
    desired result: delete row based on criteria passed

    Thanks in advance.
    Last edited by Leith Ross; 10-13-2008 at 12:06 PM.

  2. #2
    Forum Expert Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,161
    Do you mean like this?:
    Dim Criteria
    Criteria = Sheets("Sheet1").Range("A1").Value = "A" And Sheets("Sheet1").Range("B1") <> "B"
    If Criteria = True Then
    MsgBox "True"
    Else
    MsgBox "False"
    End If
    Not all forums are the same - seek and you shall find

  3. #3
    Registered User
    Join Date
    10-13-2008
    Location
    USA
    Posts
    3
    Hi Simon,
    Correct, that is what I want to do. However, I do not want to re-set the actual criteria within the variable "Criteria" as it is already passed to the subroutine.

    This way, I would like the subroutine to be more flexible so that if the criteria changes, you don't have to recode.

  4. #4
    Forum Expert Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,161
    All hard coded ranges need to be recoded, what is it you are trying to achieve? can you explain a little more?

  5. #5
    Registered User
    Join Date
    10-13-2008
    Location
    USA
    Posts
    3
    Hi Simon,
    I would like to go through all rows in a file, applying conditions to be evaluated. If those conditions are true, delete that row.

    How can I give the user the ability to specify the condition (via input box, or form) so that it is dynamic?

    For example, they may want to specify that if row A = "x" delete the row in one instance. In another instance, they may select row A = "y" and row B = "z"

    Sorry for the confusion.

  6. #6
    Forum Expert Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,161
    f course that kind of manipulation has to be done as a batch procees i.e do A=x for all of A first then A=y B=z next for all of A & B then...etci suggest a userform with a few pairs of textboxes for the criteria and perhaps checkboxes to signify which pairs will be processed, the user would enter all their pairings or criteria they want processed then check the checkboxes....etc

+ 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. exclude several values with one condition
    By Brigitte_P in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-31-2007, 12:58 PM
  2. With Statement Questions
    By OzTrekker in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-02-2007, 02:44 AM
  3. IF statement more than one condition
    By Gambinus in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-19-2007, 01:43 PM
  4. Linking a form button to a worksheet with a condition statement
    By sammyserwar in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-13-2007, 06:02 PM
  5. advanced (i think) if statement help
    By ukrockhit in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 11-06-2006, 11:41 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