在组合宏中执行时,宏无法正确运行

时间:2014-02-10 19:16:21

标签: excel vba excel-vba formula

当我自己运行这个宏时,它会正确执行:

Sub StateBIPDData()
Dim sheet_name As Range
For Each sheet_name In Sheets("WS").Range("B:B")
If sheet_name.Value = "" Then
    Exit For
Else
With Sheets(sheet_name.Value)
'***********************
'Format BI data for states
'***********************
'Countrywide Earned Car Years
.Cells(14, 2) = (Cells(53, 11))
.Cells(15, 2) = (Cells(49, 11))
.Cells(16, 2) = (Cells(45, 11))
.Cells(17, 2) = (Cells(41, 11))
.Cells(18, 2) = (Cells(37, 11))
'Paid Claim Frequency (Claim Frequency) = (Number of Paid Claims ÷ Earned House Years) ×     100
.Cells(14, 3) = (Cells(53, 17))
.Cells(15, 3) = (Cells(49, 17))
.Cells(16, 3) = (Cells(45, 17))
.Cells(17, 3) = (Cells(40, 17))
.Cells(18, 3) = (Cells(37, 17))
'.Range("C14:C18").Copy
'.Range("C14:C18").PasteSpecial xlPasteValues
'Average Loss (Claim Severity)  = Paid Losses ÷ Number of Paid Claims
.Cells(14, 4) = (Cells(53, 15))
.Cells(15, 4) = (Cells(49, 15))
.Cells(16, 4) = (Cells(45, 15))
.Cells(17, 4) = (Cells(41, 15))
.Cells(18, 4) = (Cells(37, 15))
'.Range("D14:D18").Copy
'.Range("D14:D18").PasteSpecial xlPasteValues
'Pure Premium (Loss Cost) = Paid Losses ÷ Earned House Years
.Cells(14, 5) = (Cells(53, 19))
.Cells(15, 5) = (Cells(49, 19))
.Cells(16, 5) = (Cells(45, 19))
.Cells(17, 5) = (Cells(41, 19))
.Cells(18, 5) = (Cells(37, 19))
'.Range("E14:E18").Copy
'.Range("E14:E18").PasteSpecial xlPasteValues
'.Range("K37:S56").Clear
'***********************
'Format PD data for states
'***********************
'Paid Claim Frequency (Claim Frequency) = (Number of Paid Claims ÷ Earned House Years) × 100
.Cells(14, 6) = (Cells(30, 13))
.Cells(15, 6) = (Cells(26, 13))
.Cells(16, 6) = (Cells(22, 13))
.Cells(17, 6) = (Cells(18, 13))
.Cells(18, 6) = (Cells(14, 13))
'.Range("F14:F18").Copy
'.Range("F14:F18").PasteSpecial xlPasteValues
'Average Loss (Claim Severity)  = Paid Losses ÷ Number of Paid Claims
.Cells(14, 7) = (Cells(30, 11))
.Cells(15, 7) = (Cells(26, 11))
.Cells(16, 7) = (Cells(22, 11))
.Cells(17, 7) = (Cells(18, 11))
.Cells(18, 7) = (Cells(14, 11))
'.Range("G14:G18").Copy
'.Range("G14:G18").PasteSpecial xlPasteValues
'Pure Premium (Loss Cost) = Paid Losses ÷ Earned House Years
.Cells(14, 8) = (Cells(30, 15))
.Cells(15, 8) = (Cells(26, 15))
.Cells(16, 8) = (Cells(22, 15))
.Cells(17, 8) = (Cells(18, 15))
.Cells(18, 8) = (Cells(14, 15))
'.Range("H14:H18").Copy
'.Range("H14:H18").PasteSpecial xlPasteValues
'.Range("K14:O33").Clear
'***********************
'Calculate 'Number of BI Claims Per 100 PD Claims"
'***********************
.Cells(14, 9) = (Cells(14, 3) / Cells(14, 6)) * 100
.Cells(15, 9) = (Cells(15, 3) / Cells(15, 6)) * 100
.Cells(16, 9) = (Cells(16, 3) / Cells(16, 6)) * 100
.Cells(17, 9) = (Cells(17, 3) / Cells(17, 6)) * 100
.Cells(18, 9) = (Cells(18, 3) / Cells(18, 6)) * 100
'.Range("I14:I18").Copy
'.Range("I14:I18").PasteSpecial xlPasteValues
End With
    End If
Next sheet_name
End Sub

虽然,当我在组合宏中运行代码时,代码无法正常执行。

事实上,根本没有任何事情发生,目标细胞留空。存在源单元的数据。

关于为什么值没有出现在目标细胞中的任何想法?

以下是组合宏的代码:

Sub DataAutomation_AITrends()
GetSourceSheets
CreateCwBIDataCalculate
InsertRowAllStates
CopyPasteBIPDPIPData
StateBIPDData
StatePIPData
DeleteWorkSheets
Percent_change_CalculationAllSheets
On Error Resume Next
End Sub

1 个答案:

答案 0 :(得分:0)

此代码对我有用:

Sub PercentChangeCalBIPD()
Dim iRow As Long
Dim sheet_name As Range
For Each sheet_name In Sheets("WS").Range("H:H")
    If sheet_name.Value = "" Then
        Exit For
    Else
    With Sheets(sheet_name.Value)
'90-20## Percent Change
iRow = 14
Cell_val = .Cells(iRow, 1)
While Cell_val <> ""
iRow = iRow + 3
Cell_val = .Cells(iRow, 1)
Wend
.Cells(iRow + 3, 1) = "1990-2012"
'Column B,2

Cell_val = .Cells(iRow, 2)
While Cell_val <> ""
iRow = iRow + 3
Cell_val = .Cells(iRow, 2)
Wend
.Cells(iRow + 3, 2) = "=((B14/VLOOKUP(1990, A14:B46, 2,FALSE))-1)"
End Sub