Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Spreadsheets (https://www.askmehelpdesk.com/forumdisplay.php?f=395)
-   -   Macro to select a value in the range of cells? (https://www.askmehelpdesk.com/showthread.php?t=621622)

  • Dec 22, 2011, 11:03 AM
    Dilawer
    Macro to select a value in the range of cells?
    I need a macro that will select every cell in a range based on a number in a cell. If the number in the cell (in this case the cell is b2,b5,b10 and b15) is 0 then I want to select cell b2,b5,b10,b15 in the range b1:b50

    Thanks for the help!
  • Dec 22, 2011, 03:40 PM
    JBeaucaire
    Select them, then do what? VBA is pretty powerful, you don't actually have toe "select" anything to process the found cells. I can show you a quick macro for selecting, but if you tell me the actual end-goal, I can just have VBA do that with the found cells.

    Also, you are wanting cells that have the actual number 0, not just cells that are zero value, including blanks?
  • Dec 22, 2011, 08:31 PM
    Dilawer
    Yes, I need to select only cells with zero value in the range b1:b50 then need to hide the Entire Row of selected cells.

    thanks a ton.
  • Dec 23, 2011, 06:42 AM
    JBeaucaire
    So, a blank cell has a zero value, hide those?
    A cell with the NUMBER zero, hide those?

    (that's two separate questions)
  • Dec 23, 2011, 06:50 AM
    JBeaucaire
    Actually, here, you can adapt this. There are two "tests" in this macro, remove one if you only want the other, this will hide rows that are empty in B, or if they have a numeric value of zero in B.

    Code:

    Sub HideEm()
    Dim Rw As Long
    Application.ScreenUpdating = False

    For Rw = 1 To 50
        Rows(Rw).EntireRow.Hidden = Cells(Rw, "B") = "" Or Cells(Rw, "B") = 0
    Next Rw

    Application.ScreenUpdating = True
    End Sub

  • Dec 26, 2011, 10:24 PM
    Dilawer
    thanks a ton J Beaucaire :)

    your code really halpfull to making work the following code :)

    I took "Application.ScreenUpdating = False" from your code and my code is now working perfectly

    thanks again :)

    Sub HideRows()
    Application.ScreenUpdating = False
    Range("b1:b50").Select
    Selection.EntireRow.Hidden = False
    For Each cell In ActiveSheet.Range("b1:b50")
    If cell.Value = 0 Then cell.EntireRow.Hidden = True
    Next
    End Sub
  • Dec 26, 2011, 11:32 PM
    JBeaucaire
    This is what your macro should read as to work at top efficiency:
    Code:

    Sub HideRows()
    Application.ScreenUpdating = False

      For Each cell In ActiveSheet.Range("B1:B50")
          cell.EntireRow.Hidden = cell.Value = 0
      Next cell

    Application.ScreenUpdating = True
    End Sub

  • Dec 28, 2011, 03:56 AM
    Dilawer
    Thanks again J Beaucaire :)

  • All times are GMT -7. The time now is 08:54 AM.