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

Vba 在组合宏中执行宏时,宏无法正确运行,vba,excel,formula,Vba,Excel,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 '********

当我单独运行此宏时,它会正确执行:

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

这个代码对我有用:

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 

您的代码使用对单元格的非限定调用,因此它高度依赖于activesheet在运行时的状态。我建议从使用特定工作表完全限定所有单元格引用开始。如何完全限定单元格引用?在您的代码中,单元格14、2表示Sheetssheet_name.Value,但单元格53、11表示活动工作表中的任何内容。如果要从特定工作表复制,请执行类似Dim ws as WorkSheet的操作:在此处设置ws=SheetsSheetName,则可以使用例如ws.Cells53,11,即使Cells53,11和Cells14,2中的数据位于同一工作表中?目标数据和源数据位于同一工作表中。我只需要将数据移动到同一张表上的不同单元格中。