我一直在尝试循环遍历某个工作表编号,我有 20 张全部以 M 开头的工作表,如 M1、M2 等等,直到 M20,我的问题是我正在循环和写入,但不是只在M张纸上写,我在所有纸上写。
Sub CountWSNames()
Dim I As Long
Dim xCount As Integer
For I = 1 To ActiveWorkbook.Sheets.Count
If Mid(Sheets(I).Name, 1, 1) = "M" Then xCount = xCount + 1
ThisWorkbook.Worksheets(I).Range("A50") = "V" 'This line must write only to M sheets
Next
MsgBox "There are " & CStr(xCount) & " sheets that start with 'M'", vbOKOnly, "KuTools for Excel"
End Sub
与我的类似的小代码。
ThisWorkbook.Worksheets(I).Range("A50") = "V"
此行必须仅影响以 M 开头的工作表上的单元格(“A50”)。
最佳答案
您可以分别使用。
For each sht in ActiveWorkbook.Sheets
If Mid(sht.Name, 1, 1) = "M" Then
xCount = xCount +1
sht.Range("A50") = "V" 'This line must write only to M sheets
End if
Next
您需要将这两种语法包装在 if
内。
关于Excel VBA循环遍历包含第一个字母M的所有工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53203758/