所以我正在尝试编写一个宏来计算一些相对值。我基本上会查看整个列,取一些值并将其除以另一列并写入另一列。该程序无关紧要。困扰我的是我在hold3 = hold / hold2中遇到类型不匹配。我这样崩溃了(使用hold,hold2,hold3)来查看问题所在。它属于分部。如果我只是离开hold3 = hold它不会给我一个错误。当我尝试划分或求和或执行任何操作时,它会给出不匹配错误。我尝试将所有变量声明为double,而不是使用任何变量(对于Cells(i,8).Value = Cells(i,7).Value / Cells(cuenta,7))并且没有任何作用。
Sub DivideByTotal()
Dim innerTop As Long
Dim counter As Long
Dim hold As Variant
Dim hold2 As Variant
Dim hold3 As Variant
For counter = 9 To 559
innerTop = counter + 4
Dim i As Long
For i = counter To innerTop
Dim cuenta As Long
cuenta = counter
hold = Cells(i, 7).Value
hold2 = Cells(cuenta, 7).Value
hold3 = hold / hold2
Cells(i, 8).Value = hold3
Next i
counter = counter + 4
Next counter
End Sub
答案 0 :(得分:0)
试试这个:
Sub DivideByTotal()
Dim innerTop As Long
Dim counter As Long
Dim i As Long
Dim hold As Double
Dim hold2 As Double
Dim hold3 As Double
Dim wk As Worksheet
Dim cuenta As Long
Set wk=Sheet1 'Replace it with the sheet number
With wk
For counter = 9 To 559
innerTop = counter + 4
For i = counter To innerTop
cuenta = counter
hold = .Cells(i, 7).Value
hold2 = .Cells(cuenta, 7).Value
if Isnumeric(hold)=0 or isnumeric(hold2)=0 then 'Check if hold and hold2 are numbers or not.
Debug.Print hold
Debug.Print hold2
ELse
hold3 = hold / hold2
.Cells(i, 8).Value = hold3
End if
Next i
counter = counter + 4
Next counter
End With
End Sub