VBA lookup reference

jakobt

Active Member
Joined
May 31, 2010
Messages
337
Want to develop a piece of VBA code.

Basically:

I have a table of data from a3:H1000

In cell B1 I want a reference to sum the cells in column B, based on a criteria in column F.

If the row in column F equals "VAT Return" the sum of column B for the same row should be added to Cell B1.

It is important that the formula in B1 is updated with format like: +B5+ B7 + B12.
 

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).
if you want to see the references in the cell, try something like this
Code:
Sub SumVATItems()
    Dim cel As Range, rng As Range, ws As Worksheet, fStr As String, addr As String, B2 As Range
    Set ws = ActiveSheet
    Set B2 = ws.Range("B2")
    Set rng = ws.Range("A3:H1000").CurrentRegion
    On Error Resume Next
    ws.ShowAllData
    rng.AutoFilter Field:=6, Criteria1:="VAT Return"
    For Each cel In rng.Columns(6).SpecialCells(xlCellTypeVisible)
        addr = Cells(cel.Row, "B").Address(0, 0)
        If cel.Row > 3 Then
            If fStr = vbNullString Then fStr = "=" & addr Else fStr = fStr & "+" & addr
        End If
    Next
    B2.ClearContents
    If Not fStr = vbNullString Then B2.Formula = fStr
    ws.ShowAllData
End Sub

Should yield same result as this formula
=SUMIF(F4:F1000,"VAT Return",B4:B1000)
 
Upvote 0
Thanks something like this I need.

However, the word VAT return will be included as part of a string. For example Q4 2018 VAT return.
 
Upvote 0
Code:
 rng.AutoFilter Field:=6, Criteria1:="*VAT Return*"

formula equivalent
=SUMIF(F4:F1000,"*VAT Return*",B4:B1000)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,203,069
Messages
6,053,352
Members
444,655
Latest member
didr

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