我试图找出编写宏的正确方法,该宏遍历12个具有特定名称的工作表(Jan,Feb,...,Dec)。我想也许每个功能都是一个不错的选择所以我尝试了以下内容:
dim crntSht as worksheet
set crntsht=("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
for each crntsht in worksheets
.
.
.
end for
这显然不起作用,因为我以错误的方式定义了crntsht。
有人可以建议每次循环播放所有12张的最佳方法,并跳过同一工作簿中的所有其他工作表吗?
由于
答案 0 :(得分:15)
Sub LoopThroughSheets()
Dim Months As Variant
Dim Month As Variant
Months = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", _
"Aug", "Sep", "Oct", "Nov", "Dec")
For Each Month In Months
'Code goes here.
Next Month
End Sub
答案 1 :(得分:3)
替代Siddharth的回答:
dim arrSht, i
arrSht = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", _
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
for i = lbound(arrSht) to ubound(arrSht)
with worksheets(arrSht(i))
'work with sheet
end with
next i
答案 2 :(得分:2)
使用Microsoft Excel MONTHNAME
函数返回一个字符串,该字符串表示给定1到12之间的数字的月份。
语法
MonthName(数字,[缩写])
abbreviate是可选的。此参数接受布尔值,TRUE或FALSE。如果此参数设置为TRUE,则表示月份名称缩写。如果此参数设置为FALSE,则月份名称不会缩写。
实施例
?MonthName(1,True)
会给你JAN
将此用于我们的利益
Sub Sample()
Dim ws As Worksheet
Dim i As Long
For i = 1 To 12
Set ws = ThisWorkbook.Sheets(MonthName(i, True))
With ws
'
'~~> Rest of the code
'
End With
Next i
End Sub
答案 3 :(得分:0)
我认为这可能更好
dim v as variant
for each v in thisworkbook.sheets
' do something
' msgbox v.name
' msgbox v.index
next