【问题标题】:Remove all spaces in data skipping over headerRemove all spaces in data skipping over header
【发布时间】:2022-12-01 22:19:48
【问题描述】:

I want to write a function that will find all spaces and remove them from my data, however, I need it to skip the first line in the excel sheet as that is the header line that needs to keep its spaces. For example, I have a column with the header Power with the column index of 5, I want the function to search from column 5 row 2 to the last row with data in column 5 for spaces. If there are spaces that exist in the data, I want them to be removed and replaces with no space.

This is what I have tried so far, however, I cannot get it to remove the spaces when I test it.

Sub removespace()
    Range(2,5, cells(rows.Count,5).End(xlUp)).Select
    Selection.Replace What:=" ", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByColumns, MatchCase:=True, SearchFormat:=False, _
    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
End Sub

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    Please, try:

        Range(cells(2,5), cells(rows.Count,5).End(xlUp).Replace What:=" ", Replacement:="", LookAt:=xlWhole, _
        SearchOrder:=xlByColumns, MatchCase:=True, SearchFormat:=False, _
        ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    

    Selecting only consumes Excel resources, not bringing any benefit...

    A better idea would be to fully qualify the used range. Now, it works on the active sheet. If you want running on a different than the active one, you should do it as I am saying...

    【讨论】:

    • It is telling me there is a syntax error is a comma or something missing?
    猜你喜欢
    • 2022-12-19
    • 2015-12-04
    • 2022-12-01
    • 2022-12-02
    • 2022-12-02
    • 2022-12-27
    • 2010-12-02
    • 2012-07-05
    • 2022-12-01
    相关资源
    最近更新 更多