swalowbird
Thành viên mới

- Tham gia
- 22/5/16
- Bài viết
- 49
- Được thích
- 1
Chào các bác !
Em làm đoạn code sau để phân giải đoạn mã ký tự có trong file đính kèm , nhưng hiện nay em đang bị tắc ở chỗ k =2 , code tuy chạy nhưng không ra kết quả, nhờ các bác sửa code giúp em với , thanks
Sub kkkkkk()
Dim MyString, TargetString As String, Lenth1 As Integer
Dim Rng As Range
Set Rng = Selection
Dim k As Integer
Dim i As Integer
Dim j As Integer
Dim n As Integer
Dim v() As Integer
'dim a as
MyString = Rng
TargetString = "/"
Lenth1 = Len(MyString)
j = 0
For i = 1 To Lenth1
If Mid(MyString, i, 1) = TargetString Then
j = j + 1
End If
Next i
k = j
MsgBox k, , "Thong Bao"
ReDim v(n = 1 To k) As Integer
For i = 1 To Lenth1
If Mid(MyString, i, 1) = TargetString Then
v(n) = i
MsgBox v(n), , "Thong Bao"
End If
Next i
If k = 1 Then
Rng.EntireRow.copy
Rng.Offset(1, 0).EntireRow.insert Shift:=xlDown
Rng.Offset(1, 0) = Mid(MyString, 1, v(n) - 1)
Rng.EntireRow.copy
Rng.Offset(1, 0).EntireRow.insert Shift:=xlDown
Rng.Offset(1, 0) = Mid(MyString, 1, v(n) - (Lenth1 - v(n)) - 1) & Mid(MyString, v(n) + 1, Lenth1 - v(n))
End If
If k = 2 Then
If v(2) - v(1) - 1 = Lenth1 - v(2) Then
Rng.EntireRow.copy
Rng.Offset(1, 0).EntireRow.insert Shift:=xlDown
Rng.Offset(1, 0) = Mid(MyString, 1, v(1) - 1)
Rng.EntireRow.copy
Rng.Offset(1, 0).EntireRow.insert Shift:=xlDown
Rng.Offset(1, 0) = Mid(MyString, 1, v(1) - v(2) + v(1) + 1) & Mid(MyString, v(1) + 1, v(2) - v(1) - 1)
Rng.EntireRow.copy
Rng.Offset(1, 0).EntireRow.insert Shift:=xlDown
Rng.Offset(1, 0) = Mid(MyString, v(2) + 1, Lenth1 - v(2))
End If
End If
End Sub
Em làm đoạn code sau để phân giải đoạn mã ký tự có trong file đính kèm , nhưng hiện nay em đang bị tắc ở chỗ k =2 , code tuy chạy nhưng không ra kết quả, nhờ các bác sửa code giúp em với , thanks

Sub kkkkkk()
Dim MyString, TargetString As String, Lenth1 As Integer
Dim Rng As Range
Set Rng = Selection
Dim k As Integer
Dim i As Integer
Dim j As Integer
Dim n As Integer
Dim v() As Integer
'dim a as
MyString = Rng
TargetString = "/"
Lenth1 = Len(MyString)
j = 0
For i = 1 To Lenth1
If Mid(MyString, i, 1) = TargetString Then
j = j + 1
End If
Next i
k = j
MsgBox k, , "Thong Bao"
ReDim v(n = 1 To k) As Integer
For i = 1 To Lenth1
If Mid(MyString, i, 1) = TargetString Then
v(n) = i
MsgBox v(n), , "Thong Bao"
End If
Next i
If k = 1 Then
Rng.EntireRow.copy
Rng.Offset(1, 0).EntireRow.insert Shift:=xlDown
Rng.Offset(1, 0) = Mid(MyString, 1, v(n) - 1)
Rng.EntireRow.copy
Rng.Offset(1, 0).EntireRow.insert Shift:=xlDown
Rng.Offset(1, 0) = Mid(MyString, 1, v(n) - (Lenth1 - v(n)) - 1) & Mid(MyString, v(n) + 1, Lenth1 - v(n))
End If
If k = 2 Then
If v(2) - v(1) - 1 = Lenth1 - v(2) Then
Rng.EntireRow.copy
Rng.Offset(1, 0).EntireRow.insert Shift:=xlDown
Rng.Offset(1, 0) = Mid(MyString, 1, v(1) - 1)
Rng.EntireRow.copy
Rng.Offset(1, 0).EntireRow.insert Shift:=xlDown
Rng.Offset(1, 0) = Mid(MyString, 1, v(1) - v(2) + v(1) + 1) & Mid(MyString, v(1) + 1, v(2) - v(1) - 1)
Rng.EntireRow.copy
Rng.Offset(1, 0).EntireRow.insert Shift:=xlDown
Rng.Offset(1, 0) = Mid(MyString, v(2) + 1, Lenth1 - v(2))
End If
End If
End Sub