2016-06-27 1 views
2

시트의 텍스트를 ""로 바꾸려면 다음 코드를 사용하고 있습니다. 코드를 실행할 때 오류는 발생하지 않지만 변경 사항은 없으므로 백엔드에서 실행 중이어야하지만 올바른 명령은 실행되지 않아야합니다. 그 이유에 대한 아이디어가 있습니까?Excel VBA 바꾸기 실행하지 않음

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 

다음 서브 속도의 측면에서 제 1 서브에 비해 약간의 개선이다. 또한 마지막 행은 더 이상 열 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