Vba 对于工作簿中的每个工作表
有没有想过为什么下面的代码不会在工作表中循环 我试图根据图纸名称在每张图纸中设置一列。它卡在活动工作表上,如果ws.Name“Default”,则忽略Vba 对于工作簿中的每个工作表,vba,excel,Vba,Excel,有没有想过为什么下面的代码不会在工作表中循环 我试图根据图纸名称在每张图纸中设置一列。它卡在活动工作表上,如果ws.Name“Default”,则忽略。这是作为一个模块构建的: Sub LangID_update() Dim wb As Workbook: Set wb = ThisWorkbook Dim ws As Worksheet Dim LastCol As Integer Dim LastRow As Integer Dim rng As Range Application.Sc
。这是作为一个模块构建的:
Sub LangID_update()
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet
Dim LastCol As Integer
Dim LastRow As Integer
Dim rng As Range
Application.ScreenUpdating = False
For Each ws In wb.Worksheets
If ws.Name <> "Default" Then
LastCol = ws.Cells(1, Columns.count).End(xlToLeft).Column
LastRow = ws.Cells(Rows.count, 1).End(xlUp).Row
Set rng = Range(Cells(2, LastCol + 1), Cells(LastRow, LastCol + 1))
With rng
For Each c In Range(Cells(2, LastCol + 1), Cells(LastRow, LastCol + 1))
If ws.Name = "ARGS" Then c.Value = "ESP"
If ws.Name = "AUTS" Then c.Value = "GR"
If ws.Name = "DEUS" Then c.Value = "GR"
Next c
End With
End If
Next
Application.ScreenUpdating = True
Set wb = Nothing
Set ws = Nothing
Set rng = Nothing
End Sub
子语言更新()
将wb设置为工作簿:设置wb=ThisWorkbook
将ws设置为工作表
将LastCol设置为整数
将最后一行设置为整数
变暗rng As范围
Application.ScreenUpdating=False
对于wb.工作表中的每个ws
如果ws.Name为“Default”,那么
LastCol=ws.Cells(1,Columns.count).End(xlToLeft).Column
LastRow=ws.Cells(Rows.count,1).End(xlUp).Row
设置rng=范围(单元格(2,LastCol+1),单元格(LastRow,LastCol+1))
带rng
对于范围内的每个c(单元格(2,LastCol+1),单元格(LastRow,LastCol+1))
如果ws.Name=“ARGS”,则c.Value=“ESP”
如果ws.Name=“AUTS”,则c.Value=“GR”
如果ws.Name=“DEUS”,那么c.Value=“GR”
下一个c
以
如果结束
下一个
Application.ScreenUpdating=True
设置wb=Nothing
设置ws=Nothing
设置rng=无
端接头
您使用的许多对象引用都是不合格的,因此引用的是活动工作表。在每个对象的开头添加ws.
对象限定。您可能希望明确声明您的rng
范围的工作表。(我假设它将在ws
中)
试试这个:
Sub LangID_update()
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet
Dim LastCol As Integer
Dim LastRow As Integer
Dim rng As Range
Application.ScreenUpdating = False
For Each ws In wb.Worksheets
If ws.Name <> "Default" Then
LastCol = ws.Cells(1, Columns.count).End(xlToLeft).Column
LastRow = ws.Cells(Rows.count, 1).End(xlUp).Row
Set rng = ws.Range(ws.Cells(2, LastCol + 1), ws.Cells(LastRow, LastCol + 1))
With rng
For Each c In rng
If ws.Name = "ARGS" Then c.Value = "ESP"
If ws.Name = "AUTS" Then c.Value = "GR"
If ws.Name = "DEUS" Then c.Value = "GR"
Next c
End With
End If
Next ws
Application.ScreenUpdating = True
Set wb = Nothing
Set ws = Nothing
Set rng = Nothing
End Sub
子语言更新()
将wb设置为工作簿:设置wb=ThisWorkbook
将ws设置为工作表
将LastCol设置为整数
将最后一行设置为整数
变暗rng As范围
Application.ScreenUpdating=False
对于wb.工作表中的每个ws
如果ws.Name为“Default”,那么
LastCol=ws.Cells(1,Columns.count).End(xlToLeft).Column
LastRow=ws.Cells(Rows.count,1).End(xlUp).Row
设置rng=ws.Range(ws.Cells(2,LastCol+1),ws.Cells(LastRow,LastCol+1))
带rng
对于rng中的每个c
如果ws.Name=“ARGS”,则c.Value=“ESP”
如果ws.Name=“AUTS”,则c.Value=“GR”
如果ws.Name=“DEUS”,那么c.Value=“GR”
下一个c
以
如果结束
下一个ws
Application.ScreenUpdating=True
设置wb=Nothing
设置ws=Nothing
设置rng=无
端接头
编辑:我也很确定您不需要使用带有rng的
,因为您使用for
循环遍历它,并且不必在with
语句中使用rng。这是因为您在访问范围时没有引用ws变量
Set rng = Range(ws.Cells(2, LastCol + 1), ws.Cells(LastRow, LastCol + 1))
For Each c In rng
注意:当您不为范围和单元格添加工作表限定时,它们将取自活动工作表。这就是为什么你的代码被粘贴在ActiveSheet上的原因 到目前为止,答案很准确:问题是在for块中缺少对rng
的限定。尽管如此,快速增加一行将解决问题:
如果ws.Name为“Default”,则
ws.激活
激活工作表将使代码的其余部分正常运行,因为您正在/正在考虑活动工作表,而您刚刚使该工作表亮起。我认为您的每行都有错误,但如果不了解后面的内容,很难说
For Each ws In wb.Worksheets
If ws.Name <> "Default" Then
LastCol = ws.Cells(1, Columns.count).End(xlToLeft).Column
LastRow = ws.Cells(Rows.count, 1).End(xlUp).Row
'next line is useless
'Set rng = Range(Cells(2, LastCol + 1), Cells(LastRow, LastCol + 1))
With rng
' qualify Range and Cells
For Each c In ws.Range(ws.Cells(2, LastCol + 1), ws.Cells(LastRow, LastCol + 1))
If ws.Name = "ARGS" Then c.Value = "ESP"
If ws.Name = "AUTS" Then c.Value = "GR"
If ws.Name = "DEUS" Then c.Value = "GR"
Next c
End With
End If
Next
wb.工作表中每个ws的
如果ws.Name为“Default”,那么
LastCol=ws.Cells(1,Columns.count).End(xlToLeft).Column
LastRow=ws.Cells(Rows.count,1).End(xlUp).Row
“下一行没用
'设置rng=范围(单元格(2,LastCol+1),单元格(LastRow,LastCol+1))
带rng
“限定范围和单元格
对于ws.Range中的每个c(ws.Cells(2,LastCol+1),ws.Cells(LastRow,LastCol+1))
如果ws.Name=“ARGS”,则c.Value=“ESP”
如果ws.Name=“AUTS”,则c.Value=“GR”
如果ws.Name=“DEUS”,那么c.Value=“GR”
下一个c
以
如果结束
下一个