[Help] VBA - Search column, compare, call if match etc.

WarrenKD

New Member
Joined
Dec 22, 2022
Messages
1
Office Version
  1. 2021
Platform
  1. Windows
Hello,

I was hoping to get some help here regarding some VBA code. I've searched through Google so many times and tried multiple ways, but this has totally thrown me off. I'm half decent with VBA, by that I mean I can read it and join sentences to make it work but don't fully understand it... Y'know? :) I didn't think this would stump me as much as it has, and it seems so simple!

I am looking to use a button on worksheet"D" that then triggers a search on worksheet"B". It will search column D and if column D is lower than or equal to the cell in column E on the same row, then take the info from column A, B and D and place into table "AutoStockCheck" on worksheet"D", then call my email function. Then move onto the next row until it finds a value below again and loop the same options. except it adds into the table, below the previous one. Although when pressing the button on worksheet"D" again, it will clear contents of table to start again.

I've had a go multiple times and deleted the code out of frustration so can't really say where I'm up to :)

Hope I haven't complicated it within my explanation. Any help is much appreciated and can clarify anything if needed. TIA.

Example of table to search:
1671739883476.png
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi WarrenD,

maybe this code gets you started (you already have the procedure to mail which call should be added):
VBA Code:
Public Sub MrE_1225320_1616610()
' https://www.mrexcel.com/board/threads/help-vba-search-column-compare-call-if-match-etc.1225320/
' Created: 20221224

Dim objLO         As ListObject
Dim lngCnt        As Long
Dim lngWrite      As Long
Dim arrData

Set objLO = Worksheets("D").ListObjects("AutoStockCheck")
With objLO.DataBodyRange
  If .Rows.Count > 1 Then
    .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count).Rows.Delete
  End If
End With
objLO.DataBodyRange.Rows(1).ClearContents

With Worksheets("B")
  arrData = .Range("A2", .Cells(.Rows.Count, 5).End(xlUp))
End With

For lngCnt = 1 To UBound(arrData)
  If (arrData(lngCnt, 4) <= arrData(lngCnt, 5)) Then
    lngWrite = lngWrite + 1
    objLO.DataBodyRange(lngWrite, 1) = arrData(lngCnt, 1)
    objLO.DataBodyRange(lngWrite, 2) = arrData(lngCnt, 2)
    objLO.DataBodyRange(lngWrite, 3) = arrData(lngCnt, 4)
    objLO.ListRows.Add AlwaysInsert:=True
  End If
Next lngCnt

Set objLO = Nothing

End Sub

Ciao,
Holger
 
Upvote 0
Solution

Forum statistics

Threads
1,223,247
Messages
6,171,007
Members
452,374
Latest member
keccles

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