VBA使用范围选择多张纸

VBA使用范围选择多张纸

本文介绍了VBA使用范围选择多张纸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在单元格C2到C5中有一些工作表名称,它们是动态的,我想使用VBA同时选择它们.

I would I have some sheet names in cells C2 to C5, which are to be dynamic and I would like to select them those at the same time using VBA

但是到目前为止,我发现的唯一方法与使用数组和硬编码"工作表名称有关,而是想使用Ranges

But the only way I have found so far relates to using arrays and "hard-coding" the sheet names in, and would like instead to use Ranges

这是我到目前为止尝试过的代码

here is the code I have tried so far

Sub ssheets()
    Worksheets(Array("Sheet2", "Sheet3","Sheet4","Sheet5")).Select
End Sub

理想情况下,我希望使用Range("C2:C5")的东西,这样我就可以动态选择相关的工作表,而不必在VBA代码中键入"Sheet2","Sheet3","Sheet4","Sheet5"等

I would ideally like something that uses Range("C2:C5") so that I can dynamically select the relevant sheets without having to type in "Sheet2", "Sheet3","Sheet4","Sheet5" etc into the VBA code

谢谢

推荐答案

工作表名称数组必须为包含一维数组的Variant类型. Range("C2:C5")返回一个二维数组.要将其用作工作表名称数组,必须对其进行转置.

The sheet names array has to be of type Variant containing a one dimensional array. The Range("C2:C5") returns a two dimensional array. To use this as sheet names array, you have to transpose it.

Sub ssheets()
    Dim oWS As Worksheet
    Dim aSheetnames As Variant
    Set oWS = Worksheets(1)
    aSheetnames = oWS.Range("C2:C5")
    aSheetnames = Application.WorksheetFunction.Transpose(aSheetnames)
    Worksheets(aSheetnames).Select
End Sub

这篇关于VBA使用范围选择多张纸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 12:59