Dodaj do ulubionych

EXCEL - ilość linii w pliku tekstowym

30.10.08, 13:26
Pozdrawiam.
Mam makro, które wczytuje do arkusza plik tekstowy, a następnie go
obrabia.
Chciałabym, aby po wczytaniu pliku tekstowego makro posiadało w
zmiennej informację o ilości linii tego pliku, aby ta zmienna była
wykorzystywana przez zmienną np. w ten sposób:
Range("A4:A344").Select - gdzie 344 to ilość linii.
Jak to zrobić?

Dziękuję za ewentualne uwagi.

Barbara Kozłowska
Obserwuj wątek
    • Gość: Gość Re: EXCEL - ilość linii w pliku tekstowym IP: *.acn.waw.pl 30.10.08, 18:48
      Linie można policzyć modyfikując makro, które wczytuje plik. Bez znajomości tego
      makra trudno coś konkretnego powiedzieć.
      • basia555 Re: EXCEL - ilość linii w pliku tekstowym 03.11.08, 09:19
        No to całe makro (przepraszam, że długie):

        Sub Makro1()
        '
        ' Makro1 Makro
        ' Makro zarejestrowane 6-04-2005, autor ja
        '
        ' Klawisz skrótu: Ctrl+Shift+Q
        '
        Columns("I:J").Select
        Range("I3").Activate
        Selection.NumberFormat = "[h]:mm:ss"
        Columns("K:K").Select
        Selection.NumberFormat = "#,##0.00"
        Range("B4").Select
        With ActiveSheet.QueryTables.Add
        (Connection:="TEXT;C:\test\test.bzd", Destination:=Range("B4"))
        .Name = "test"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = False
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = xlWindows
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = True
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1,
        1)
        .Refresh BackgroundQuery:=False
        End With
        Range("A4").Select
        ActiveCell.FormulaR1C1 = "1"
        Range("A5").Select
        ActiveCell.FormulaR1C1 = "2"
        Range("A4:A5").Select
        Selection.AutoFill Destination:=Range("A4:A344"),
        Type:=xlFillDefault
        Range("A4:A344").Select
        ActiveWindow.ScrollRow = 1
        Range("A4:K344").Select
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
        End With
        Range("H345").Select
        ActiveCell.FormulaR1C1 = "RAZEM:"
        Range("H345:K345").Select
        Range("K345").Activate
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
        End With
        Range("J345").Select
        With Selection.Interior
        .ColorIndex = 1
        .Pattern = xlSolid
        End With
        Range("H345").Select
        With Selection
        .HorizontalAlignment = xlRight
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .ShrinkToFit = False
        .MergeCells = False
        End With
        Range("I345").Select
        ActiveCell.FormulaR1C1 = "=SUM(R[-341]C:R[-1]C)"
        Range("K345").Select
        ActiveCell.FormulaR1C1 = "=SUM(R[-338]C:R[-1]C)"
        Range("A1:A3").Select
        End Sub

        I to całe makro. Większość z niego dla zrozumienia jest
        niepotrzebna, tak go stworzył program Excel.

        Pozdrawiam

        Barbara Kozłowska
        • Gość: Gość Re: EXCEL - ilość linii w pliku tekstowym IP: *.acn.waw.pl 03.11.08, 12:25
          Można np tak:

          W module dodajemy nową funkcję do liczenia linii:

          Public Function ilosc_linii(nazwa_pliku As String) As Integer
          Open nazwa_pliku For Input As #1
          While Not (EOF(1))
          Line Input #1, tmp
          ilosc_linii = ilosc_linii + 1
          Wend
          Close #1
          End Function

          A w funkcji, która podałaś, przed ostatnim End Sub dodajemy:
          Range("A4:A" & ilosc_linii("C:\test\test.bzd") & "").Select

          Jeżeli plik ma np 25 linii wtedy w wyniku złączenia tekstu uzyskamy:
          Range("A4:A25").Select
          • basia555 Re: EXCEL - ilość linii w pliku tekstowym 13.11.08, 17:16
            Dziękuję za pomoc.

            Barbara Kozłowska

Nie masz jeszcze konta? Zarejestruj się


Nakarm Pajacyka