【问题标题】:Run on matrix and find if cell is number or string (letters)在矩阵上运行并查找单元格是数字还是字符串(字母)
【发布时间】:2020-11-09 08:24:21
【问题描述】:

我在 excel 中构建了一个 vba 程序,该程序将在 10X10 矩阵上运行,其中包含数字和字母(每个单元格只有一个 - 或字母或数字) 当你找到一个带有数字的单元格时,将其变为蓝色并写下数字(而不是数字) 当您找到带有字母的单元格时,将其变为黄色并写入字符串(而不是数字) 最后得到所有带有数字的单元格并计算平均值(如果只是得到一个总和变量和数字单元格数量的计数器) 答案应该写入另一个工作表 - 带有颜色和单词“数字”和“字符串”的新矩阵,低于平均值

这是我的新程序

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

结束子

由于某种原因,我无法确定该程序不会进入下一个循环 t 将在第一行运行然后停止。 现在它甚至因为这个错误而无法工作:“范围类的选择方法失败”

请帮帮我。

【问题讨论】:

  • 您可能会受益于阅读Why is “Can someone help me?” not an actual question? • 和How to avoid using Select in Excel VBA。 • 请注意Range(i & j) 解析为Range(11) 之类的东西,它不是一个有效的范围地址,两次相同的循环Do Until Sheets("Matrix").Range(i & j).Value = "" 没有任何意义
  • 你可以在没有循环的情况下更快地做到这一点。搜索SpecialCells
  • 如果你写Range(i & j),VBA 将连接i 和j。这将导致11 不是有效范围。你的意思可能是Sheets("Matrix").cells(i, j).Value
  • @FunThomas 感谢你的“细胞”方法,我把它写到了另一张纸上。我现在唯一的问题是为什么它在完成所有列后不会下降 1 行
  • 我做了更改.. 但它仍然不起作用。现在它向我显示了这个错误:“范围类的选择方法通过 VBA 失败,错误 1004”,我不知道为什么(它一直工作到现在)这是我在更改后的程序 Do until Sheets("Matrix").Cells (i, j).Value = "" j = 1 直到 Sheets("Matrix").Cells(i, j).Value = "" x = Sheets("Matrix").Cells(i, j).Value If IsNumeric(x) Then Sheets("Matrix2").Cells(i, j).Select

标签: excel vba


【解决方案1】:

一个棘手的双循环

快速修复

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

【讨论】:

  • 非常感谢您的回答(我会使用它),但我仍然想知道为什么我的不起作用,而且更改需要在另一张表中进行,所以我猜是:With Cells。 .. 它将是 With Sheets("Sheet2").Cells... 对吗?
  • 为什么这段代码不起作用??? Sheets("Matrix2").Cells(i, j).Select
  • 我做了你所说的关于 ws1= thisworkbook.worksheets("Matrix") (我添加了 Dim ws1 作为工作表) - 但它没有工作(对象变量或块变量未设置,对于行ws1 = ...) 我将 As int 添加到 i
  • 我没有在代码中的任何地方使用 Select,因为它是要避免的(请参阅您在帖子中得到的第一条评论)。但是如果你需要在最后添加到代码中,你必须先激活才能确定:ThisWorkbook Worksheets("Matrix2").Activate。现在您可以放心地选择了。
  • 而且我还重置了 j 我在第一个循环结束后添加了它
猜你喜欢
  • 2018-10-14
  • 2013-10-09
  • 2011-09-01
  • 2012-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-28
  • 1970-01-01
相关资源
最近更新 更多