2017-09-25 1 views
0

500 셀이있는 열에서 MS Excel의 각 셀을 100 자로 트리밍하고 싶습니다.VBA에서 문자열에서 텍스트를 제거하는 방법 Microsoft Excel

첫 번째 셀부터 문자열 길이가 100 자인지 확인하십시오. 단어가 100 개 이상인 경우 셀에서 1 단어를 제거한 다음 100 개가 넘으면 다른 단어를 제거하고 문자열이 100 개 미만이 될 때까지 다시 확인하십시오. 그런 다음 이전 셀을 대체하는 동일한 셀에 100 개 미만의 문자열을 붙여 넣으십시오 100 개 이상의 문자열.

그런 다음 다른 셀로 이동하고 이전 단계를 완료하십시오. 제거 할

단어가 배열 여기

에있는 것은 지금까지

Sub RemoveWords() 
Dim i As Long 
Dim cellValue As String 
Dim stringLenth As Long 
Dim myString As String 
Dim words() As Variant 
words = Array("Many", "specific ", "Huawei", "tend", "Motorolla", "Apple") 

myString = "Biggest problem with many phone reviews from non-tech specific publications is that its reviewers tend to judge the phones in a vacuum" 
For i = 1 To 13 
cellValue = Cells(i, 4).Value 
     If Not IsEmpty(cellValue) Then 
      stringLength = Len(cellValue) 
      ' test if string is less than 100 
      If stringLength > 100 Then 
       Call replaceWords(cellValue, stringLength, words) 
      Else 
       ' MsgBox "less than 100 " 
      End If 
     End If   
    Next i 

End Sub 

Public Sub replaceWords(cellValue, stringLength, words) 
    Dim wordToRemove As Variant 
    Dim i As Long 
    Dim endString As String 
    Dim cellPosition As Variant 

    i = 0 

    If stringLength > 100 Then 

     For Each wordToRemove In words 
      If InStr(1, UCase(cellValue), UCase(wordToRemove)) = 1 Then 
      MsgBox "worked word found" & " -- " & cellValue & " -- " & key 
      Else 
      Debug.Print "Nothing worked" & " -- " & cellValue & " -- " & key 

      End If 
     Next wordToRemove 
    Else 
    MsgBox "less than 100 " 
    End If 

End Sub 
+0

그냥 또 다른 방법은, 당신이 그것을 좋아하는 경우에 ... 내가 코드를 가지고있는이 분리 내 코드입니다 문자열을 단어로 분리하고 (', 공백 문자로 구분), 분리 된 단어의 배열을 반환합니다. 이제는 문자열 길이가 100보다 길면 모든 단어의 배열을 만들 수 있고 그런 다음 계속 연결할 수 있습니다 길이의 수를 유지하면서 .. –

+3

퀘스트를 잊어 버린 것 같습니다. 이온! – SJR

+3

당신에게 차이가 있는지 모르겠지만 Motorola가 아닌 Motorola입니다. – Moacir

답변

0
Sub NonKeyWords() 
' remove non key words 
' 

Dim i As Long 
Dim cellValue As String 
Dim stringLenth As Long 
Dim wordToRemove As Variant 
Dim words() As Variant 
Dim item As Variant 

' assign non-key words to array 
words = words = Array("Many", "specific ", "Huawei", "tend", "Motorolla", "Apple") 

' loop though all cells in column D 
For i = 2 To 2000 
cellValue = Cells(i, 4).Value 
    If Not IsEmpty(cellValue) Then 
     ' test if string is less than 100 
     If Len(cellValue) > 100 Then 
     'Debug.Print "BEFORE REMOVING: " & cellValue 
      Call replaceWords(cellValue, words, i) 
     Else 
      ' MsgBox "less than 100" 
     End If 
    End If 
Next i 

End Sub 

Public Sub replaceWords(cellValue, words, i) 

If Len(cellValue) > 100 Then 

     For Each wordsToDelete In words 
      If Len(cellValue) > 100 Then 
      cellValue = Replace(cellValue, wordsToDelete, "") 
      'Debug.Print cellValue 
      Debug.Print "String length after removal = " & Len(cellValue) 
      Debug.Print "remove another word................" 
      'cells(i, 4).ClearContents 
      Cells(i, 4).Value = cellValue 
      Else 
      'exit 
      End If 
     Next 
Else 
    Debug.Print "SAVE: " & cellValue 

End If 

End Sub 
관련 문제