如何在我的宏中更改我的工作表数组引用

时间:2016-03-24 20:39:59

标签: excel vba excel-vba macros

我在excel中记录了以下代码。基本上我想复制相同的公式 IF(A1="Facility Variance Report",MID(A2,FIND("_",A2)+1,2)&"-"&TRIM((MID(A2,FIND(":",A2)+2,23)))&"-Var",MID(A2,FIND("_",A2)+1,2)&"-"&TRIM((MID(A2,FIND(":",A2)+2,23)))&"-Inc") 进入除前三个之外的每个工作表。我的问题是,无论如何我可以缩短从第三个工作表开始的数组引用,而不是那个之后的每个工作表?我问的原因是因为无法确定工作表名称每次都会以相同的方式出现。

    Sub Macro1()
       Sheets("OH-Knox Community Hospital-Var ").Select
    ActiveWindow.ScrollWorkbookTabs Position:=xlLast
    Sheets(Array("OH-Knox Community Hospital-Var ", "OH-Knox Community Hospital-Inc" _
        , "WV-Bluefield Regional Medic-Var", "WV-Bluefield Regional Medical C", _
        "FL-Wuesthoff Medical Center-Var", "FL-Wuesthoff Medical Center - R", _
        "NC-Johnston Medical Center-Var", "NC-Johnston Medical Center-Inc", _
        "LA-Ochsner Medical Center --Var", "=MID(A2,FIND(""_"",A2)+1,2)&""-""&I", _
        "WV-Plateau Medical Center --Var", "LTM Income Stat-HM_WV_469593 -", _
        "Facility Varian-HM_NM_15140 - ", "LTM Income Stat-HM_NM_15140 - ", _
        "Facility Varian-HM_NC_469584 -", "LTM Income Stat-HM_NC_469584 -", _
        "Facility Varian-HM_IL_416691 -", "LTM Income Stat-HM_IL_416691 -", _
        "Facility Varian-HM_AR_12253 - ", "LTM Income Stat-HM_AR_12253 - ", _
        "Facility Varian-HM_MS_414655 -", "LTM Income Stat-HM_MS_414655 -", _
        "Facility Varian-HM_AR_12521 - ", "LTM Income Stat-HM_AR_12521 - ", _
        "Facility Varian-HM_MS_475748 -")).Select
    Sheets("OH-Knox Community Hospital-Var ").Activate
    Sheets(Array("LTM Income Stat-HM_MS_475748 -", "Facility Varian-HM_TX_416680 -", _
        "LTM Income Stat-HM_TX_416680 -", "Facility Varian-HM_TX_416690 -", _
        "LTM Income Stat-HM_TX_416690 -", "Facility Varian-HM_TX_469595 -", _
        "LTM Income Stat-HM_TX_469595 -", "Facility Varian-HM_MS_469582 -", _
        "LTM Income Stat-HM_MS_469582 -", "Facility Varian-HM_MS_469586 -", _
        "LTM Income Stat-HM_MS_469586 -", "Facility Varian-HM_OK_416699 -", _
        "LTM Income Stat-HM_OK_416699 -", "Facility Varian-HM_AZ_395470 -", _
        "LTM Income Stat-HM_AZ_395470 -", "Facility Varian-HM_NM_469587 -", _
        "LTM Income Stat-HM_NM_469587 -", "Facility Varian-HM_NM_469588 -", _
        "LTM Income Stat-HM_NM_469588 -", "Facility Varian-HM_NM_469589 -", _
        "LTM Income Stat-HM_NM_469589 -", "Facility Varian-HM_NM_469590 -", _
        "LTM Income Stat-HM_NM_469590 -", "Facility Varian-HM_WA_391716 -", _
        "LTM Income Stat-HM_WA_391716 -")).Select Replace:=False
    Sheets(Array("Facility Varian-HM_WA_391717 -", "LTM Income Stat-HM_WA_391717 -", _
        "Facility Varian-HM_WA_391718 -", "LTM Income Stat-HM_WA_391718 -", _
        "Facility Varian-HM_AZ_469592 -", "LTM Income Stat-HM_AZ_469592 -", _
        "Facility Varian-HM_KY_475745 -", "LTM Income Stat-HM_KY_475745 -", _
        "Facility Varian-HM_NC_470554 -", "LTM Income Stat-HM_NC_470554 -", _
        "Facility Varian-HM_KY_469591 -", "LTM Income Stat-HM_KY_469591 -", _
        "Facility Varian-HM_WV_469583 -", "LTM Income Stat-HM_WV_469583 -", _
        "Facility Varian-HM_SC_182783 -", "LTM Income Stat-HM_SC_182783 -", _
        "Facility Varian-HM_FL_475750 -", "LTM Income Stat-HM_FL_475750 -", _
        "Facility Varian-HM_CO_475749 -", "LTM Income Stat-HM_CO_475749 -", _
        "Facility Varian-HM_CO_475751 -", "LTM Income Stat-HM_CO_475751 -", _
        "Facility Varian-HM_CO_10176 - ", "LTM Income Stat-HM_CO_10176 - ", _
        "Facility Varian-HM_FL_244515 -")).Select Replace:=False
    Sheets(Array("LTM Income Stat-HM_FL_244515 -", "Facility Varian-HM_FL_475746 -", _
        "LTM Income Stat-HM_FL_475746 -", "Facility Varian-HM_LA_39597 - ", _
        "LTM Income Stat-HM_LA_39597 - ", "Facility Varian-HM_LA_161289 -", _
        "LTM Income Stat-HM_LA_161289 -", "Facility Varian-HM_LA_33025 - ", _
        "LTM Income Stat-HM_LA_33025 - ", "Facility Varian-HM_LA_351095 -", _
        "LTM Income Stat-HM_LA_351095 -", "Facility Varian-HM_LA_310198 -", _
        "LTM Income Stat-HM_LA_310198 -", "Facility Varian-HM_LA_217368 -", _
        "LTM Income Stat-HM_LA_217368 -")).Select Replace:=False
    ActiveCell.FormulaR1C1 = ""
    Range("A4:F4").Select
    ActiveCell.FormulaR1C1 = ""
    Range("A4:F4").Select
    Selection.NumberFormat = "General"
    ActiveCell.FormulaR1C1 = _
        "=IF(R[-3]C=""Facility Variance Report"",MID(R[-2]C,FIND(""_"",R[-2]C)+1,2)&""-""&TRIM((MID(R[-2]C,FIND("":"",R[-2]C)+2,23)))&""-Var"",MID(R[-2]C,FIND(""_"",R[-2]C)+1,2)&""-""&TRIM((MID(R[-2]C,FIND("":"",R[-2]C)+2,23)))&""-Inc"")"
    Range("A5").Select
End Sub

1 个答案:

答案 0 :(得分:1)

这段代码可以解决问题:

Sub insert_formula()

    Dim formula As String

    formula = "=IF(R[-3]C=""Facility Variance Report"",MID(R[-2]C,FIND(""_"",R[-2]C)+1,2)&""-""&TRIM((MID(R[-2]C,FIND("":"",R[-2]C)+2,23)))&""-Var"",MID(R[-2]C,FIND(""_"",R[-2]C)+1,2)&""-""&TRIM((MID(R[-2]C,FIND("":"",R[-2]C)+2,23)))&""-Inc"")"

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

End Sub

粘贴到您的vba模块并运行它。