giaphuc2210
Thành viên mới

- Tham gia
- 29/12/14
- Bài viết
- 11
- Được thích
- 0
Option Explicit
Private Sub CommandButton1_Click()
Dim MyRow&
MyRow = 5
Do Until Cells(MyRow, 2) = vbNullString
Cells(MyRow, 3) = Trim2(convertABC(Cells(MyRow, 2)), " ")
MyRow = MyRow + 1
Loop
End Sub
Private Function convertABC(s$)
Dim Ctrl As Control, Str$, StrFind$
Str = s
For Each Ctrl In UserForm1.Controls
StrFind = Trim(Ctrl.Object.Text)
If StrFind <> VbNullString then
Str = Replace(Str, StrFind, StrFind & " ")
End if
Next
convertABC = Str
End Function
Public Function Trim2$(s$, StrTrim$)
Dim Str$
Str = s
Do Until InStr(1, Str, StrTrim & StrTrim, vbTextCompare) = 0
Str = Replace(Str, StrTrim & StrTrim, StrTrim)
Loop
Trim2$ = Trim(Str)
End Function
Option Explicit
Private Sub CommandButton1_Click()
Dim a$(), aFind$(), ubFind&, i&, MyRow&
MyRow = 5
ubFind = -1
Do Until Cells(MyRow, 2) = vbNullString
a = Split(Cells(MyRow, 2), " ")
For i = 0 To UBound(a)
If Not isInAFind(a(i), aFind(), ubFind) Then
ubFind = ubFind + 1
ReDim Preserve aFind(ubFind)
aFind(ubFind) = a(i)
End If
Next
MyRow = MyRow + 1
Loop
MyRow = 5
Do Until Cells(MyRow, 2) = vbNullString
Cells(MyRow, 3) = Trim2(convertXYZ(Cells(MyRow, 2), aFind(), ubFind), " ")
MyRow = MyRow + 1
Loop
End Sub
Private Function isInAFind(s$, a$(), ub&) As Boolean
Dim i&
For i = 0 To ub
If s = a(i) Then
isInAFind = True
Exit For
End If
Next
End Function
Private Function convertXYZ(s$, a$(), ub&)
Dim i&, Str$, StrFind$
Str = s
For i = 0 To ub
StrFind = Trim(a(i))
Str = Replace(Str, StrFind, StrFind & " ")
Next
convertXYZ = Str
End Function
Public Function Trim2$(s$, StrTrim$)
Dim Str$
Str = s
Do Until InStr(1, Str, StrTrim & StrTrim, vbTextCompare) = 0
Str = Replace(Str, StrTrim & StrTrim, StrTrim)
Loop
Trim2$ = Trim(Str)
End Function