2

So I have this list that is X rows long. Each has 5 columns: Equipment, Type, Material, Size and Price this is in the Sheet2.

I also have a database in sheet1 with the same column filled in. I have written a code in VBA that for each row in Sheet2 I can fill in Equipment, Type, Material and Size and it will search in the database in sheet1 the matching price for those criteria and past this under the column Price in Sheet2.

Now the problem that I have is if I for example filled in row 1, row 2 and row 3 after each other it works and gives me the price but if I later want to change the variables in row 1 or 2 it doesn't change/update the Price but it still works for row 3 and forward.

How do I make it so that it does change/Update the price in row 1 and 2 if I change the variables there.

my code:

Option Explicit

Public r As Long
Public Const adOpenStatic = 3
Public Const adOpenKeySet = 1
Public Const adLockReadOnly = 1

Sub cmdSearch_Click()
    Dim strCriteriaEquipment As String
    Dim strCriteriaType As String
    Dim strCriteriaMaterial As String
    Dim strCriteriaSize As String
    Dim strSQL As String
    Dim strSourceTable As String
    Dim c As Long, LR As Long

    LR = Cells(Rows.Count, 2).End(xlUp).Row

    For r = 1 To LR
        c = 2
        With Worksheets("Summary")
            strCriteriaEquipment = Worksheets("Summary").Cells(r, c).Value
            strCriteriaType = Worksheets("Summary").Cells(r, c + 1).Value
            strCriteriaMaterial = Worksheets("Summary").Cells(r, c + 2).Value
            strCriteriaSize = Worksheets("Summary").Cells(r, c + 3).Value
        End With
    Next r

    strSourceTable = "[DB$" & Replace(Worksheets("DB").Range("SourceData").Address, "$", "") & "]"
    strSQL = "SELECT [Price] FROM " & strSourceTable & vbNewLine
    strSQL = strSQL & "WHERE [Equipment]= """ & strCriteriaEquipment & """" & vbNewLine
    strSQL = strSQL & "AND [Type]=""" & strCriteriaType & """" & vbNewLine
    strSQL = strSQL & "AND [Material]=""" & strCriteriaMaterial & """" & vbNewLine
    strSQL = strSQL & "AND [Size]=""" & strCriteriaSize & """;"

    Dim rstRecordSet As Object 'ADODB.Recordset
    Dim con As Object 'ADODB.Connection
    Dim strWorkBookPath As String

    strWorkBookPath = ThisWorkbook.FullName

    Set con = CreateObject("ADODB.Connection")
    Set rstRecordSet = CreateObject("ADODB.RecordSet")

    con.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & strWorkBookPath & ";" & _
        "Extended Properties=""Excel 8.0;HDR=Yes"";"
    rstRecordSet.Open strSQL, con, adOpenStatic, adLockReadOnly

    With Worksheets("Summary")
        For r = r - 29 To LR
            c = 5
            If Not (rstRecordSet.EOF And rstRecordSet.BOF) Then
                .Range("ResultTable").Cells(r, c).CopyFromRecordset rstRecordSet
            Else
                .Range("ResultTable").Cells(r, c).Value = "Data Not Found!"
            End If
        Next r
    End With

    rstRecordSet.Close
    con.Close
    Set rstRecordSet = Nothing
    Set con = Nothing
    strWorkBookPath = vbNullString

    strSQL = vbNullString
    strCriteriaEquipment = vbNullString
    strCriteriaType = vbNullString
    strCriteriaMaterial = vbNullString
    strCriteriaSize = vbNullString

    strSourceTable = vbNullString
End Sub


Public Function UniqueStringWithDelimiter(varArray As Variant, strDelimiter As String) As Variant
    Dim varTemp() As Variant
    Dim lngLoop As Long
    Dim strConcat As String
    ReDim Preserve varTemp(0 To 0)

    varTemp(0) = varArray(0, 0)
    strConcat = strConcat & varArray(0, 0)

    For lngLoop = 1 To UBound(varArray, 2)
        If InStr(1, strConcat, varArray(0, lngLoop), vbTextCompare) = 0 Then
            strConcat = strConcat & strDelimiter & varArray(0, lngLoop)
        End If
    Next lngLoop

    UniqueStringWithDelimiter = strConcat.
    strConcat = vbNullString
    Erase varTemp

End Function

Now to update everytime I change something in Sheet2 I just wrote this:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Call cmdSearch_Click
End Sub

So again my question how do I update/change the price if I change a variable in row 1 or row 2 if row 3 was the last row that was used in the sheet.

This is the datbase that I am using:
This is the Datbase that i am using

This is Sheet2:
This is Sheet2

1
  • 2
    Just a note: There is no need to Set ANYTHING = Nothing or emptying a string like strANYTHING = vbNullString. This is completely useless because VBA does this automatically on End Sub so no benefit doing this. Commented Sep 18, 2017 at 13:12

1 Answer 1

3

1) One immediate problem I see that will cause your issue (and there may be more, but I don't have time to dissect so much at this moment), is that the initial loop:

For r = 1 To LR
c = 2
With Worksheets("Summary")
    strCriteriaEquipment = Worksheets("Summary").Cells(r, c).Value
    strCriteriaType = Worksheets("Summary").Cells(r, c + 1).Value
    strCriteriaMaterial = Worksheets("Summary").Cells(r, c + 2).Value
    strCriteriaSize = Worksheets("Summary").Cells(r, c + 3).Value

End With
Next r

is not doing what you may expect. At the end of this loop you only have set the values for the last row of data (I suspect row 3) to pass into your query.

You'll need to write your queries inside this loop as well so that the query is run for each set of criteria in each line.

For example:

For r = 1 to LR
    c = 2
    With Worksheets("Summary")
        'code to set criteria
    End With
    'code to download data price
    'code to stick data and price in summary tab
Next r

2) Also, make sure to qualify all your objects. The line

LR = Cells(Rows.Count, 2).End(xlUp).Row

may return different results if the sheet you desire to be active is not actually active. Better to say this, for example, and leave out guess works:

LR = Worksheets("Summary").Cells(Rows.Count, 2).End(xlUp).Row

3) Using Worksheet_SelectionChange will fire your code every time you move from one to another in your worksheet. If you want to only fire the code when you make a change to the criteria in your data, use Worksheet_Change instead. You can also define which specific cells being changes will run the code as well.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Scott Holtzman! This was super helpfull i got it to work and thanks foor the extra tips :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.