Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

The three data tabs connected to database should automatically refresh when the input changes in the another tab

0

I have three data tabs connected to a database with three different views which depicts the description of an item whose ID(s) is/are entered by the user in the input tab(the user can only enter in one of the fields; either SKU or Web ID or DVCS).

The input can be only be one of the following:

i. One or a List of SKUIDs 

OR

ii. One or a List of  WEB_IDs :

OR

iii. One or a List of D-V-S-Cs 

So whenever the above input changes the data tabs should be refreshed automatically to retrieve records of those ID(s) 

In the attached sample file I have only given one ID for SKU field as an example and its corresponding data points are shown in the other three tabs.

What would be the approach?

Answer
Discuss

Discussion

Hi again Dr Liss

Sorry but I'm struggling to follow what you're trying to do here - your test file gives me no clues of how "A -ID" (say) is linked to the data tabs or what data might be returned or modified in response to what input changes are made.

Please modify your original question (and file) to give an example showing what input is made and what response or changes you need).
John_Ru (rep: 6152) Jun 29, '21 at 4:16 am
Hi John,
I have revised my problem statement. Hope it helps you to understand better!
Dr Liss (rep: 26) Jun 29, '21 at 6:39 am
Add to Discussion

Answers

0
Selected Answer

You may accomplish what you describe with a simple Worksheet_Change event procedure. Try the one below.

Private Sub Worksheet_Change(ByVal Target As Range)
    ' 273

    Const TriggerRange  As String = "D3,D4,D5"          ' specify 3 cells
    Const LookupClm     As String = "B,A,J"             ' columns relate to TriggerRange

    Dim Ws              As Worksheet                    ' Data source
    Dim LookupRng       As Range
    Dim Rs              As Long                         ' Ws row number
    Dim Clm()           As String                       ' split of LookupClm
    Dim Triggers        As Range
    Dim TriggerID       As Integer                      ' identify the trigger
    Dim i               As Integer                      ' loop counter: Triggers
    Dim Rt              As Long                         ' Target row

    Set Triggers = Range(TriggerRange)
    If Not Application.Intersect(Target, Triggers) Is Nothing Then
        Application.EnableEvents = False                ' prevent next action from calling this proc
        With Target
            For i = 1 To Triggers.Areas.Count
                If Triggers.Areas(i).Address = .Address Then
                    TriggerID = i
                Else
                    Triggers.Areas(i).ClearContents
                End If
            Next i

            Clm = Split("," & LookupClm, ",")           ' align index with TriggerID
            Set Ws = Worksheets("Data1")
            With Ws
                Set LookupRng = .Range(.Cells(1, Clm(TriggerID)), _
                                       .Cells(.Rows.Count, Clm(TriggerID)).End(xlUp))
            End With
            On Error Resume Next
            Rs = WorksheetFunction.Match(.Value, LookupRng, 0)
            If Err Then
                MsgBox "The search for """ & .Value & """ was unsuccessful.", _
                       vbInformation, "Invalid search criterium"
            Else
                With Ws
                    Set LookupRng = .Range(.Cells(Rs, 1), _
                                           .Cells(Rs, .Columns.Count).End(xlToLeft))
                End With
                Rt = 7                                  ' first row to write to (-1)
                For i = 1 To LookupRng.Cells.Count
                    Cells(i + Rt, "C").Value = Ws.Cells(1, i).Value
                    Cells(i + Rt, "D").Value = LookupRng.Cells(i).Value
                Next i
            End If
        End With
        Application.EnableEvents = True
    End If
End Sub

You were asking for an approach. Therefore my focus was on how to make sure that only one entry by the user is processed. Like your own sample, my code also doesn't deal with the difference between your 3 Data tabs but the method shown can be extended with little effort.

The part that probably needs a little explaining is the relationship between the two constants TriggerRange and LookupClm at the top of the code. The trigger range is D3:D5 but I specified D3,D4,D5. The difference is that you could specify non-contiguous cells and the code can handle that (not tested).

If any of these cells is changed the procedure will fire. The value entered must be looked up in different columns. D3 must be looked up in column B, D4 in column A and since I couldn't figure out where to look for "DVSC" I assigned column J. You can assign different columns to suit your needs.

All my lookups are done on the same sheet. Of course, columns A, B and J could also be on different sheets. In that case you need to expand the definition of Ws to be different depending upon the TriggerID.

Data are retreved from the worksheet specified by the TriggerID. In the attached workbook select D3, press F2 and ENTER for a demonstration. Of course, the target area could be anywhere in the workbook.

Discuss

Discussion

Thanks, Variatus. I have revised my attachment to a new view in the input tab rest all the tabs remain as is. How will the code look?
I see in your code, the worksheet change event triggers to display data in the input tab. But per my requirement, it should result in refreshing the data tabs connect to SQL query(already written) to fetch those ID results.
Dr Liss (rep: 26) Jul 6, '21 at 6:06 am
Dr Liss,
This promises to be endless. My answer solves your problem of enabling only one of 3 possible user entries. You want to reject that solution because the action taken isn't to your liking.
Instead, I suggest you upgrade your question to exclude the part that is solved and rephrase it for the parts that we don't understand. Apparently you want the user entry to trigger an SQL query. That is possible but your question includes no details about that query.
Once the query has been triggered data it generates are supposed to be distributed to your three tabs. That is possible, too. But without knowing the nature of that data no suggestion can be offered. And, anyway, that problem is too far away. Integrate the partial solution you have here into your project so that you can advance to the next hurdle.
You were right in looking for an approach. Further expanding the question in more discussion and more amendments promises more complication but no solution. I think an approach has been suggested. A first step has been shown. There is no more juice to be squeezed from this stone.
Variatus (rep: 4889) Jul 6, '21 at 7:26 am
Understood your point. Let me take it from here and break the problem statement into pieces and integrate it finally. I will try to create another thread for the next piece if I need help!
Thank you for your advice.
Dr Liss (rep: 26) Jul 7, '21 at 5:45 am
Add to Discussion
0

Dr Liss

Not sure how/where your data is coming from (to populate sheets Data 1, Dat 2 and Data 3) but I think an approach might be:

In Input sheet:

  1. use Data Validation on cells D3:D5 to ensure only one cell has a value
  2. use Private Sub Worksheet_Change(ByVal Target As Range) to detect a change of input
  3. use VBA's Split function to split any multi item list into an array
  4. loop through that array and...
  5. use whatever database query method you have to upate the other sheets
Discuss

Discussion

Thanks, John_Ru.These were my initial steps apparently and we match on that.
To answer your question, the data is coming through an external source through SQL query.
Do you have any idea on how will the user input value(s) be passed on to the data tabs for it refresh based on those values?
Dr Liss (rep: 26) Jun 29, '21 at 9:10 am
Dr Liss
I haven't sent queries to SQL database (previously I could rely on Database Administrators to do that for me!) but found this elsewhere Using SQL in VBA on Excel. Run SELECT Queries from VBA which should give you a start. Sorry I can't help more. Perhaps Variatus can add to this (but I think he's been busy on other things recently)
John_Ru (rep: 6152) Jun 29, '21 at 10:45 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login