How to execute a function for each cell in a column and loop through all workbooks?

隐身守侯 提交于 2019-12-18 21:18:25

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/17243947/how-to-execute-a-function-for-each-cell-in-a-column-and-loop-through-all-workboo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!