Can Excel vba invoke an autofilter when a hyperlink is clicked to open new workbook?

妖精的绣舞 提交于 2019-12-12 06:38:37

问题


Fairly new to programming and I want to know if it's possible to create an autofilter on a workbook that is opened by a hyperlink on a separate workbook. I'll explain: I have code to compile a summary of information on one spreadsheet, workbook1. The information resides in workbook2. The user is working in workbook1 and inputs the record numbers they are searching. When the macro runs all the associated information from each record number gets imported from workbook2 into workbook1. I currently create a hyperlink to open workbook2 (in case user needs further details) and I would like to have workbook2 open up with an autofilter based on the record numbers when the hyperlink is clicked. Is this possible? I've read and seen code where the autofilter is invoked through a hyperlink from sheet to sheet in the same workbook, but not sure I comprehend vba code enough to apply it on separate workbooks. Thanks in advance.


回答1:


Thanks @JMcD, your post helped me figure things out. I used your code, modified it to fit my needs, and inserted in module2. I use another module to create hyperlink. I then put a _followhyperlink private sub in workbook1's sheet1 that calls module2. Now if I click the hyperlink, workbook2 opens up, applies the autofilter based on the value in A1. I will post the code here once I figure out how to make it look pretty as your post above. Thanks again.




回答2:


Set workbook 2 as a variable in your workbook 1 code module, then call your methods.

dim wb as workbook
dim i as integer
set wb = workbooks.open("C:\dogstuff\sticks\thebeststicks.xlsx")
i = thisworkbook.worksheets("throwingstuff").cells(5,5).value
wb.worksheets("rank").range(cells(2,1),cells(2,10)).autofilter
wb.worksheets("rank").range(cells(2,1),cells(2,10)).autofilter field:=1, criteria1:=I

Hope that helps.



来源:https://stackoverflow.com/questions/36706709/can-excel-vba-invoke-an-autofilter-when-a-hyperlink-is-clicked-to-open-new-workb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!