Consulting

Results 1 to 5 of 5

Thread: Show avarage of cells with zero value in selected range

  1. #1
    VBAX Regular
    Joined
    Jul 2012
    Posts
    7
    Location

    Show avarage of cells with zero value in selected range

    I have trouble explaining my macro to take all zero cells count how many i have and show me avarage in message box.

    Whats wrong ?

    [VBA]Sub srsadas()

    Dim i, count As Integer
    Dim area, cell As Range
    Dim sum, avg As Double
    Dim color1, color2 As Integer
    Dim example As Range

    i = InputBox("Give the value of i")
    Set area = Application.InputBox("Select range", "Range", , , , , , 8)

    For Each cell In area
    cell(a + 1, b + 1).Value = 2 * i
    i = i + 1

    Next cell
    For Each cell In area
    If cell Mod 7 = 0 Or cell Mod 12 = 0 Then cell = 0
    Next
    color1 = InputBox("colour a")
    color2 = InputBox("colour b")
    For Each cell In area
    If cell Mod i = 0 Then cell.Interior.ColorIndex = color2
    If cell Mod i <> 0 Then cell.Interior.ColorIndex = color1
    Next

    Set example = area.Value = 0
    sum = example.Cells.count
    avg = sum / area.Cells.count
    MsgBox avg
    End Sub[/VBA]

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Avoid using reserved VBA/Excel names to desinate variable.
    In your case: do not use area, nor cell

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by snb
    Avoid using reserved VBA/Excel names to desinate variable.
    In your case: do not use area, nor cell
    Nothing wrong with cell, I use it all the time, the reserved word is Cells.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [VBA]Sub srsadas()

    Dim i, count As Integer
    Dim area As Range, cell As Range
    Dim color1 As Long, color2 As Long

    i = InputBox("Give the value of i")
    Set area = Application.InputBox("Select range", "Range", , , , , , 8)

    color1 = InputBox("colour a")
    color2 = InputBox("colour b")
    For Each cell In area
    cell(a + 1, b + 1).Value = 2 * i
    Select Case True
    Case cell.Value Mod 7 = 0, cell.Value Mod 12 = 0: cell.Value = 0
    Case cell Mod i = 0: cell.Interior.ColorIndex = color2
    Case cell Mod i <> 0: cell.Interior.ColorIndex = color1
    End Select
    i = i + 1
    Next cell

    MsgBox Application.Evaluate("AVERAGE(IF(" & area.Address & "<>)," & area.Address & "))")
    End Sub
    [/VBA]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Regular
    Joined
    Jul 2012
    Posts
    7
    Location
    is is even worse now
    IT doesn't change color of 0 value cells

    And still says the same "type mismatch"

    I have changed all cell name with c but there is still some error

Posting Permissions

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