Автор: MORB_id
Дата сообщения: 24.09.2007 12:58
		Можно ли как-нибудь оптимизировать данный макрос? 
 [more] 
 Attribute VB_Name = "Module1" 
 Public Function VerifyFile(FileName$) As Boolean 
   ' Проверка - существует ли указанный файл 
   On Error Resume Next 
   ' Файл открывается как входной, последовательный 
   Open FileName$ For Input As #1 
   If Err Then ' Ошибка при открытии - нет файла 
     VerifyFile = False 
   Else 
     VerifyFile = True: Close #1 
   End If 
 End Function 
  
 Sub dgs() 
 Dim Msg, Style, Title, Help, Ctxt, Response, MyString 
 Dim fn, nfn, TEMP(100), tst, lfn, lcfr As String 
 Dim t1xt 
 Dim TempPos(10), strF, i, j, k, nuklpos, pogrpos, aktpos, perpos, l, filenum, pr As Integer 
 Dim r As Range 
 Dim nuklvst As Boolean 
 strF = 0 
 TempPos(1) = 1 
 Msg = "Новый или продолжать? Да - новый. Нет - продолжить." 
 fn = Application.GetOpenFilename("RPT Files (*.rpt),*.rpt", , "Открытие документа *.rpt") 
 If fn = False Then 
 Help = "DEMO.HLP"    ' Define Help file 
 Ctxt = 1000 
 Response = MsgBox("Вы не выбрали файл", vbOKOnly, "Открытие файла", Help, Ctxt) 
 Exit Sub 
 End If 
 filenum = CInt(Mid(fn, InStrRev(fn, "\", , vbTextCompare) + 1, (InStrRev(fn, ".", , vbTextCompare)) - (InStrRev(fn, "\", , vbTextCompare) + 1))) 
 Style = vbYesNo 
 Help = "DEMO.HLP"    ' Define Help file 
 Ctxt = 1000 
 Response = MsgBox(Msg, Style, Title, Help, Ctxt) 
 If Response = vbYes Then    ' User chose Yes. 
 ' Perform some action. 
 Range("A1").Select 
 ActiveCell.FormulaR1C1 = "Номер" 
 Range("B1").Select 
 ActiveCell.FormulaR1C1 = "Время начала измерения" 
 Range("C1").Select 
 ActiveCell.FormulaR1C1 = "T изм" 
 Range("D1").Select 
 ActiveCell.FormulaR1C1 = "Среднее время" 
 Range("E1").Select 
 ActiveCell.FormulaR1C1 = "Час" 
 Rows("1:1").Select 
 Selection.Locked = False 
 Selection.FormulaHidden = False 
  
 Columns("D:E").Select 
 With Selection.Font 
 .Name = "Arial CYR" 
 .FontStyle = "обычный" 
 .Size = 10 
 .Strikethrough = False 
 .Superscript = False 
 .Subscript = False 
 .OutlineFont = False 
 .Shadow = False 
 .Underline = xlUnderlineStyleNone 
 .ColorIndex = 10 
 End With 
  
 Close #1 
 Open fn For Input Access Read Shared As #1 'открытие файла 
 Do While Not (EOF(1)) 'пока не конец файла (обращение к файлу по номеру) 
 Line Input #1, TEMP(strF + 1) 'Ввод данных в переменную TEMP без chr(10)&chr(13) 
 LTrim (RTrim(TEMP(strF + 1))) 
 strF = strF + 1 'Переменная для подсчета числа строк 
 Loop 'Окончание цикла для подсчета строк 
 Close #1 'Закрытие файла 
 Range("B3").Select 
 ActiveCell.FormulaR1C1 = Trim(Mid(TEMP(4), 23, 18)) 
  
 Columns("B").Select 
 ActiveCell.Name = "rrrrr" 
 Set r = Range("rrrrr") 
 i = CInt(Mid(CStr(r.Columns.End(xlDown).Address), InStrRev(CStr(r.Columns.End(xlDown).Address), "$", , vbTextCompare) + 1, 1)) 
  
 Range("A" + CStr(i + 1)).Select 
 ActiveCell.FormulaR1C1 = CStr(CInt(Mid(fn, InStrRev(fn, "\", , vbTextCompare) + 1, (InStrRev(fn, ".", , vbTextCompare)) - (InStrRev(fn, "\", , vbTextCompare) + 1)))) 
  
 Range("B" + CStr(i + 1)).Select 
 ActiveCell.FormulaR1C1 = Trim(Mid(TEMP(4), 23, 18)) 
 Selection.NumberFormat = "dd/mm/yy hh:mm;@" 
 Range("B" + CStr(i)).Select 
 Selection.NumberFormat = "dd/mm/yy hh:mm;@" 
 Columns("B:B").Select 
 Selection.NumberFormat = "dd/mm/yy hh:mm;@" 
 Columns("C:C").Select 
 Selection.NumberFormat = "0.00" 
 Columns("D:D").Select 
 Selection.NumberFormat = "dd/mm/yy hh:mm;@" 
 Columns("E:E").Select 
 Selection.NumberFormat = "0.00" 
  
  
 strF = strF - 1 
 Range("C" + CStr(i + 1)).Select 
 ActiveCell.FormulaR1C1 = CStr(Mid(TEMP(strF), InStrRev(TEMP(strF), ",", , vbTextCompare) + 1, InStrRev(TEMP(strF), ".", , vbTextCompare) + 2)) 
  
 Range("D" + CStr(i + 1)).Select 
 tst = "=" + CStr("B" + CStr(i + 1) + "+C" + CStr(i + 1) + "/3600/24/2") 
  
 ActiveCell.Formula = "=" + CStr("B" + CStr(i + 1) + "+C" + CStr(i + 1) + "/3600/24/2") 
 Range("E" + CStr(i + 1)).Select 
 tst = "=(D" + CStr(i + 1) + "-B$3)*24" 
 ActiveCell.Formula = "=(D" + CStr(i + 1) + "-B$3)*24" 
 Cells(1, 6).Value = Mid(TEMP(8), 1, InStr(1, TEMP(8), " ", vbTextCompare) - 1) 
  
 nuklpos = InStr(1, Trim(TEMP(6)), "Н", vbTextCompare) 
 aktpos = InStr(1, Trim(TEMP(6)), "А", vbTextCompare) + 1 
 pogrpos = InStr(aktpos, Trim(TEMP(6)), "П", vbTextCompare) 
 perpos = InStr(1, Trim(TEMP(6)), "П", vbTextCompare) - 5 
 k = 6 
 For j = 8 To 256 
 If Trim(TEMP(j)) = "" Then Exit For 
 If Trim(Mid(Trim(TEMP(j)), aktpos, 2)) <> "" Then 
  
 Range(Cells(i + 1, k), Cells(i + 1, k)).Select 
 ActiveCell.Formula = Trim(Mid(Trim(TEMP(j)), aktpos, pogrpos - aktpos)) 
 Range(Cells(1, k), Cells(1, k)).Select 
 ActiveCell.Formula = Mid(Trim(TEMP(j)), 1, InStr(1, Trim(TEMP(j)), " ", vbTextCompare) - 1) 
 If (Trim(Mid(Trim(TEMP(j)), perpos, aktpos - perpos)) <> "") Then 
 Range(Cells(2, k), Cells(2, k)).Select 
 ActiveCell.Formula = Trim(Mid(Trim(TEMP(j)), perpos, aktpos - perpos)) 
 End If 
 k = k + 1 
 End If 
 Next j 
 label1: 
  
 lensl = InStrRev(fn, "\", , vbTextCompare) + 1 
 lendot = InStr(InStrRev(fn, "\", , vbTextCompare), fn, ".", vbTextCompare) 
 fnum = CInt(Mid(fn, lensl, lendot - lensl)) + 1 
 zerolen = lendot - 1 - lensl - Len(CStr(fnum)) 
 'For j = 1 To zerolen 
 'nfn = nfn + "0" 
 'Next j 
 'nfn = nfn + CStr(fnum) 
 'nfn = nfn + ".RPT" 
 'nfn = Mid(fn, 1, InStrRev(fn, "\", , vbTextCompare) + 1) + nfn 
 'fn = nfn 
  
 filenum = CInt(Mid(fn, lensl, lendot - lensl)) + 1 
 lfn = lendot - lensl 
 'Range(Cells(i + 1, 1), Cells(i + 1, 1)).Select 
  
 lcfr = Len(CStr(filenum)) 
 lfn = lendot - lensl - Len(CStr(fnum)) 
 nfn = Mid(fn, 1, InStrRev(fn, "\", , vbTextCompare)) 
 If lfn <> 0 Then 
 For j = 1 To lfn 
 nfn = nfn + "0" 
 Next j 
 End If 
 nfn = nfn + CStr(filenum) + ".rpt" 
 fn = nfn 
  
 i = i + 1 
 FileName$ = fn 
  
 If (VerifyFile(FileName$) = False) Then GoTo label2 
 If (VerifyFile(FileName$)) Then 
 strF = 0 
 Open fn For Input Access Read Shared As #1 'открытие файла 
 Do While Not (EOF(1)) 'пока не конец файла (обращение к файлу по номеру) 
 Line Input #1, TEMP(strF + 1) 'Ввод данных в переменную TEMP без chr(10)&chr(13) 
 LTrim (RTrim(TEMP(strF + 1))) 
 strF = strF + 1 'Переменная для подсчета числа строк 
 Loop 'Окончание цикла для подсчета строк 
 Close #1 'Закрытие файла 
 nuklpos = InStr(1, Trim(TEMP(6)), "Н", vbTextCompare) 
 aktpos = InStr(1, Trim(TEMP(6)), "А", vbTextCompare) + 1 
 pogrpos = InStr(aktpos, Trim(TEMP(6)), "П", vbTextCompare) - (InStr(aktpos, Trim(TEMP(6)), "П", vbTextCompare) - InStr(aktpos, Trim(TEMP(6)), ",", vbTextCompare)) 
  
 Range("A" + CStr(i + 1)).Select 
 ActiveCell.FormulaR1C1 = CStr(CInt(Mid(fn, InStrRev(fn, "\", , vbTextCompare) + 1, (InStrRev(fn, ".", , vbTextCompare)) - (InStrRev(fn, "\", , vbTextCompare) + 1)))) 
 Range("B" + CStr(i + 1)).Select 
 ActiveCell.FormulaR1C1 = Trim(Mid(TEMP(4), 23, 18)) 
 Selection.NumberFormat = "dd/mm/yy hh:mm;@" 
 strF = strF - 1 
 Range("C" + CStr(i + 1)).Select 
 ActiveCell.FormulaR1C1 = CStr(Mid(TEMP(strF), InStrRev(TEMP(strF), ",", , vbTextCompare) + 1, InStrRev(TEMP(strF), ".", , vbTextCompare) + 2)) 
 Range("D" + CStr(i + 1)).Select 
 ActiveCell.Formula = "=" + CStr("B" + CStr(i + 1) + "+C" + CStr(i + 1) + "/3600/24/2") 
 Range("E" + CStr(i + 1)).Select 
 ActiveCell.Formula = "=(D" + CStr(i + 1) + "-B$3)*24" 
 j = 8 
 Do While Trim(TEMP(j)) <> "" 
 If (Trim(Mid(TEMP(j), aktpos, pogrpos - aktpos))) <> "" Then 
 For l = 6 To k - 1 
 Range(Cells(1, l), Cells(1, l)).Select 
 If (ActiveCell.Formula = Mid(Trim(TEMP(j)), 1, InStr(1, Trim(TEMP(j)), " ", vbTextCompare) - 1)) Then 
 Range(Cells(i + 1, l), Cells(i + 1, l)).Select 
 ActiveCell.Formula = Trim(Mid(Trim(TEMP(j)), aktpos, pogrpos - aktpos)) 
 Exit For 
 End If 
  
 Next l 
 End If 
 j = j + 1 
 Loop 
  
 j = 8 
 Do While Trim(TEMP(j)) <> "" 
 If (Trim(Mid(Trim(TEMP(j)), aktpos, pogrpos - aktpos))) <> "" Then 
 nuklvst = False 
 For l = 6 To k - 1 
 Range(Cells(1, l), Cells(1, l)).Select 
 If (ActiveCell.Formula = Mid(Trim(TEMP(j)), 1, InStr(1, Trim(TEMP(j)), " ", vbTextCompare) - 1)) Then 
 nuklvst = True 
 Exit For 
 End If 
 Next l 
 If nuklvst = False Then 
 Range(Cells(1, k), Cells(1, k)).Select 
 ActiveCell.Formula = Mid(Trim(TEMP(j)), 1, InStr(1, Trim(TEMP(j)), " ", vbTextCompare) - 1) 
 Range(Cells(i + 1, k), Cells(i + 1, k)).Select 
 ActiveCell.Formula = Trim(Mid(Trim(TEMP(j)), aktpos, pogrpos - aktpos)) 
 If (Trim(Mid(Trim(TEMP(j)), perpos, aktpos - perpos)) <> "") Then 
 Range(Cells(2, k), Cells(2, k)).Select 
 ActiveCell.Formula = Trim(Mid(Trim(TEMP(j)), perpos, aktpos - perpos)) 
 End If 
 k = k + 1 
 End If 
 End If 
 j = j + 1 
 Loop 
  
 End If 
 GoTo label1 
 label2: 
  
 Else    ' User chose No. 
 pr = 1 
 Open fn For Input Access Read Shared As #1 'открытие файла 
 Do While Not (EOF(1)) 'пока не конец файла (обращение к файлу по номеру) 
 Line Input #1, TEMP(strF + 1) 'Ввод данных в переменную TEMP без chr(10)&chr(13) 
 LTrim (RTrim(TEMP(strF + 1))) 
 strF = strF + 1 'Переменная для подсчета числа строк 
 Loop 'Окончание цикла для подсчета строк 
 Close #1 'Закрытие файла 
 For j = 3 To 65536 
 If IsEmpty(Range(Cells(j, 2), Cells(j, 2))) Then 
 i = j 
 Exit For 
 End If 
 Next j 
 i = i - 1 
  
 For j = 6 To 256 Step 1 
 If IsEmpty(Cells(1, j)) Then 
 k = j 
 Exit For 
 End If 
 Next j 
 label3: 
 If pr = 1 Then GoTo label5 
 lensl = InStrRev(fn, "\", , vbTextCompare) + 1 
 lendot = InStr(InStrRev(fn, "\", , vbTextCompare), fn, ".", vbTextCompare) 
 fnum = CInt(Mid(fn, lensl, lendot - lensl)) + 1 
 zerolen = lendot - 1 - lensl - Len(CStr(fnum)) 
 'For j = 1 To zerolen 
 'nfn = nfn + "0" 
 'Next j 
 'nfn = nfn + CStr(fnum) 
 'nfn = nfn + ".RPT" 
 'nfn = Mid(fn, 1, InStrRev(fn, "\", , vbTextCompare) + 1) + nfn 
 'fn = nfn 
  
 filenum = CInt(Mid(fn, lensl, lendot - lensl)) + 1 
 lfn = lendot - lensl 
 'Range(Cells(i + 1, 1), Cells(i + 1, 1)).Select 
 lcfr = Len(CStr(filenum)) 
 lfn = lendot - lensl - Len(CStr(fnum)) 
 nfn = Mid(fn, 1, InStrRev(fn, "\", , vbTextCompare)) 
 If lfn <> 0 Then 
 For j = 1 To lfn 
 nfn = nfn + "0" 
 Next j 
 End If 
 nfn = nfn + CStr(filenum) + ".rpt" 
 fn = nfn 
  
  
 i = i + 1 
 FileName$ = fn 
  
 If (VerifyFile(FileName$) = False) Then GoTo label4 
 If (VerifyFile(FileName$)) Then 
 strF = 0 
 Open fn For Input Access Read Shared As #1 'открытие файла 
 Do While Not (EOF(1)) 'пока не конец файла (обращение к файлу по номеру) 
 Line Input #1, TEMP(strF + 1) 'Ввод данных в переменную TEMP без chr(10)&chr(13) 
 LTrim (RTrim(TEMP(strF + 1))) 
 strF = strF + 1 'Переменная для подсчета числа строк 
 Loop 'Окончание цикла для подсчета строк 
 Close #1 'Закрытие файла 
 label5: 
 pr = 5 
 nuklpos = InStr(1, Trim(TEMP(6)), "Н", vbTextCompare) 
 aktpos = InStr(1, Trim(TEMP(6)), "А", vbTextCompare) + 1 
 pogrpos = InStr(aktpos, Trim(TEMP(6)), "П", vbTextCompare) - (InStr(aktpos, Trim(TEMP(6)), "П", vbTextCompare) - InStr(aktpos, Trim(TEMP(6)), ",", vbTextCompare)) 
  
 Range("A" + CStr(i + 1)).Select 
 ActiveCell.FormulaR1C1 = CStr(CInt(Mid(fn, InStrRev(fn, "\", , vbTextCompare) + 1, (InStrRev(fn, ".", , vbTextCompare)) - (InStrRev(fn, "\", , vbTextCompare) + 1)))) 
 Range("B" + CStr(i + 1)).Select 
 ActiveCell.FormulaR1C1 = Trim(Mid(TEMP(4), 23, 18)) 
 Selection.NumberFormat = "dd/mm/yy hh:mm;@" 
 strF = strF - 1 
 Range("C" + CStr(i + 1)).Select 
 ActiveCell.FormulaR1C1 = CStr(Mid(TEMP(strF), InStrRev(TEMP(strF), ",", , vbTextCompare) + 1, InStrRev(TEMP(strF), ".", , vbTextCompare) + 2)) 
 Range("D" + CStr(i + 1)).Select 
 ActiveCell.Formula = "=" + CStr("B" + CStr(i + 1) + "+C" + CStr(i + 1) + "/3600/24/2") 
 Range("E" + CStr(i + 1)).Select 
 ActiveCell.Formula = "=(D" + CStr(i + 1) + "-B$3)*24" 
 j = 8 
 Do While Trim(TEMP(j)) <> "" 
 If (Trim(Mid(TEMP(j), aktpos, pogrpos - aktpos))) <> "" Then 
 For l = 6 To k - 1 
 Range(Cells(1, l), Cells(1, l)).Select 
 If (ActiveCell.Formula = Mid(Trim(TEMP(j)), 1, InStr(1, Trim(TEMP(j)), " ", vbTextCompare) - 1)) Then 
 Range(Cells(i + 1, l), Cells(i + 1, l)).Select 
 ActiveCell.Formula = Trim(Mid(Trim(TEMP(j)), aktpos, pogrpos - aktpos)) 
 perpos = InStr(1, Trim(TEMP(6)), "П", vbTextCompare) - 5 
 'If (Trim(Mid(Trim(TEMP(j)), perpos, aktpos - perpos)) <> "") Then 
 'Range(Cells(2, k), Cells(2, k)).Select 
 'ActiveCell.Formula = Trim(Mid(Trim(TEMP(j)), perpos, aktpos - perpos)) 
 'End If 
 Exit For 
 End If 
  
 Next l 
 End If 
 j = j + 1 
 Loop 
  
 j = 8 
 Do While Trim(TEMP(j)) <> "" 
 If (Trim(Mid(Trim(TEMP(j)), aktpos, pogrpos - aktpos))) <> "" Then 
 nuklvst = False 
 For l = 6 To k - 1 
 Range(Cells(1, l), Cells(1, l)).Select 
 If (ActiveCell.Formula = Mid(Trim(TEMP(j)), 1, InStr(1, Trim(TEMP(j)), " ", vbTextCompare) - 1)) Then 
 nuklvst = True 
 Exit For 
 End If 
 Next l 
 If nuklvst = False Then 
 Range(Cells(1, k), Cells(1, k)).Select 
 ActiveCell.Formula = Mid(Trim(TEMP(j)), 1, InStr(1, Trim(TEMP(j)), " ", vbTextCompare) - 1) 
 Range(Cells(i + 1, k), Cells(i + 1, k)).Select 
 ActiveCell.Formula = Trim(Mid(Trim(TEMP(j)), aktpos, pogrpos - aktpos)) 
 If (Trim(Mid(Trim(TEMP(j)), perpos, aktpos - perpos)) <> "") Then 
 Range(Cells(2, k), Cells(2, k)).Select 
 ActiveCell.Formula = Trim(Mid(Trim(TEMP(j)), perpos, aktpos - perpos)) 
 End If 
 k = k + 1 
 End If 
 End If 
 j = j + 1 
 Loop 
  
 End If 
 GoTo label3 
 label4: 
  
 End If 
 End Sub 
 [/more] 
 h**p://ifolder.ru/3466560 - pass от архива мой ник наоборот 
 PS. Извиняюсь за стиль написания макроса.