Excel VBA:如何使用.replace重写我的长FormulaArray代码

时间:2017-03-23 06:29:51

标签: arrays excel-vba replace vba excel

如何使用.FormulaArray重写我的长.Replace。请帮帮我

Private Sub CommandButton2_Click()
Dim x As Variant
Dim y As Variant

Workbooks.Open (TextBox3.Text)

x = Split(TextBox2.Value, Application.PathSeparator)
y = Split(TextBox3.Value, Application.PathSeparator)

Workbooks(x(UBound(x))).Sheets("KomponentenohneDuplikate").Activate

Workbooks(x(UBound(x))).Sheets("KomponentenohneDuplikate").Range("z5").FormulaArray = "=iferror(INDEX('[" & y(UBound(y)) & "]Regelung&Partno'!C1:C10,MATCH(1,('[" & y(UBound(y)) & "]Regelung&Partno'!C5=RC[-25])*('[" & y(UBound(y)) & "]Regelung&Partno'!C9=""Diverse""),0),10),""-"")"
With Workbooks(x(UBound(x))).Sheets("KomponentenohneDuplikate").Range("y5:y3263")
.FillDown
.Value = .Value
End With

我尝试了以下代码。结果仅显示为Part2。该计划不会取代Part2Part3中的公式。     Dim FormulaPart1 As String     Dim FormulaPart2 As String     Dim FormulaPart3 As String     DIM origRS As Long

origRS = Application.ReferenceStyle
Application.ReferenceStyle = xlR1C1

FormulaPart1 = "=IFerror(""Part2"",""-"")"
FormulaPart2 = "INDEX('[" & y(UBound(y)) & "]Regelung&Partno'!C1:C10,""Part3"",0),10)"
FormulaPart3 = "MATCH(1,('[" & y(UBound(y)) & "]Regelung&Partno'!C5=RC[-25])*('[" & y(UBound(y)) & "]Regelung&Partno'!C9=""Diverse"")"

With Workbooks(x(UBound(x))).Worksheets("KomponentenohneDuplikate").Range("z5")

.FormulaArray = FormulaPart1
.Replace what:="""Part2""", replacement:=FormulaPart2, lookat:=xlPart
.Replace what:="""Part3""", replacement:=FormulaPart3, lookat:=xlPart

End With
Application.ReferenceStyle = origRS

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

这是使用.Replace功能

的正确公式
Dim FormulaPart1 As String
Dim FormulaPart2 As String
Dim FormulaPart3 As String
Dim origRS As Long

origRS = Application.ReferenceStyle
Application.ReferenceStyle = xlR1C1

FormulaPart1 = "=IFERROR(""Part2"",""-"")"
FormulaPart2 = "INDEX('[" & y(UBound(y)) & "]Regelung&Partno'!C1:C10,""Part3"",10)"
FormulaPart3 = "MATCH(1,('[" & y(UBound(y)) & "]Regelung&Partno'!C5=RC[-25])*('[" & y(UBound(y)) & "]Regelung&Partno'!C9=""Diverse""),0)"

With     Workbooks(x(UBound(x))).Worksheets("KomponentenohneDuplikate").Range("z5")

.FormulaArray = FormulaPart1
.Replace what:="""Part2""", replacement:=FormulaPart2, lookat:=xlPart
.Replace what:="""Part3""", replacement:=FormulaPart3, lookat:=xlPart

End With
Application.ReferenceStyle = origRS