Moving an array formula to VBA?

WSBirch

Board Regular
Joined
Apr 10, 2018
Messages
59
Office Version
  1. 365
Platform
  1. Windows
Good morning,
I have this array formula that's listed down a column C2:C69.
Excel Formula:
{=IFERROR(VLOOKUP($B$1,IF('[Workbook2.xlsm]Schedule'!$C$2:$C$5000=$B2,'[Workbook2.xlsm]Schedule'!$A$2:$D$5000,""),4,FALSE),"")}

It checks a two different columns for matching references, and if both match, then it returns the VLOOKUP value for the one that matches both references.

Currently, obviously, the formula runs in each of the 68 cells down the column. C2 starts with the above formula and is repeated down until it stops in C69.

It does a great job. However, is there a way to run this automatically in VBA when there's matching references instead?

For example, the array formula at cell C15 has a value that's returned blank because currently there's no matching references for the VLOOKUP. However, I'd like to be able to manually enter a value, say "123456" into cell C15. While C15 = 123456, if ever there's a time where there is a matching reference that would be returned in C15 instead, I want it to overwrite what's currently written into cell C15.

It's for a dock schedule. I want the cells to automatically return a Trailer # that's recorded in Workbook2, but sometimes there's trailers that we place at locations that are not being recorded and instead need entered manually, but should be overwritten if there's ever something assigned to that location instead.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
It's possible, but sounds like a recipe for disaster - a column that mixes formulae and values, where the values might be overwritten automatically if values change somewhere else in the workbook.

Much easier to see what's happening with something like:
ABCD
1FormulaManualValue
2VLOOKUPOverrideTo use
3
4AResult1Result1
5BMyEntry1MyEntry1
6CResult2MyEntry2Result2
7DResult3Result3
8EMyEntry3MyEntry3
Sheet1
Cell Formulas
RangeFormula
D4:D8D4=IF(B4="",C4,B4)

It would also be easy to add error messages or conditional formatting, e.g. to identify where the formula was blank and a manual override had not been provided.
 
Upvote 0
That's actually a pretty great solution. I like simple like that. I do need to ask then since it's my original request, but slightly different now because you make a good point, is there a way with VBA to "remember" what manually entered value is being overwritten by the VLOOKUP value so when the VLOOKUP value disappears, the manually written in value reappears?
 
Upvote 0
If C2:C69 are formulae, occasionally overwritten with a manual entry when the VLOOKUP errors and returns "", then you could write VBA to

- monitor the lookup table
- replace any manual entry in the results section with a VLOOKUP formula if that VLOOKUP would now work
- store the previous manual entry in case it was needed again
- reinstate any previous manual entries (i.e. retrieve from storage) where the VLOOKUP formula now failed.

The storage/remembering part could be done in a VBA array size 68x1, i.e. the size of the results section.

But for this to persist after you close Excel, you'd need to store somewhere in the workbook. It seems to me the logical place to do this is next to the VLOOKUP formulae, hence my suggestion in Post #2.

So in summary, yes it's possible, but it would complicate the workbook and add VBA calculation overhead.

Just FYI, here's some VBA code for a simpler example - monitoring changes (i.e. user changes, not changes in formula results) in Sheet1!A1:A10:

VBA Code:
'Code Module
Public MyValues As Variant
Public rngToMonitor As Range
'ThisWorkbook module
Private Sub Workbook_Open()
    
    Set rngToMonitor = Worksheets("Sheet1").Range("A1:A10")
    MyValues = rngToMonitor.Value2
    
End Sub
'Sheet1 module
Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim i As Long
    
    If Not Application.Intersect(rngToMonitor, Target) Is Nothing Then
        For i = 1 To UBound(MyValues)
            If rngToMonitor(i).Value <> MyValues(i, 1) Then
                MsgBox "You've changed " & rngToMonitor(i).Address & " from """ & MyValues(i, 1) & """ to """ & rngToMonitor(i).Value & """"
                MyValues(i, 1) = rngToMonitor(i).Value
            End If
        Next i
    End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,424
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top