修剪Excel VBA编辑器中发布的参考文本

时间:2013-08-12 18:08:32

标签: excel vba excel-vba

enter code here我在处理它的过程中有以下代码,所以我试图从.txt文件中获取文本以显示在Excel上的单元格中,代码将为< / p>

Sub citi()
Dim c As Range
Open "C:\Users\alvaradod\Desktop\citi macro\Import File.txt" For Input As #1
R = 0
Dim i As Range
Dim num As Integer
Dim arrData() As String
the_value = Sheets("Prog").Range("A1")
Do Until EOF(1)
Line Input #1, Data
If Not Left(Data, 1) = "" Then
'import this row
R = R + 1
Cells(R, 1).Value = Data
'Mid(the_value, 3, 5)
'Left(Data, Len(Data) - 3)).Value
End If
Loop
For Each i In Range("A1")
i.Select
ActiveCell.Rows("1:1").Mid(Data(i), 49, 5).Select
'ActiveCell.Rows("1:1").EntireRow.Select
Selection.Copy
Sheets("Import").Range("A1").End(xlUp).Offset(num, 0).PasteSpecial
ActiveCell.Rows.Delete
 num = num + 1
Next i

End Sub

“第11行将把文字从第一行翻到.TXT文件到EXCEL,在这个功能之后,我需要在EXCEL页面中修剪这个相同的文字,以显示第一个5个字符”

1 个答案:

答案 0 :(得分:0)

你的问题不是很清楚,但也许是这样的?

Sub citi()

    Dim oFSO As Object
    Dim arrData() As String
    Dim arrImport1(1 To 65000) As String
    Dim arrImport2(1 To 65000) As String
    Dim i As Long, j As Long

    Set oFSO = CreateObject("Scripting.FileSystemObject")
    arrData = Split(oFSO.OpenTextFile("C:\Test\test.txt").ReadAll, vbCrLf)

    For i = LBound(arrData) To UBound(arrData)
        If Len(arrData(i)) > 0 Then
            j = j + 1
            arrImport1(j) = Mid(arrData(i), 3, 5)
            arrImport2(j) = Mid(arrData(i), 49, 5)
        End If
    Next i

    If j > 0 Then
        Sheets("Sheet1").Range("A1").Resize(j).Value = Application.Transpose(arrImport1)
        Sheets("Sheet2").Range("A1").Resize(j).Value = Application.Transpose(arrImport2)
    End If

    Set oFSO = Nothing
    Erase arrData
    Erase arrImport

End Sub