excel - Excel vba循环遍历包含第一个字母M的所有工作表
问题描述
我一直在尝试遍历某个表号,我有 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
.
推荐阅读
- php - 在 WooCommerce 简单产品的添加到购物车按钮中显示可用性
- c# - 页面重新加载时图像不断消失
- javascript - 我找不到在某个 PDO 实例中提交此复选框循环结果的方法
- pandas - 如何使用 Dask 正确迭代 for 循环?
- python-3.x - Kinesis GetRecords API 返回空 Records[] 数组
- ios - 根据 UIProgressView 状态同步描边填充动画?
- c# - ASP.NET MVC - 如何从 asp.net 站点中删除登录 Cookie
- c# - c# ASP.Net Core 和 Linq:聚合函数 MIN 和 MAX
- javascript - 将框架放在另一个框架的顶部
- rest - 如何在哈希表中仅存储 REST 响应的一部分?