运行时错误“1004”,用于在工作簿中的每个工作表中输入公式

时间:2016-04-11 14:25:52

标签: excel vba excel-vba

所以我有一个正常运行的代码。基本上将每个工作表的单元格A2和A1中的不同部分拼凑在一起,以创建唯一的选项卡名称。但我遇到了重复标签名称的情况。所以我调整了公式,使它更长一点,以避免这些错误,但现在当我运行宏时,我得到运行时错误'1004'。不知道为什么因为没有改变而是公式。

以下是代码:

Sub Tab_Name_Creation()
Dim s As Worksheet
Dim formula As String
For Each s In ActiveWorkbook.Worksheets

s.Activate
With ActiveWindow
    .View = xlNormalView
End With

Next s
Call UnhideRows
formula = "=IF(R[-3]C=""Facility Variance Report"",IF(MID(R[-2]C,FIND("":"",R[-2]C)+2,20)=""Baptist Memorial Hos"",MID(R[-2]C,FIND(""_"",R[-2]C)+1,2)&""-""&TRIM((MID(R[-2]C,FIND("":"",R[-2]C)+2,17)))&IF(IFERROR(FIND(""West"",R[-2]C),0)>0,"" W"","""")&"" ""&MID(R[-2]C,FIND(CHAR(1),SUBSTITUTE(R[-2]C,""-"",CHAR(1),2))+1,3)&"" -FVar"",MID(R[-2]C,FIND(""_"",R[-2]C)+1,2)&""-""&TRIM(" & _
    "2]C,FIND("":"",R[-2]C)+2,20)))&IF(IFERROR(FIND(""West"",R[-2]C),0)>0,"" W"","""")&""-FVar""),IF(MID(R[-2]C,FIND("":"",R[-2]C)+2,20)=""Baptist Memorial Hos"",MID(R[-2]C,FIND(""_"",R[-2]C)+1,2)&""-""&TRIM((MID(R[-2]C,FIND("":"",R[-2]C)+2,17)))&IF(IFERROR(FIND(""West"",R[-2]C),0)>0,"" W"","""")&"" ""&MID(R[-2]C,FIND(""-"",R[-2]C,FIND(""-"",R[-2]C)+1)+1,3)&""-LTM"",MID(" & _
    "ND(""_"",R[-2]C)+1,2)&""-""&TRIM((MID(R[-2]C,FIND("":"",R[-2]C)+2,20)))&IF(IFERROR(FIND(""West"",R[-2]C),0)>0,"" W"","""")&""-LTM""))"

For i = 5 To ThisWorkbook.Worksheets.Count
    Worksheets(i).Range("A4").NumberFormat = "General"
    Worksheets(i).Range("A4") = formula
Next i


Call RenameFromA4
Call SortWorkBook
Call listsheets
MsgBox "Report Has Been Updated"
End Sub

为您提供有关宏本身的一些背景知识。我有一张约有94张纸的表格,其中的标签名称是帐号,这让人们很难理解。所以我创建了这个宏,首先将工作簿视图更改为普通视图,而不是取消隐藏电子表格顶部的行,而不是插入给出问题的公式,而不是重命名标签,对它们进行排序,而不是创建/组织目录

任何有关我在线获得运行时错误的见解将不胜感激:

工作表(i)。范围(“A4”)=公式

公式的示例 单元格A1包含:LTM损益表 细胞A2包含:HM_LA_217368 - HM:圣马丁医院

excel中的公式:IF(A1="Facility Variance Report",IF(MID(A2,FIND(":",A2)+2,20)="Baptist Memorial Hos",MID(A2,FIND("_",A2)+1,2)&"-"&TRIM((MID(A2,FIND(":",A2)+2,17)))&IF(IFERROR(FIND("West",A2),0)>0," W","")&" "&MID(A2,FIND("-",A2,FIND("-",A2)+1)+1,3)&"-FVar",MID(A2,FIND("_",A2)+1,2)&"-"&TRIM((MID(2:2,FIND(":",A2)+2,20)))&IF(IFERROR(FIND("West",A2),0)>0," W","")&"-FVar"),IF(MID(A2,FIND(":",A2)+2,20)="Baptist Memorial Hos",MID(A2,FIND("_",A2)+1,2)&"-"&TRIM((MID(A2,FIND(":",A2)+2,17)))&IF(IFERROR(FIND("West",A2),0)>0," W","")&" "&MID(A2,FIND("-",A2,FIND("-",A2)+1)+1,3)&"-LTM",MID(A2,FIND("_",A2)+1,2)&"-"&TRIM((MID(A2,FIND(":",A2)+2,20)))&IF(IFERROR(FIND("West",A2),0)>0," W","")&"-LTM"))

产生“LA-St.Martin Hospital-LTM”的结果,这将是新的标签名称。

所以我将公式连接起来以适应字符串:

formula = "=IF(R[-3]C=""Facility Variance Report"",IF(MID(R[-2]C,FIND("":"",R[-2]C)+2,20)=""Baptist Memorial Hos"",MID(R[-2]C,FIND(""_"",R[-2]C)+1,2)&""-""&TRIM((MID(R[-2]C,FIND("":"",R[-2]C)+2,17)))&IF(IFERROR(FIND(""West"",R[-2]C),0)>0,"" W"","""")&"" ""&MID(R[-2]C,FIND(""-"",R[-2]C,FIND(""-"",R[-2]C)+1)+1,3)&""-FVar""" & _
formula = formula & "MID(R[-2]C,FIND(""_"",R[-2]C)+1,2)&""-""&TRIM((MID(2:2,FIND("":"",R[-2]C)+2,20)))&IF(IFERROR(FIND(""West"",R[-2]C),0)>0,"" W"","""")&""-FVar"")" & _
formula = formula & "IF(MID(R[-2]C,FIND("":"",R[-2]C)+2,20)=""Baptist Memorial Hos"",MID(R[-2]C,FIND(""_"",R[-2]C)+1,2)&""-""&TRIM((MID(R[-2]C,FIND("":"",R[-2]C)+2,17)))&IF(IFERROR(FIND(""West"",R[-2]C),0)>0,"" W"","""")&"" ""&MID(R[-2]C,FIND(""-"",R[-2]C,FIND(""-"",R[-2]C)+1)+1,3)&""-LTM""" & _
formula = forumla & "MID(R[-2]C,FIND(""_"",R[-2]C)+1,2)&""-""&TRIM((MID(R[-2]C,FIND("":"",R[-2]C)+2,20)))&IF(IFERROR(FIND(""West"",R[-2]C),0)>0,"" W"","""")&""-LTM""))"

字符串

仍然出错

0 个答案:

没有答案