Results 1 to 4 of 4

Thread: picking number from a set of cells

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    2

    picking number from a set of cells

    Hello, I'm new to this site and new to VBA.

    I need to write code in VBA that will do this:

    If the number 1 is generated in cells B23 to B28, I need a 1 placed in B17. If 1 isn't generated in cells B23 to B28, I need a zero placed in B17.

    If 2 is generated in cells B23 to B28, I need a 1 placed in B18. If 2 isn't generated in cells B23 to B28, I need a zero placed in B18.

    If 3 is generated in cells B23 to B28, I need a 1 placed in B19. If 3 isn't generated in cells B23 to B28, I need a zero placed in B19.

    If 4 is generated in cells B23 to B28, I need a 1 placed in B20. If 4 isn't generated in cells B23 to B28, I need a zero placed in B20.

    If 5 is generated in cells B23 to B28, I need a 1 placed in B21. If 5 isn't generated in cells B23 to B28, I need a zero placed in B21.

    If 6 is generated in cells B23 to B28, I need a 1 placed in B22. If 6 isn't generated in cells B23 to B28, I need a zero placed in B22.

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: picking number from a set of cells

    Welcome to the forums!
    I am assuming you want this code to run whenever a change is made to the worksheet?
    If so, copy the attached code into the Code page for the sheet in question.

    VB Code:
    1. Private Sub Worksheet_Change(ByVal Target As Range)
    2. Dim MyRange As Range
    3. Dim x As Integer
    4. Dim Chk As Integer
    5. Dim MyCell As Range
    6.    
    7.     Set MyRange = Range("B23:B28") 'The range you need to check for your numbers
    8.     For x = 1 To 6 'Loop throught he numbers you want to check
    9.         Chk = 0 'assume the value is not found
    10.         For Each MyCell In MyRange 'Loop throught the cells in your range
    11.             If MyCell.Value = x Then
    12.                 Chk = 1 'Find a match
    13.                 Exit For 'don't need to look anymore
    14.             End If
    15.         Next MyCell
    16.         Application.EnableEvents = False 'Stops the Worksheet Change event from firing - which would cause an infinite loop
    17.         Range("B16").Offset(x, 0) = Chk 'record if the number is found
    18.         Application.EnableEvents = True 'Turn events back on
    19.     Next x
    20.    
    21.     Set MyRange = Nothing
    22. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: picking number from a set of cells

    As a side note, you don't need to use VBA to achieve the results you want. You could just use the following formula in cell B17 and copy it down to fill the range B17:B22.

    =IF(ISERROR(MATCH(A17,$B$23:$B$28,0)),0,1)

    Note: I've assumed that you have labels of 1 to 6 in cells A17:A22. If this is not the case then change the reference A17 in the formula to the value that you want to check for in that cell.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    2

    Re: picking number from a set of cells

    Thank you so much.

    I really need to learn Excel better and I want to learn VB (and VBA). How do you suggest I start?

    Again many thanks....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width