VBA Worksheet Select Dependant On Cell Value

a 夏天 提交于 2019-12-12 00:27:08

问题


I am pretty new to VBA, so please excuse me if this is a simple question! I have created a template report (titled "Client_Name") that I want populated for each client - in essence all this involves is selecting the client in the PivotTable's report filter and copying the PivotTable's data values to the template. In order to automate this, I have created the following code to duplicate the template and rename it for the client currently selected in the PivotTable:

Sheet_Name_To_Create = Sheets("Pivot").Range("B1").Value
Sheets("Client_Name").Select
Sheets("Client_Name").Copy After:=Sheets(Sheets.Count)
Sheets(ActiveSheet.Name).Name = Sheet_Name_To_Create

My next step would be to go back to the PivotTable ("Pivot" worksheet) and copy the data to the newly created client worksheet. However, the worksheet's name will obviously change differ for each client. Is there a way to reference the VBA code to select the worksheet whose name is the same as the client currently shown in the PivotTables's report filter?


回答1:


Sheets(ActiveSheet.Name) is an inconvenient way of saying ActiveSheet.

Just capture ActiveSheet in a variable after copying.

Dim CopiedSheet as Worksheet
Set CopiedSheet = ActiveSheet

You don't need sheet's name when you have the sheet itself.

Recommended reading: How to avoid using Select in Excel VBA macros



来源:https://stackoverflow.com/questions/20956358/vba-worksheet-select-dependant-on-cell-value

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