我有一个完美运行的宏,直到我必须创建一个长公式来处理工作表中的某些场景。该公式基本上从文本中创建一个名称,我继续并使用此名称重命名单元格的选项卡。这是vba代码:
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(""-"",R[-2]C,FIND(""-"",R[-2]C)+1)+1,3)&""-FVar"",MID(R[-2]C,FIND(""_"",R[-2]C)+1,2)&""-""&MID(R[-2]C,FIN" & _
"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"","""")&""-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(""-" & _
")+1)+1,3)&""-LTM"",MID(R[-2]C,FIND(""_"",R[-2]C)+1,2)&""-""&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""))"
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
这是公式:
=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(CHAR(1),SUBSTITUTE(A2,"-",CHAR(1),2))+1,3)&" -FVar",MID(A2,FIND("_",A2)+1,2)&"-"&MID(A2,FIND("_",A2)+1,2)&"-"&TRIM((MID(A2,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)&"-"&MID(A2,FIND("_",A2)+1,2)&"-"&TRIM((MID(A2,FIND(":",A2)+2,20)))&IF(IFERROR(FIND("West",A2),0)>0," W","")&"-LTM"))
我需要帮助是否有一种方法可以连接字符串?或者我必须找到一种方法来浓缩公式?
答案 0 :(得分:1)
继续使用formula
formula = formula + "text"
变量
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(""-"",R[-2]C,FIND(""-"",R[-2]C)+1)+1,3)&""-FVar"",MID(R[-2]C,FIND(""_"",R[-2]C)+1,2)&""-""&MID(R[-2]C,FIN"
formula = formula + "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"","""")&""-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(""-"
formula = formula + ")+1)+1,3)&""-LTM"",MID(R[-2]C,FIND(""_"",R[-2]C)+1,2)&""-""&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""))"
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