+ Reply to Thread
Results 1 to 24 of 24

Vba code with if statement and index

  1. #1
    Registered User
    Join Date
    06-24-2013
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    35

    Vba code with if statement and index

    Hello

    I am trying to create a code that will either allow the user to input a value, or INDEX a value depending on the input in the column before. I have figured out how to allow the user to input the value, thanks to some one else on this Forum, but I can't seem to get the INDEX formula right.

    As of now, this is my code:


    Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("D4:D40")) Is Nothing Then Exit Sub
    If Target = "INPUT WALL" Then
    Cells(Target.Row, "E") = " "
    Cells(Target.Row, "E").Interior.ColorIndex = 15

    Else
    Range(Target.Row, "E").Formula = "=IF(B4=0,0, INDEX(Piping!$B$2:$AC$27,MATCH(C4,Piping!$B$2:$B$27,),MATCH(D4,Piping!$B$2:$AC$2,)))"
    Cells(Target.Row, "E").Interior.ColorIndex = 2
    End If
    End Sub

    the INDEX however is only looking at C4 and D4 for the whole range from 4 to 40.

    I want it to be like a normal formula you put into a cell and drop the box down. So that when I am in row 5, it is indexing c5 and D5, and when I am in row 6, it is indexing C6 and D6, and so on. because as of now, no matter what row I am in, it still index's C4 and D4

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Vba code with if statement and index

    Maybe:

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    06-24-2013
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Vba code with if statement and index

    nope... well that is assuming you ment to just change the formula line. I don't think sub brickwall015() would do anything.

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Vba code with if statement and index

    Yes, I did mean just to change the formula line within the macro.

    Maybe:

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    06-24-2013
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Vba code with if statement and index

    No, it says "Range of Object worksheet failed"

    what is happening with the formula I have now is that if the cell in column D does not say "INPUT WALL" everything is fine. And if the next row has input wall, I am able to input a value into row E, but then, if the row below is NOT input wall, then it replaces the value I inputted with "#N/A" and if you click on the cell, it shows the index formula.

    It seems like, the way this is set up now, that the whole column is either going to allow a user input, or it will index, but i want each row to allow both of those options. I am sure it has to do with the Range(D4:D40), but I don't know how else to do it so that each row will be independent but the code will work over the whole range.

  6. #6
    Registered User
    Join Date
    06-24-2013
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Vba code with if statement and index

    Okay, so I was able to allow the rows to be independent by changing

    if Target = "INPUT WALL" Then

    to

    Cells(Target.Row, "D") = "INPUT WALL" Then

    however, now it does not accept the INDEX formula, which is

    Range(Target.Row, "E").Formula = "=IF(B4=0,0, INDEX(Piping!$B$2:$AC$27,MATCH(C4,Piping!$B$2:$B$27,),MATCH(D4,Piping!$B$2:$AC$2,)))"

    because it is only looking at row 4, even if I am in a different row. and it says the same thing it did before, about "range of object worksheet failed'

  7. #7
    Registered User
    Join Date
    06-24-2013
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Vba code with if statement and index

    and if I change the range of the formula to say Range("E4:E40").Formula = ... then I get the same problem as before, it fills the whole column with the formula, whether it says "INPUT WALL" or not

  8. #8
    Registered User
    Join Date
    06-24-2013
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Vba code with if statement and index

    if I upload the file could you possibly help me?

  9. #9
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Vba code with if statement and index

    Sure. I'd be glad too take a look at it.

  10. #10
    Registered User
    Join Date
    06-24-2013
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Vba code with if statement and index

    how do i upload a file?

  11. #11
    Registered User
    Join Date
    06-24-2013
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Vba code with if statement and index

    here it is
    Attached Files Attached Files

  12. #12
    Registered User
    Join Date
    06-24-2013
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Vba code with if statement and index

    so the grey boxes are all lists that the user selects, and if they select "INPUT WAll" I want them to input a wall thickness. Otherwise I want it to execute the index formula. But I can't seem to get it to work and index with the individual row.

  13. #13
    Registered User
    Join Date
    06-24-2013
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Vba code with if statement and index

    any luck? I would really like to figure this out soon!! Thanks in advance

  14. #14
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Vba code with if statement and index

    I am not sure how this is supposed to work, but this change will input the formula with the correct row.

    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    06-24-2013
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Vba code with if statement and index

    it works!!

    THANK YOU SO MUCH, YOUR A GENIUS!!

  16. #16
    Registered User
    Join Date
    06-24-2013
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Vba code with if statement and index

    the only thing is that if I clear all of the cells, to input things into the different drop down list, I get a runtime error 14 Type mismatch

    And when I click debug, it hi lights the "If Target.Value = "INPUT WALL" Then" line... any way to have it so that if D4 is nothing then it ends the sub?

  17. #17
    Registered User
    Join Date
    06-24-2013
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Vba code with if statement and index

    Runtime error 13

  18. #18
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Vba code with if statement and index

    Try:

    Please Login or Register  to view this content.

  19. #19
    Registered User
    Join Date
    06-24-2013
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Vba code with if statement and index

    IT is doing something crazy!!

    HAHA once i select from the list in B4, all of the cells in row 4 have #N/A, like from colmn B until the end of the sheet.

    And when i clear it I still get the same error :/

  20. #20
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Vba code with if statement and index

    I don't know what it is designed to do. I only was concerned with adding the row number to the formula as a cell was selected in Range("D4:D40"). On testing that it worked on the example sheet you provided. However, you have other formulas and links in the sample and I can't tell how they interact with each other.

  21. #21
    Registered User
    Join Date
    06-24-2013
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Vba code with if statement and index

    I figured it out, Target.Value = "Input Wall" needed to be target.text

    THANKS AGAIN!

  22. #22
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Vba code with if statement and index

    You're welcome. Glad to hear that, and thanks for the feedback. Please comply with Forum Rule No. 9.


    9. Acknowledge the responses you receive, good or bad. If your problem is solved, please say so clearly, and mark your thread as Solved: Click Thread Tools above your first post, select "Mark your thread as Solved". Or click the Edit button on your first post in the thread, Click Go Advanced, select [SOLVED] from the Prefix dropdown, then click Save Changes. If more than two days have elapsed, the Dropdown option or Edit button will not appear -- ask a moderator to mark it.

  23. #23
    Registered User
    Join Date
    06-24-2013
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Vba code with if statement and index

    well it isn't totally working actually.
    It works once, then doesn't work... it is really weird

  24. #24
    Registered User
    Join Date
    06-24-2013
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Vba code with if statement and index

    well with a little bit of messing around, here is the final code, and it works like i wanted it too!

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("D4:D40")) Is Nothing Then
    If Target.Text = " " Then Exit Sub

    If Target.Text = "INPUT WALL" Then
    Target.Offset(, 1).Value = " "
    Target.Offset(, 1).Interior.ColorIndex = 15

    Else
    Target.Offset(, 1).Formula = "=IF(D" & Target.Row & "=0,0, INDEX(Piping!$B$2:$AC$27,MATCH(C" & Target.Row & ",Piping!$B$2:$B$27,),MATCH(D" & Target.Row & ",Piping!$B$2:$AC$2,)))"
    Target.Offset(, 1).Interior.ColorIndex = 2
    End If
    End If

    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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