Попробуйте
Sub test()
Dim vDB, vR()
Dim s As String, s2 As String
Dim sT As Integer, sE As Integer, co As Integer
Dim str As String, strResult As String
Dim i As Long, n As Long, c As Long, r As Long
Dim num As Long
Dim T1 As String, T2 As String
Dim strFn As String
s = vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf
s2 = "," & Format(0, "000")
vDB = Range("a1").CurrentRegion
n = UBound(vDB, 1)
sT = 1
For i = 1 To n
If vDB(i, 2) = "" Then
num = num + 1
c = c + 5
If num = 1 Then
sE = sT + 4
Else
sT = sE + 1
sE = sT + 9
End If
T1 = Format(TimeSerial(0, 0, sT), "hh:mm:ss")
T2 = Format(TimeSerial(0, 0, sE), "hh:mm:ss")
ReDim Preserve vR(1 To c)
vR(c - 4) = num
vR(c - 3) = T1 & s2 & "-->" & T2 & s2
vR(c - 2) = s
vR(c - 1) = vDB(i, 1)
vR(c) = s
Else
r = r + 1
If r = 1 Then
num = num + 1
c = c + 4
sT = sE + 1
sE = sT + 9
T1 = Format(TimeSerial(0, 0, sT), "hh:mm:ss")
T2 = Format(TimeSerial(0, 0, sE), "hh:mm:ss")
ReDim Preserve vR(1 To c)
vR(c - 3) = num
vR(c - 2) = T1 & s2 & "-->" & T2 & s2
vR(c - 1) = vDB(i, 1)
vR(c) = "High:" & vDB(i, 2) & " Low:" & vDB(i, 3) & " Close:" & vDB(i, 4) & vbCrLf
Else
c = c + 2
ReDim Preserve vR(1 To c)
vR(c - 1) = vDB(i, 1)
vR(c) = "High:" & vDB(i, 2) & " Low:" & vDB(i, 3) & " Close:" & vDB(i, 4) & vbCrLf
If r = 3 Then r = 0
End If
End If
Next i
strResult = Join(vR, vbCrLf)
Range("f1").Resize(UBound(vR)) = WorksheetFunction.Transpose(vR)
'@@ Save Text file
strFn = "Test1.srt"
strFn = ThisWorkbook.Path & "\" & strFn
TransToUTF strResult, strFn 'make srt file
End Sub
Sub TransToUTF(strTxt As String, strFile As String)
Dim objStream As Object
Set objStream = CreateObject("ADODB.Stream")
With objStream
.Charset = "utf-8"
.Open
.WriteText strTxt
.SaveToFile strFile, 2
.Close
End With
Set objStream = Nothing
End Sub
Вы получили удовлетворительные ответы от других, но я исправил свои ответы.Отображение результатов на листе займет много времени.Это также добавит много данных.Почему хорошо использовать массивы - это тема этого сайта.См. Это
Sub test()
Dim vDB, vR()
Dim s As String, s2 As String, s3 As String
Dim sT As Integer, sE As Integer, co As Integer
Dim str As String, strResult As String
Dim i As Long, n As Long, c As Long, r As Long
Dim num As Long
Dim T1 As String, T2 As String
Dim strFn As String
s = WorksheetFunction.Rept(Space(1) & vbCrLf, 4) & Space(1)
s2 = "," & Format(0, "000")
s3 = WorksheetFunction.Rept(Space(1) & vbCrLf, 4)
vDB = Range("a1").CurrentRegion
n = UBound(vDB, 1)
sT = 1
For i = 1 To n
If vDB(i, 2) = "" Then
num = num + 1
c = c + 5
If num = 1 Then
sE = sT + 4
Else
sT = sE + 1
sE = sT + 9
End If
T1 = Format(TimeSerial(0, 0, sT), "hh:mm:ss")
T2 = Format(TimeSerial(0, 0, sE), "hh:mm:ss")
ReDim Preserve vR(1 To c)
vR(c - 4) = num
vR(c - 3) = T1 & s2 & " --> " & T2 & s2
vR(c - 2) = s
vR(c - 1) = vDB(i, 1)
vR(c) = s3
Else
r = r + 1
If r = 1 Then
num = num + 1
c = c + 4
sT = sE + 1
sE = sT + 9
T1 = Format(TimeSerial(0, 0, sT), "hh:mm:ss")
T2 = Format(TimeSerial(0, 0, sE), "hh:mm:ss")
ReDim Preserve vR(1 To c)
vR(c - 3) = num
vR(c - 2) = T1 & s2 & " --> " & T2 & s2
vR(c - 1) = vDB(i, 1)
vR(c) = "High:" & vDB(i, 2) & " Low:" & vDB(i, 3) & " Close:" & vDB(i, 4) & vbCrLf & Space(1)
Else
c = c + 2
ReDim Preserve vR(1 To c)
vR(c - 1) = vDB(i, 1)
vR(c) = "High:" & vDB(i, 2) & " Low:" & vDB(i, 3) & " Close:" & vDB(i, 4) & vbCrLf & Space(1)
If r = 3 Then r = 0
End If
End If
Next i
strResult = Join(vR, vbCrLf)
'@@ This not need. This is just for reviewing the results of the code on the sheet.
'Range("f1").Resize(UBound(vR)) = WorksheetFunction.Transpose(vR)
'@@ Save Text file
strFn = "Test1.srt"
strFn = ThisWorkbook.Path & "\" & strFn
TransToUTF strResult, strFn 'make srt file
End Sub
Sub TransToUTF(strTxt As String, strFile As String)
Dim objStream As Object
Set objStream = CreateObject("ADODB.Stream")
With objStream
.Charset = "utf-8"
.Open
.WriteText strTxt
.SaveToFile strFile, 2
.Close
End With
Set objStream = Nothing
End Sub