嵌套循环中复制粘贴列的溢出错误

时间:2018-12-31 04:30:53

标签: excel vba

我需要从名为“ Matrix”的工作表中复制y值列并将其粘贴到名为“ All Normalized”的工作表中,格式不是问题,但列数不仅是10,而且是无限的。

我需要将Matrix中的值复制为一列,并将每个单元格除以列中的第一个数字以对其进行归一化(第一个值从第3行开始)。而且我一直收到此错误:“运行时错误'6'-溢出”。

如何解决此错误并正确规范化数据?

Sub NewNorm()
Set WB = ThisWorkbook
Application.ScreenUpdating = False

'X-Values
With WB.Sheets("All Normalized")
[A3].Value = 0
[A4].Value = 1E-18
[A5].Value = 0.0001
[A6].Value = 0.001
[A7].Value = 0.01
[A8].Value = 0.5
[A9].Value = 1
[A10].Value = 2
[A11].Value = 3
[A12].Value = 4
[A13].Value = 5
[A14].Value = 6
[A15].Value = 7
[A16].Value = 8
[A17].Value = 9
[A18].Value = 10
[A19].Value = 20
[A20].Value = 30
[A21].Value = 40
[A22].Value = 50
[A23].Value = 60
[A24].Value = 70
[A25].Value = 80
[A26].Value = 90
[A27].Value = 100
[A28].Value = 150
[A29].Value = 175
[A30].Value = 180
[A31].Value = 185
[A32].Value = 190
[A33].Value = 200
[A34].Value = 300
[A35].Value = 400
[A36].Value = 500
[A37].Value = 1000
End With

Dim ColumnCount As Integer
ColumnCount = 10
Dim Colum As Long

For Columz = 2 To columnz  'Loop through each cell, normalizing
For rowz = 3 To 10
    Sheets("All Normalized").Cells(rowz, Columz).Value = Sheets("Matrix").Cells(rowz, Columz).Value / Sheets("Matrix").Cells(3, Columz).Value 'ERROR HERE
Next rowz
Next Columz

Application.ScreenUpdating = True

End Sub

1 个答案:

答案 0 :(得分:0)

你的叙事,

  

...将每个单元格除以列中的第一个数字...

您的数据,

[A3].Value = 0
[A4].Value = 1E-18
[A5].Value = 0.0001
[A6].Value = 0.001
...

您的代码,

Sheets("All Normalized").Cells(rowz, Columz).Value = _
  Sheets("Matrix").Cells(rowz, Columz).Value / Sheets("Matrix").Cells(3, Columz).Value

您不能将数字除以零。会导致溢出错误。