vba 如何为列中的每个单元格执行一个函数并遍历所有工作簿?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17243947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:56:14 来源:igfitidea点击:
How to execute a function for each cell in a column and loop through all workbooks?
提问by user2510323
Here's what I have so far:
这是我到目前为止所拥有的:
Sub TrimColumnD()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Dim c As Range
For Each c In ActiveSheet.UsedRange.Columns("D").Cells
c.Value = WorksheetFunction.Trim(c.Value)
Next c
Next ws
End Sub
The trim function only works on the cells in the first worksheet.
修剪功能仅适用于第一个工作表中的单元格。
回答by Kazimierz Jawor
Please change this line:
请更改此行:
For Each c In ActiveSheet.UsedRange.Columns("D").Cells
into this one:
进入这个:
For Each c In ws.UsedRange.Columns("D").Cells
In your code internal loop refers to activesheet while it should refer to ws variable
representing sheet.
在您的代码中,内部循环指的是 activesheet,而它应该指的是ws variable
表示工作表。