2016-06-27 12 views
2

シート上のテキストを ""で置き換えるために使用しようとしている次のコードがあります。コードを実行すると、エラーは発生しませんが、何も変わらないので、バックエンドで実行されている必要がありますが、適切な指示はありません。それがなぜそうであるかのための任意のアイデア?Excel VBA Replace not executing

Sub Replacetext 

Dim sd As Worksheet 

    Set sd = Sheets("StatementData") 

Dim sdlastrowv As Long 

    sdlastrowv = sd.Cells(sd.Rows.Count, "A").End(xlUp).Row 

Dim sdalldata As Range, sdcel As Range, sdcelv As String 

Set sdalldata = sd.Range("A1", "K" & sdlastrowv) 

sd.Activate 

    For Each sdcel In sdalldata 
     If InStr(1, sdcelv, "Investor Ref :") Then 
      sdcel.Value.Replace What:="Investor Ref :", Replacement:="" 
     End If 
    Next sdcel 

End Sub 
+1

私が代わりに ''そしてsdalldata.Replace "投資参考:" セル '設定しsdalldata = sd.Range( "A1"、 "K" &sdlastrowv)によって細胞を行くの置き換えのために行くだろう "" '。注意:余分な空白がある場合、それを置き換えることはありません。これは本当に '' Investor Ref: ''は誤字ではなく、代わりに '' Investor Ref: "'でしょうか? – Sgdva

+0

この**は、値がセルに存在する場合にエラーを発生させます**。おそらく特別な空白のためにエラーが発生することはありません。 –

答えて

2

コードは次のようになります。

Sub Replacetext() 

    Dim sd As Worksheet 
    Set sd = Sheets("StatementData") 

    Dim sdlastrowv As Long 
    sdlastrowv = sd.Cells(sd.Rows.Count, "A").End(xlUp).Row 

    Dim sdalldata As Range, sdcel As Range, sdcelv As String 
    Set sdalldata = sd.Range("A1", "K" & sdlastrowv) 

    sd.Activate 

    For Each sdcel In sdalldata 
     If InStr(1, sdcel, "Investor Ref :") Then 
      sdcel.Replace What:="Investor Ref :", Replacement:="" 
     End If 
    Next sdcel 

End Sub 

sdcel.Replace ...sdcelsdcel.Value.Replace ...sdcelvを変更しました。

2

ここにはマイナーな変更が加えられた作業用サブがあります(コメントを見てください)。私は可能な限り元のコードに固執して、自分自身を見つけることができました。また、私はこのような変数の命名と.Valueの代わり.Value2を使用するなど、いくつかの良いコーディングプラクティスを実装:

Option Explicit 
Option Compare Text 

Sub ReplaceTextCellByCell() 

Dim shtData As Worksheet 
Dim lngLastRow As Long 
Dim rngAllData As Range, rngCell As Range 

Set shtData = ThisWorkbook.Worksheets("StatementData") 
lngLastRow = shtData.Cells(shtData.Rows.Count, "A").End(xlUp).Row 

'I exchanged the comma for a colon. The comma would mean 
' that you are referring to two cells only. The cell 
' A1 and the cell K20 (or whatever the last row is) 
' The colon instead means that you want every cell 
' between these two to be included in the range 
Set rngAllData = shtData.Range("A1:K" & lngLastRow) 

'The following line is not necessary. Therefore I commented it out. 
'shtData.Activate 

For Each rngCell In rngAllData 
    If InStr(1, rngCell.Value2, "Investor Ref :") Then 
     rngCell.Value = Replace(rngCell.Value2, "Investor Ref :", "") 
    End If 
Next rngCell 

End Sub 

次のサブ速度の点で第一副にわたってわずかな改善です。さらに、最終行は列Aに基づいて決定されるのではなく、最後の全体行に決定されるようになりました。必要に応じてこれをもう一度変更することができます。

Option Explicit 
Option Compare Text 

Sub ReplaceTextWithFind() 

Dim shtData As Worksheet 
Dim lngLastRow As Long 
Dim rngAllData As Range, rngCell As Range, strFirstAddress As String 

Set shtData = ThisWorkbook.Worksheets("StatementData") 
lngLastRow = shtData.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row 
Set rngAllData = shtData.Range("A1:K" & lngLastRow) 

'Based on the example provided by Microsoft here 
'https://msdn.microsoft.com/en-us/library/office/ff839746.aspx 

With rngAllData 
    Set rngCell = .Find(What:="Investor Ref :", LookIn:=xlValues) 
    If Not rngCell Is Nothing Then 
     strFirstAddress = rngCell.Address 
     Do 
      rngCell.Value2 = Replace(rngCell.Value2, "Investor Ref :", "") 
      Set rngCell = .FindNext(rngCell) 
      If rngCell Is Nothing Then Exit Sub 
      If rngCell.Address = strFirstAddress Then Exit Sub 
     Loop 
    End If 
End With 

End Sub