0

中間スペース、改行、トリムを取り除くための次のコードがあります

しかし、うまくいきTrimません。その理由は何ですか?

Sub Replace()

  With Sheets("Input_Data").Range("A1:A6300")
  'With Sheets("Input_Limits").Range("A1:A6300")

    .Cells.Replace "  ", " ", xlPart, xlByRows, False
    .Cells.Replace vbLf, "", xlPart, xlByRows, False
    '.Cells.Trim

  End With

End Sub

それは与えます:

エラー - オブジェクトはこのプロパティ メソッドをサポートしていません

4

4 に答える 4

1

次のコードは、あなたが求めていることを実行します。アクティブ シートのすべてのセルにトリム操作を適用していることに注意してください。それがあなたが望むものでない場合は、明らかに編集できます...

Sub trimAll()
  Dim c As Range

  For Each c In ActiveSheet.UsedRange.Cells
    If Not (IsEmpty(c) Or IsError(c) Or IsFormula(c)) Then
      c.Value = Trim(c.Value)
    End If
  Next c

  With ActiveSheet.UsedRange.Cells
    .Cells.Replace "  ", " ", xlPart, xlByRows, False
    .Cells.Replace vbLf, "", xlPart, xlByRows, False
  End With

End Sub

Function IsFormula(c)
  ' use this to test for formulas - don't edit those or they become values!
  IsFormula = True
  On Error Resume Next
  If Left(c.Formula, 1) = "=" Then IsFormula = True Else IsFormula = False
End Function
于 2013-02-05T15:33:28.043 に答える
1

範囲では使用できませんTrimTrimドキュメントには次のように記載されています。

Trim( string )
必要な文字列引数は、有効な文字列式です。

また、Trim文字列内のスペースを削除しないという点で、タスクには適していません。これはドキュメントに記載されています(強調が追加されています):

先頭のスペース (LTrim)、末尾のスペース (RTrim)、または先頭と末尾の両方のスペース (Trim)を除いた、指定された文字列のコピーを含む Variant (String) を返します。

于 2013-02-05T01:53:51.633 に答える
0

Trim は範囲をサポートしていないため、次を追加できます。

Dim c as Cell
For each c in .Cells
  c.Value = Trim(c.Value)
Next c

免責事項: テストされていません。より完全な解決策については、他の回答を参照してください。

于 2013-02-05T03:35:56.600 に答える
-1
Sub DescTrim(Cx As Integer)  
    Dim Rx As Integer: Rx = 5 'Start at row 5  
    Dim STrimmed As String  
        For Rx = 5 To ActiveSheet.UsedRange.Rows.Count  
            STrimmed = Trim(Cells(Rx, Cx).Value)  
            While InStr(STrimmed, chr(32) & (chr(32))  
                STrimmed = Replace(STrimmed, Chr(32) & Chr(32), chr(32))  
            Wend  
            Cells(Rx, Cx).Value = STrimmed  
        Next  
End Sub  
于 2015-12-09T19:50:51.773 に答える