VBA Excel:如何为列中的每个单元执行函数,并循环遍历所有工作簿?

时间:2021-02-22 12:13:31

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 works only on the cells in the first worksheet, but that's it. Any suggestions?

修剪功能只对第一个工作表中的单元格有效,但仅此而已。有什么建议吗?

Thanks in advance

谢谢提前

1 个解决方案

#1


13  

you need to 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变量。

#1


13  

you need to 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变量。