简体   繁体   中英

Excel VBA: how to rewrite my long FormulaArray code with .replace

How to rewrite my long .FormulaArray using .Replace . Please help me

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

I tried following code. result showing only as Part2 . the program not replace formula in Part2 and Part3 . 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

Please help me how to solve this.

Here is correct formula using .Replace funcation,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM