2016-03-24 2 views
0

したがって、相対値を計算するマクロを作成しようとしています。私は基本的に列全体に行き、いくつかの値をとり、別の値で割り、別の列に書きます。その手順は関係ありません。私は、hold3 = hold/hold2でタイプの不一致が発生することを嫌にしています。私はそのように故障しました(hold、hold2、hold3を使用して)問題がどこにあるかを確認します。それは部門にある。私がhold3 = holdを離れると、それは私にエラーを与えません。私が分割したり合計したり、何らかの操作をしようとすると、それは私に不一致エラーを与えます。私はすべての変数を何も変数を使用しないでdoubleと宣言しようとしました(Cells(i、8).Value = Cells(i、7).Value/Cells(cuenta、7))と何も動作しません。VBAでの型の不一致、セル(x、x)での演算の実行ができません。

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 
+3

あなたは分裂を行うために使用している値だかどうか確認するために 'のDebug.Print hold'と'のDebug.Print hold2'を使用してください。同様に 'msgbox hold'と' msgbox hold2'を使うこともできます。 – newguy

+1

空白のセルがある場合は、バリアントを使用するときにこのエラーが発生します。 –

答えて

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 
関連する問題