excel - How to create a unique identifier or equivalent to a dynamic table in Excel?
问题描述
I am assisting our artwork department to create a Graph with information in regards to time spent at each stage (Designing and approving artwork).
Originally I created a drop down filter menu and then used VLookUP to populate the relevant fields. These are all in a date format so I used the DATEDIF function to determine the days taken at each state.
I then want to populate a waterfall chart with that data.
I have recently been learning Macros and VBA and though it would be 'smarter' to create a search box to filter the data.
I was then going to duplicate that data into another table to create the source for my chart.
Unfortunately through testing I realized I cant use VlookUp or equivalent because the table collapses and the Cell identifier is no longer the same as that in the top of the list.
Is there a way I can do this? Am I going about it wrong?
The code for the search box is the following:
Sub SearchBox()
'PURPOSE: Filter Data on User-Determined Column & Text/Numerical value
Dim myButton As OptionButton
Dim SearchString As String
Dim ButtonName As String
Dim sht As Worksheet
Dim myField As Long
Dim DataRange As Range
Dim mySearch As Variant
'Load Sheet into A Variable
Set sht = ActiveSheet
'Unfilter Data (if necessary)
On Error Resume Next
sht.ShowAllData
On Error GoTo 0
'Filtered Data Range (include column heading cells)
Set DataRange = sht.Range("AA21:AN28") 'Cell Range
'Set DataRange = sht.ListObjects("Table1").Range 'Table
'Retrieve User's Search Input
mySearch = sht.Shapes("UserSearch").TextFrame.Characters.Text 'Control Form
'mySearch = sht.OLEObjects("UserSearch").Object.Text 'ActiveX Control
'mySearch = sht.Range("A1").Value 'Cell Input
'Determine if user is searching for number or text
If IsNumeric(mySearch) = True Then
SearchString = "=" & mySearch
Else
SearchString = "=*" & mySearch & "*"
End If
'Loop Through Option Buttons
For Each myButton In sht.OptionButtons
If myButton.Value = 1 Then
ButtonName = myButton.Text
Exit For
End If
Next myButton
'Determine Filter Field
myField = Application.WorksheetFunction.Match(ButtonName, DataRange.Rows(1), 0)
'Filter Data
DataRange.AutoFilter _
Field:=myField, _
Criteria1:=SearchString, _
Operator:=xlAnd
'Clear Search Field
sht.Shapes("UserSearch").TextFrame.Characters.Text = "" 'Control Form
'sht.OLEObjects("UserSearch").Object.Text = "" 'ActiveX Control
'sht.Range("A1").Value = "" 'Cell Input
Exit Sub
'ERROR HANDLERS
HeadingNotFound:
MsgBox "The column heading [" & ButtonName & "] was not found in cells " & DataRange.Rows(1).Address & ". " & _
vbNewLine & "Please check for possible typos.", vbCritical, "Header Name Not Found!"
End Sub
Thanks in advance
解决方案
推荐阅读
- c++ - 将 char* 推送到向量时出现问题,但在每次迭代后,它会将指向相同值缓冲区的指针添加到向量中
- c# - 如何在泛型 C# 类中引用结构的类型参数?
- scons - "TypeError: Tried to lookup Dir '...' as a File." in scons variant_dir build
- android - Option menu text color different in android pie 9.0
- laravel - How can I access docker database Remotly?
- vue.js - ...mapState() 中定义的计算属性没有设置器
- java - 春季会话 jdbc
- javascript - 当我在 Javascript 中创建元素时,输入和标签之间的差距与原始 DOM 元素不同
- ios - Control AVAudioRecorder from bluetooth commands
- swift - 如何修复:“49:CreateDataFile 失败”