Daniel Lichtenstadt
Daniel Lichtenstadt

Reputation: 3

Run on matrix and find if cell is number or string (letters)

I build a vba program in excel that will run on a 10X10 matrix with numbers and letters (each cell have only one of them - or letter or number) and when u find a cell with numbers turn it to blue and write number (instead of the number) when u find a cell with letters turn it to yellow and write string (instead of the number) and in the end get all the cells with numbers and calculate the average (in the if just get a sum variable and counter of the amount of number cells) the answer should be written to another worksheet - the new matrix with the colors and the words "number" and "string", and below that the average

this is my new program

Sub Num_Str_Matrix()
Sheets("Matrix2").Cells.Clear
Dim i, j As Integer
Dim Sum As Integer
Dim Counter As Integer
Dim Average As Double
Dim x As String
Sum = 0
Counter = 0
i = 1
j = 1
Do Until Sheets("Matrix").Cells(i, j).Value = ""
    j = 1
    Do Until Sheets("Matrix").Cells(i, j).Value = ""
        x = Sheets("Matrix").Cells(i, j).Value
        If IsNumeric(x) Then
            Sum = Sum + Sheets("Matrix").Cells(i, j).Value
            Counter = Counter + 1
            Sheets("Matrix2").Cells(i, j).Select
            With Selection.Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .ThemeColor = xlThemeColorAccent5
                .TintAndShade = 0.599993896298105
                .PatternTintAndShade = 0
                ActiveCell.FormulaR1C1 = "Number"
            End With
        Else
            Sheets("Matrix2").Cells(i, j).Select
            With Selection.Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .ThemeColor = xlThemeColorAccent4
                .TintAndShade = 0.599993896298105
                .PatternTintAndShade = 0
                ActiveCell.FormulaR1C1 = "String"
            End With
        End If
    j = j + 1
    Loop
i = i + 1
Loop
Average = Sum / Counter

End Sub

the program for some reason that I can't figure wont go to the next loop t will work on the first row and then stop. and now it wont even work because of this Error: "Select method of Range class failed"

Please help me.

Upvotes: 0

Views: 57

Answers (1)

VBasic2008
VBasic2008

Reputation: 54807

A Tricky Double Do Loop

A Quick Fix

Option Explicit

Sub Num_Str_Matrix()
    Dim i As Long, j As Long
    Dim x As Variant
    i = 1
    j = 1
    x = Sheets("Matrix").Cells(i, j).Value
    Do
        Do
            If IsNumeric(x) Then
                With Cells(i, j)
                    .Value = "Number"
                    With .Interior
                        .Pattern = xlSolid
                        .PatternColorIndex = xlAutomatic
                        .ThemeColor = xlThemeColorAccent5
                        .TintAndShade = 0.599993896298105
                        .PatternTintAndShade = 0
                    End With
                End With
            Else
                With Cells(i, j)
                    .Value = "String"
                    With .Interior
                        .Pattern = xlSolid
                        .PatternColorIndex = xlAutomatic
                        .ThemeColor = xlThemeColorAccent4
                        .TintAndShade = 0.599993896298105
                        .PatternTintAndShade = 0
                    End With
                End With
            End If
            ' Debug.Print Cells(i, j).Address, Cells(i, j).Value
            j = j + 1
            x = Sheets("Matrix").Cells(i, j).Value
        Loop Until x = ""
        j = 1
        i = i + 1
        x = Sheets("Matrix").Cells(i, j).Value
    Loop Until x = ""

End Sub

Upvotes: 0

Related Questions