0

D列に「+」と「-」があるすべてのセルを削除したい。次のマクロを試してみましたが、うまくいくと思いましたが、何も起こりません。

 Sub doWhile4()

'Replace blank spaces with underscores in a Range of Cells, using VBA loops; or 'Remove     
 blank spaces in a Range of Cells, using VBA loops.

 Dim iCell As Range
 Dim textString As String
 Dim n As Integer

'iCell is a Cell in the specified Range which contains the textString
'textString is the text in a Cell in which blank spaces are to be replaced with
'underscores. n is the position of blank space(s) occurring in a textString

 For Each iCell In ActiveSheet.Range("D2:D34")
     textString = iCell
     n = InStr(textString, "+")

    'The VBA InStr function returns the position of the first occurrence of a string within
    'another string. Using this to determine the position of the first blank space in the 
    'textString.

     Do While n > 0
         textString = Left(textString, n - 1) & Right(textString, Len(textString) - n)

        'This line of code is to remove all blank spaces in the   
        'textString

         n = InStr(textString, "+")
     Loop
     iCell = textString
 Next

 End Sub

私は何を間違っていますか?

4

2 に答える 2

1

セルに + または - が含まれている場合は、これを試して値をクリアしてください。

Option Explicit

Sub doWhile4()

'Replace blank spaces with underscores in a Range of Cells, using VBA loops; or
'Remove blank spaces in a Range of Cells, using VBA loops.

 Dim iCell As Range
 Dim textString As String
 Dim n As Integer
 Dim hasPlus As Boolean
 Dim hasMinus As Boolean

'iCell is a Cell in the specified Range which contains the textString
'textString is the text in a Cell in which blank spaces are to be replaced with
'underscores. n is the position of blank space(s) occurring in a textString

 For Each iCell In ActiveSheet.Range("D2:D34")
     textString = iCell
     'reset our boolean variables for each iteration
     hasPlus = False
     hasMinus = False
     If InStr(textString, "+") > 0 Then hasPlus = True
     If InStr(textString, "-") > 0 Then hasMinus = True

    'The VBA InStr function returns the position of the first occurrence of a string within
    'another string. Using this to determine the position of the first blank space in the
    'textString.

     If hasPlus Or hasMinus Then iCell.Value = vbNullString

 Next

 End Sub
于 2013-03-13T01:54:31.640 に答える