我找不到我做错了什么我的代码不工作所以.我是一个有点新手在这方面,我不太明白问题是什么
它在这一行矩阵=范围(“B5”).调整大小(行,列)上给我警告
Sub TamsayiliRasgeleMatris()
'Deklarasyonlar
Dim rows As Integer, cols As Integer
Dim lowerBound As Integer, upperBound As Integer
Dim sum As Double, average As Double
'Kullanıcıdan girdiler alma
rows = Range("A2").Value
cols = Range("B2").Value
lowerBound = Range("C2").Value
upperBound = Range("D2").Value
'Boş bir matris oluşturma
Dim matrix As Variant
matrix = Range("B5").Resize(rows, cols)
'Matrisi rastgele sayılarla doldurma
For i = 1 To rows
For j = 1 To cols
matrix(i, j) = Int((upperBound - lowerBound + 1) * Rnd + lowerBound)
sum = sum + matrix(i, j)
Next j
Next i
'Matrisi çalışma sayfasına yazma
matrix.Copy Destination:=Range("B5")
'Ortalama değerini hesaplayın ve E2 hücresine yazma
average = sum / (rows * cols)
Range("E2").Value = average
'Matris transpozunu oluşturun ve altına yapıştırın
Dim transposed As Variant
transposed = Application.Transpose(matrix)
transposed.Copy Destination:=Range("B5").Offset(rows + 1, 0)
'Değerleri ortalama değerine göre renklendirin
For i = 1 To rows
For j = 1 To cols
If matrix(i, j) < average Then
matrix(i, j).Interior.Color = vbCyan
ElseIf matrix(i, j) > average Then
matrix(i, j).Interior.Color = vbMagenta
End If
Next j
Next i
End Sub
2条答案
按热度按时间mrzz3bfm1#
了解范围和数组
第一节第一节第一节第一节第一次
liwlm1x92#
以下是一些建议,可能会使您的代码运行
考虑以下代码片段:
因为:
matrix
被声明为Variant
类型Value
是任何Range
对象的默认属性那么
matrix
最终会得到一个Variant
数组,就像你编写了:接下来您将编写:
这将导致错误,因为数组没有任何
Copy
方法,而后者可用于许多对象,其中包括Range
对象因此,您应该"反转"基准表分配代码行,如下所示:
稍微复杂一点的是修复另一个错误的
Copy
语句其沿着先前定位的路线将被编码如下:
您会注意到,我在
Resize()
属性中交换了cols
和rows
,以考虑换位最后是以下片段:
将按如下方式勾选: