}
Option Explicit
Option Base 1
Sub PrepaFormulas()
' PrepaFormulas Macro
Const Hd1 As String = "Net Covers"
Const Hd2 As String = "Food (1)"
Const Hd3 As String = "Service Charge"
Dim WsN As String
Dim WkWs As Worksheet
Dim Form1 As String, Form2 As String, Form3 As String, Form4 As String
Dim FC As Integer, LC As Integer, FR As Integer, LR As Integer
Dim WkAdd As String, WkRg As Range
Dim RgMx As Variant
Dim F, T
Dim AAA, BBB, CCC
T = Array("#", "µ1", "µ2", "µ3")
ReDim RgMx(1 To UBound(T, 1), 1 To 1)
Dim I As Integer
For I = 1 To 4
RgMx(I, 1) = T(I)
Next I
ReDim Preserve RgMx(1 To UBound(RgMx, 1), 1 To 2)
Dim RgAdd1 As String, RgAdd2 As String, RgAdd3 As String
Form1 = "=IFERROR(INDEX('#'!µ1,MATCH($F7,'#'!µ2,0),MATCH($K$6,'#'!µ3,0)),0)"
Form2 = "=IFERROR(INDEX('#'!µ1,MATCH(M$6,'#'!µ2,0),MATCH($F7,'#'!µ3,0)),0)"
Form3 = "=IFERROR(INDEX('#'!µ1,MATCH(Q$6,'#'!µ2,0),MATCH($F7,'#'!µ3,0)),0)"
Form4 = "=IFERROR(INDEX('#'!µ1,MATCH($F7,'#'!µ2,0),MATCH($S$6,'#'!µ3,0)),0)"
WsN = Range("F2")
If (Not (Evaluate("isref('" & WsN & "'!A1)"))) Then MsgBox (" Sheet : " & WsN & vbCrLf & " DO NOT exist "): Exit Sub
Set WkWs = Sheets(WsN)
With WkWs
'--------------------------
Set F = .Cells.Find(Hd1, LookIn:=xlValues, Lookat:=xlWhole)
If Not (F Is Nothing) Then
FC = 1
FR = F.Row
LC = .Cells(FR, Columns.Count).End(xlToLeft).Column
LR = .Cells(FR + 1, FC).End(xlDown).Row
RgMx(1, 2) = WsN
RgMx(2, 2) = Range(.Cells(FR + 1, FC), .Cells(LR, LC)).Address
RgMx(3, 2) = Range(.Cells(FR + 1, FC), .Cells(LR, FC)).Address
RgMx(4, 2) = Range(.Cells(FR, FC), .Cells(FR, LC)).Address
For I = 1 To UBound(RgMx, 1)
Form1 = Replace(Form1, RgMx(I, 1), RgMx(I, 2))
Next I
Set WkRg = Range(Cells(7, "K"), Cells(Rows.Count, "K").End(3))
WkRg.Formula = Form1
End If
'--------------------------
Set F = .Cells.Find(Hd2, LookIn:=xlValues, Lookat:=xlWhole)
If Not (F Is Nothing) Then
FC = 3
FR = F.Row
LC = .Cells(FR + 1, Columns.Count).End(xlToLeft).Column
LR = .Cells(FR + 1, FC).End(xlDown).Row
RgMx(1, 2) = WsN
RgMx(2, 2) = Range(.Cells(FR, FC), .Cells(LR, LC)).Address
RgMx(3, 2) = Range(.Cells(FR, FC), .Cells(LR, FC)).Address
RgMx(4, 2) = Range(.Cells(FR - 1, FC), .Cells(FR - 1, LC)).Address
For I = 1 To UBound(RgMx, 1)
Form2 = Replace(Form2, RgMx(I, 1), RgMx(I, 2))
Next I
Set WkRg = Range(Cells(7, "M"), Cells(Rows.Count, "M").End(3)).Resize(, 3)
WkRg.Formula = Form2
For I = 1 To UBound(RgMx, 1)
Form3 = Replace(Form3, RgMx(I, 1), RgMx(I, 2))
Next I
Set WkRg = Range(Cells(7, "Q"), Cells(Rows.Count, "Q").End(3)).Resize(, 2)
WkRg.Formula = Form3
End If
'--------------------------
'=IFERROR(INDEX('1'!$A$173:$AQ$212,MATCH($F7,'1'!$A$173:$A$212,0),MATCH($S$6,'1'!$A$172:$AQ$172,0)),0)
Set F = .Cells.Find(Hd3, LookIn:=xlValues, Lookat:=xlWhole)
If Not (F Is Nothing) Then
FC = 1
FR = F.Row
LC = .Cells(FR + 1, Columns.Count).End(xlToLeft).Column
LR = .Cells(FR + 1, FC).End(xlDown).Row
RgMx(1, 2) = WsN
RgMx(2, 2) = Range(.Cells(FR + 1, FC), .Cells(LR, LC)).Address
RgMx(3, 2) = Range(.Cells(FR + 1, FC), .Cells(LR, FC)).Address
RgMx(4, 2) = Range(.Cells(FR, FC), .Cells(FR, LC)).Address
For I = 1 To UBound(RgMx, 1)
Form4 = Replace(Form4, RgMx(I, 1), RgMx(I, 2))
Next I
Set WkRg = Range(Cells(7, "S"), Cells(Rows.Count, "S").End(3))
WkRg.Formula = Form4
End If
End With
MsgBox ("Job Done")
'
End Sub