我希望有人可以帮助我。我是VBA的新手,完全是自学成才,使用这样的论坛。我有一张4张工作簿,前3个是可见的,第4张是隐藏的:
我创建了以下代码,意图取消隐藏Sheet1,将公式添加到单元格A2,B2,C2和D2(第1行包含标题)并将公式填充到最后一行,该行等于行数数据表中的数据基于仪表板上单元格F6的值。当我到达“.Range(”A2:D2“)。公式= strformulas”,我的代码停止,我得到运行时错误'1004'应用程序定义或对象定义错误。
Sub addformulasSheet1()
Dim sh1 As Worksheet, sh2 As Worksheet
Set sh1 = Sheets("Dashboard")
Set sh2 = Sheets("Sheet1")
Set Data = Sheets("Data")
Set lr = sh1.Range("F6")
sh2.Visible = xlSheetVisible
sh2.Activate
With sh2
Dim strformulas(1 To 4) As Variant
strformulas(1) = "=IF(OR(Data!$AW2=""Yes"",Data!$AX2=""Yes"",Data!$AY2=""Yes"",Data!$BB2=""Yes"",Data!$BM2=1,Data!$BN2=1,Data!$BO2=1,Data!$BP2=1,Data!$BQ2=1,Data!$BR2=1),Data!$F2,"""")"
strformulas(2) = "=IF(OR(AND(ICPAS!$Q2=""No"",ICPAS!$R2=""Yes""),AND(ICPAS!$Q2=""Yes"",ICPAS!$V2=""Yes""),ICPAS!$S2=""Yes"",ICPAS!$T2=""Yes"",ICPAS!$AF2<>"""",ICPAS!$AG2<>"""",ICPAS!$AH2=1),ICPAS!$C2,"""")"
strformulas(3) = "=IF(Data!$BH2=1,Data!$F2,"""")"
strformulas(4) = "=IF(ICPAS!$AJ2=1,ICPAS!$C2,"")"
.Range("A2:D2").Formula = strformulas
.Range("P2").resize(, UBound(strformulas)) = strformulas
.Range("A2:D" & lr).FillDown
End With
sh2.Visible = xlSheetHidden
End Sub