运行带有排除项的宏多张工作表
Posted
技术标签:
【中文标题】运行带有排除项的宏多张工作表【英文标题】:run macro multiple sheets with exclusions 【发布时间】:2013-10-23 02:14:39 【问题描述】:我正在尝试跨工作簿中的多个数据表运行此宏,但我无法让代码在工作簿中正常运行。如果我在单个工作表上运行宏,它工作正常,但我现在尝试运行所有工作表并排除“数据”和“更新”工作表并且没有任何运气,如果有任何关于如何操作的建议,代码如下正确运行所有工作表?谢谢
Sub UpdatePrices()
Dim ws As Worksheet, Ldate As String, DateRng As Range
Set DateRng = Range("A3") 'date range is last date
Ldate = DateRng.Value 'defines ldate as most recent date
For Each ws In ActiveWorkbook.Worksheets
'Inserts a new row with containing today's Date and exclude sheets
If Ldate <> Date And ws.Name <> "DATA" Or ws.Name <> "UPDATE" Then
DateRng.EntireRow.Insert
DateRng.Offset(-1, 0) = "=TODAY()"
Else
End If
Next ws
End sub
【问题讨论】:
【参考方案1】:试试下面的代码
Sub UpdatePrices()
Dim ws As Worksheet, Ldate As String, DateRng As Range
Set DateRng = Sheets("Sheet1").Range("A3") 'date range is last date
Ldate = DateRng.Value 'defines ldate as most recent date
For Each ws In ThisWorkbook.Worksheets
ws.Select
'Inserts a new row with containing today's Date and exclude sheets
If Ldate <> Date And UCase(ws.Name) <> "DATA" And UCase(ws.Name) <> "UPDATE" Then
ws.Rows(DateRng.Row).EntireRow.Insert
ws.Cells(DateRng.Row, DateRng.Column).Offset(-1, 0) = "=TODAY()"
End If
Next
End Sub
【讨论】:
以上是关于运行带有排除项的宏多张工作表的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Julia 创建具有多张工作表的 excel 文件?
Python:在多张工作表上将 Pandas DataFrame 写入 Excel 的最快方法