Populating a Table based on drop down boxes

DavidWT87

New Member
Joined
May 10, 2023
Messages
23
Office Version
  1. 2016
Platform
  1. Windows
Hello

I am hoping to have a table of data that changes based on the inputs chosen.

i.e - I have a blank table I want to use in combination with some formulas, and want a user to be able to choose a Year and Table No. from drop down boxes, and then have that corresponding reference table copy itself into my blank table - which my other formulas will then look at to pull out the data I need.

I could probably figure out how to do it with a lot of if statements, (but it would get horrendously complex- this is just a snapshot example and in reality there are going to be 30-40 reference tables so am wondering if there is something more elegant I can do to deal with that amount of data.

Thanks in advance


Auotmated Testing Analysis.xlsm
BCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAF
2
3J2601 TABLEH70-T40 4-7kg Non-commsTarget Pressure, Ptarget (Mpa)
4
5Year2010Initial Tank Pressure, P0 (Mpa)
60.525101520304050
7TableD25Ambient Temperature, Tamb (oC)-40
8-30
9-20
10-10
110
1210
1320
1425
1530
1635
1740
1845
1950
20
21
222010 TABLE D25Target Pressure, Ptarget (Mpa)2010 TABLE D26Target Pressure, Ptarget (Mpa)
23
24Initial Tank Pressure, P0 (Mpa)Initial Tank Pressure, P0 (Mpa)
250.5251015203040500.525101520304050
26Ambient Temperature, Tamb (oC)-4071.671.170.270.068.566.964.861.558.5Ambient Temperature, Tamb (oC)-4072.672.171.271.069.567.965.862.559.5
27-3072.171.670.670.469.067.465.261.858.7-3073.172.671.671.470.068.466.262.859.7
28-2072.972.371.371.069.568.065.762.460.0-2073.973.372.372.070.569.066.763.461.0
29-1073.472.971.970.070.068.466.564.462.9-1074.473.972.971.071.069.467.565.463.9
30074.073.472.470.670.769.668.667.165.7075.074.473.471.671.770.669.668.166.7
311066.371.174.173.272.471.670.969.668.41067.372.175.174.273.472.671.970.669.4
322067.972.174.573.774.073.472.271.970.72068.973.175.574.775.074.473.272.971.7
332569.072.875.174.574.774.373.373.072.02570.073.876.175.575.775.374.374.073.0
343070.673.975.875.275.475.174.374.173.33071.674.976.876.276.476.175.375.174.3
353572.975.376.476.076.175.975.375.174.53573.976.377.477.077.176.976.376.175.5
364073.275.676.876.376.476.275.675.374.64074.276.677.877.377.477.276.676.375.6
374576.377.276.976.576.476.275.675.374.74577.378.277.977.577.477.276.676.375.7
385077.877.677.376.976.676.275.775.374.75078.878.678.377.977.677.276.776.375.7
39
40
412016 TABLE D25Target Pressure, Ptarget (Mpa)2016 TABLE D26Target Pressure, Ptarget (Mpa)
42
43Initial Tank Pressure, P0 (Mpa)Initial Tank Pressure, P0 (Mpa)
440.5251015203040500.525101520304050
45Ambient Temperature, Tamb (oC)-4073.673.172.272.070.568.966.863.560.5Ambient Temperature, Tamb (oC)-4076.175.674.774.573.071.469.366.063.0
46-3074.173.672.672.471.069.467.263.860.7-3076.676.175.174.973.571.969.766.363.2
47-2074.974.373.373.071.570.067.764.462.0-2077.476.875.875.574.072.570.266.964.5
48-1075.474.973.972.072.070.468.566.464.9-1077.977.476.474.574.572.971.068.967.4
49076.075.474.472.672.771.670.669.167.7078.577.976.975.175.274.173.171.670.2
501068.373.176.175.274.473.672.971.670.41070.875.678.677.776.976.175.474.172.9
512069.974.176.575.776.075.474.273.972.72072.476.679.078.278.577.976.776.475.2
522571.074.877.176.576.776.375.375.074.02573.577.379.679.079.278.877.877.576.5
533072.675.977.877.277.477.176.376.175.33075.178.480.379.779.979.678.878.677.8
543574.977.378.478.078.177.977.377.176.53577.479.880.980.580.680.479.879.679.0
554075.277.678.878.378.478.277.677.376.64077.780.181.380.880.980.780.179.879.1
564578.379.278.978.578.478.277.677.376.74580.881.781.481.080.980.780.179.879.2
575079.879.679.378.978.678.277.777.376.75082.382.181.881.481.180.780.279.879.2
58
59
602020 TABLE D25Target Pressure, Ptarget (Mpa)2020 TABLE D26Target Pressure, Ptarget (Mpa)
61
62Initial Tank Pressure, P0 (Mpa)Initial Tank Pressure, P0 (Mpa)
630.5251015203040500.525101520304050
64Ambient Temperature, Tamb (oC)-4072.872.371.471.269.768.166.062.759.7Ambient Temperature, Tamb (oC)-4073.573.072.171.970.468.866.763.460.4
65-3073.372.871.871.670.268.666.463.059.9-3074.073.572.572.370.969.367.163.760.6
66-2074.173.572.572.270.769.266.963.661.2-2074.874.273.272.971.469.967.664.361.9
67-1074.674.173.171.271.269.667.765.664.1-1075.374.873.871.971.970.368.466.364.8
68075.274.673.671.871.970.869.868.366.9075.975.374.372.572.671.570.569.067.6
691067.572.375.374.473.672.872.170.869.61068.273.076.075.174.373.572.871.570.3
702069.173.375.774.975.274.673.473.171.92069.874.076.475.675.975.374.173.872.6
712570.274.076.375.775.975.574.574.273.22570.974.777.076.476.676.275.274.973.9
723071.875.177.076.476.676.375.575.374.53072.575.877.777.177.377.076.276.075.2
733574.176.577.677.277.377.176.576.375.73574.877.278.377.978.077.877.277.076.4
744074.476.878.077.577.677.476.876.575.84075.177.578.778.278.378.177.577.276.5
754577.578.478.177.777.677.476.876.575.94578.279.178.878.478.378.177.577.276.6
765079.078.878.578.177.877.476.976.575.95079.779.579.278.878.578.177.677.276.6
Sheet2
Cell Formulas
RangeFormula
X26:AF38X26=L26+1
L45:T57L45=L26+2
X45:AF57X45=L26+4.5
L64:T76L64=L26+1.2
X64:AF76X64=L26+1.9
Cells with Data Validation
CellAllowCriteria
F7:G7ListD25,D26
F5:G5List2010,2016,2020
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
I'd be happy to TRY to assist but I'd need a workbook to work with. Otherwise I'd have to make fake data and to guess about what the workbook contains and does. Use the link icon above the message area.
 
Upvote 0
One way could be to give your ranges intuitive names & use the Indirect function to return them based on your choices in E4 & E6. For example, I named your first 2 ranges with actual data as "TD25Y2010" and "TD26Y2010" and used the Indirect function to build the references to them. Example below, and as a separate tip - avoid using merged cells like the plague, they will cause you more harm than good.

Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAE
1
2J2601 TABLEH70-T40 4-7kg Non-commsTarget Pressure, Ptarget (Mpa)
3
4Year2010Initial Tank Pressure, P0 (Mpa)
50.525101520304050
6TableD25Ambient Temperature, Tamb (oC)-4071.671.170.27068.566.964.861.558.5
7-3072.171.670.670.46967.465.261.858.7
8-2072.972.371.37169.56865.762.460
9-1073.472.971.9707068.466.564.462.9
1007473.472.470.670.769.668.667.165.7
111066.371.174.173.272.471.670.969.668.4
122067.972.174.573.77473.472.271.970.7
13256972.875.174.574.774.373.37372
143070.673.975.875.275.475.174.374.173.3
153572.975.376.47676.175.975.375.174.5
164073.275.676.876.376.476.275.675.374.6
174576.377.276.976.576.476.275.675.374.7
185077.877.677.376.976.676.275.775.374.7
19
20
212010 TABLE D25Target Pressure, Ptarget (Mpa)2010 TABLE D26Target Pressure, Ptarget (Mpa)
22
23Initial Tank Pressure, P0 (Mpa)Initial Tank Pressure, P0 (Mpa)
240.5251015203040500.525101520304050
25Ambient Temperature, Tamb (oC)-4071.671.170.27068.566.964.861.558.5Ambient Temperature, Tamb (oC)-4072.671.671.47068.466.262.859.71
26-3072.171.670.670.46967.465.261.858.7-3073.372.37270.56966.763.4611
27-2072.972.371.37169.56865.762.460-2073.972.9717169.467.565.463.91
28-1073.472.971.9707068.466.564.462.9-1074.473.471.671.770.669.668.166.71
2907473.472.470.670.769.668.667.165.7072.175.174.273.472.671.970.669.41
301066.371.174.173.272.471.670.969.668.41073.175.574.77574.473.272.971.71
312067.972.174.573.77473.472.271.970.72073.876.175.575.775.374.374731
32256972.875.174.574.774.373.373722574.976.876.276.476.175.375.174.31
333070.673.975.875.275.475.174.374.173.33076.377.47777.176.976.376.175.51
343572.975.376.47676.175.975.375.174.53576.677.877.377.477.276.676.375.61
354073.275.676.876.376.476.275.675.374.64078.277.977.577.477.276.676.375.71
364576.377.276.976.576.476.275.675.374.74578.678.377.977.677.276.776.375.71
375077.877.677.376.976.676.275.775.374.750111111111
Sheet1
Cell Formulas
RangeFormula
K6:S18K6=INDIRECT("T"&E6&"Y"&E4)
W25:AE37W25=L26+1
Dynamic array formulas.
 
Upvote 0
I'd be happy to TRY to assist but I'd need a workbook to work with. Otherwise I'd have to make fake data and to guess about what the workbook contains and does. Use the link icon above the message area.
Hi. I attached a mini sheet to my post, or do you need something different?
 
Upvote 0
Did Kevin9999 suggestion do what you need? It seems like good advice.
 
Upvote 0
I put together a workbook that does what you want. Unfortunately I could not imagine a way to do it with worksheet lookup formulas so I had to write some code to do it.

The workbook is HERE.

I hope that you have at least some familiarity with VBA code?

I did you a favor and removed merged cells. As kevin9999 said, they really are a pain to deal with! I used cell alignment called Center Across Selection instead. Right click on cells to be formatted then select Format Cells menu item => Alignment tab

As the kevin9999 suggested, I used range names. Excel allows you to give a meaningful name to a cell or range of cells. For example, one source data table is named Table2010D25. In fact, all data tables must have a name with the same general pattern as that or my code will not find them. In this case there was another table which is named Table2010D26. Similarly the cell where user specifies the source data table is named Table.

To apply a name for a source data table select all cells in the respective source data table, including the empty cell in the upper left of the data and including the numeric row and column headers, then use Formula => Name Manager menu item to apply names. Importantly I used names whose "scope" is for the worksheet. Names can be scoped to the workBOOK or workSHEET. You specify the scope -- Sheet1 in my workbook -- when creating the name.

The workbook uses the Worksheet_Change event for the worksheet containing the data tables. I'll leave it to you to read up on worksheet events. In short, worksheet events are "triggered" when something related to the worksheet occurs. The Worksheet_Change event triggers when there is any change to the worksheet. In your case we only want the event to do something -- get the data from the specified source data table -- when someone changes the cell named Table.

VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

'   If user changed the cell name Table then copy the specified source
'   data table and paste its values into the target data table. Do by
'   calling sub named GetTableData.

    If Target.Cells(1) = Me.Range("Table") _
     Then
        Call GetTableData(Me)
    End If

End Sub

If user does change the cell named Table then the Worksheet_Change event calls the sub that gets the data from the specified source data table.

VBA Code:
Option Explicit

' ----------------------------------------------------------------
' Procedure Name: GetTableData
' Purpose: Put the specified source table data's date into the target data table.
' Procedure Kind: Sub
' Procedure Access: Public
' Parameter pwsData (Worksheet): The worksheet object that points to the worksheet to process..
' Author: Jim
' Date: 6/6/2023
' ----------------------------------------------------------------

Sub GetTableData(pwsData As Worksheet)

'   Name of the source data table.
    Dim psSourceTableName As String
   
'   Upperleftmost cell in the target data table (where data is placed).
    Dim rTargetAnchorCell As Range
   
'   The range is set to be the entire source data table including headers.
    Dim rSourceTableRange As Range
   
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
       
    With pwsData
   
'       Set the anchor cell (upperleftmost cell) for the target data range.
'       As of 6/5/23 there is no value in the anchor cell.
        Set rTargetAnchorCell = .Range("TableAnchorCell")
       
'       Get source the data table name using the two named ranges (Cells) in the
'       worksheet: 1. Year and 2. Table. That is used to refer to the table.
        psSourceTableName = "Table" & .Range("Year") & .Range("Table")
       
'       Attempt to point the rSourceTableRange range object to the range
'       whose name is in psSourceTableName -- the source data table.
        On Error Resume Next
        Set rSourceTableRange = .Range(psSourceTableName)
        On Error GoTo 0

'       If the attempt to set the range object above then tell user and exist sub.
        If rSourceTableRange Is Nothing _
         Then
            MsgBox "The named range for Table " _
                   & .Range("Year") & " " & .Range("Table") _
                   & " does not exist.", vbCritical
           
            Exit Sub
        End If
   
'       Copy the source data table then paste values only into the target range.
        rSourceTableRange.Copy
       
        rTargetAnchorCell.PasteSpecial Paste:=xlPasteValues
       
'       Make cell rTargetAnchorCell the active cell.
        rTargetAnchorCell.Select
       
        Application.CutCopyMode = False
       
    End With

    Application.EnableEvents = True

End Sub

Consider using Data Validation to make a dropdown list in the cell named Table.
 
Upvote 0
I put together a workbook that does what you want. Unfortunately I could not imagine a way to do it with worksheet lookup formulas so I had to write some code to do it.

The workbook is HERE.

I hope that you have at least some familiarity with VBA code?

I did you a favor and removed merged cells. As kevin9999 said, they really are a pain to deal with! I used cell alignment called Center Across Selection instead. Right click on cells to be formatted then select Format Cells menu item => Alignment tab

As the kevin9999 suggested, I used range names. Excel allows you to give a meaningful name to a cell or range of cells. For example, one source data table is named Table2010D25. In fact, all data tables must have a name with the same general pattern as that or my code will not find them. In this case there was another table which is named Table2010D26. Similarly the cell where user specifies the source data table is named Table.

To apply a name for a source data table select all cells in the respective source data table, including the empty cell in the upper left of the data and including the numeric row and column headers, then use Formula => Name Manager menu item to apply names. Importantly I used names whose "scope" is for the worksheet. Names can be scoped to the workBOOK or workSHEET. You specify the scope -- Sheet1 in my workbook -- when creating the name.

The workbook uses the Worksheet_Change event for the worksheet containing the data tables. I'll leave it to you to read up on worksheet events. In short, worksheet events are "triggered" when something related to the worksheet occurs. The Worksheet_Change event triggers when there is any change to the worksheet. In your case we only want the event to do something -- get the data from the specified source data table -- when someone changes the cell named Table.

VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

'   If user changed the cell name Table then copy the specified source
'   data table and paste its values into the target data table. Do by
'   calling sub named GetTableData.

    If Target.Cells(1) = Me.Range("Table") _
     Then
        Call GetTableData(Me)
    End If

End Sub

If user does change the cell named Table then the Worksheet_Change event calls the sub that gets the data from the specified source data table.

VBA Code:
Option Explicit

' ----------------------------------------------------------------
' Procedure Name: GetTableData
' Purpose: Put the specified source table data's date into the target data table.
' Procedure Kind: Sub
' Procedure Access: Public
' Parameter pwsData (Worksheet): The worksheet object that points to the worksheet to process..
' Author: Jim
' Date: 6/6/2023
' ----------------------------------------------------------------

Sub GetTableData(pwsData As Worksheet)

'   Name of the source data table.
    Dim psSourceTableName As String
  
'   Upperleftmost cell in the target data table (where data is placed).
    Dim rTargetAnchorCell As Range
  
'   The range is set to be the entire source data table including headers.
    Dim rSourceTableRange As Range
  
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
      
    With pwsData
  
'       Set the anchor cell (upperleftmost cell) for the target data range.
'       As of 6/5/23 there is no value in the anchor cell.
        Set rTargetAnchorCell = .Range("TableAnchorCell")
      
'       Get source the data table name using the two named ranges (Cells) in the
'       worksheet: 1. Year and 2. Table. That is used to refer to the table.
        psSourceTableName = "Table" & .Range("Year") & .Range("Table")
      
'       Attempt to point the rSourceTableRange range object to the range
'       whose name is in psSourceTableName -- the source data table.
        On Error Resume Next
        Set rSourceTableRange = .Range(psSourceTableName)
        On Error GoTo 0

'       If the attempt to set the range object above then tell user and exist sub.
        If rSourceTableRange Is Nothing _
         Then
            MsgBox "The named range for Table " _
                   & .Range("Year") & " " & .Range("Table") _
                   & " does not exist.", vbCritical
          
            Exit Sub
        End If
  
'       Copy the source data table then paste values only into the target range.
        rSourceTableRange.Copy
      
        rTargetAnchorCell.PasteSpecial Paste:=xlPasteValues
      
'       Make cell rTargetAnchorCell the active cell.
        rTargetAnchorCell.Select
      
        Application.CutCopyMode = False
      
    End With

    Application.EnableEvents = True

End Sub

Consider using Data Validation to make a dropdown list in the cell named Table.
Hello. Thank you, I shall take a look at this. It’s a project at work, which I’ve had to put slightly on the back burner since asking the question hence my tardy replies - but thank you for your help. I shall come back once I’m able to take a proper look 👍🏻
 
Upvote 0

Forum statistics

Threads
1,215,412
Messages
6,124,761
Members
449,187
Latest member
hermansoa

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