Reference excel worksheet by name?

后端 未结 3 924
说谎
说谎 2021-02-02 11:12

I have the name of a worksheet stored as a string in a variable. How do I perform some operation on this worksheet?

I though I would do something like this:



        
3条回答
  •  难免孤独
    2021-02-02 11:30

    To expand on Ryan's answer, when you are declaring variables (using Dim) you can cheat a little bit by using the predictive text feature in the VBE, as in the image below. screenshot of predictive text in VBE

    If it shows up in that list, then you can assign an object of that type to a variable. So not just a Worksheet, as Ryan pointed out, but also a Chart, Range, Workbook, Series and on and on.

    You set that variable equal to the object you want to manipulate and then you can call methods, pass it to functions, etc, just like Ryan pointed out for this example. You might run into a couple snags when it comes to collections vs objects (Chart or Charts, Range or Ranges, etc) but with trial and error you'll get it for sure.

提交回复
热议问题