繁体   English   中英

Excel 中文本文件的自动导入/绘图

[英]Automatic Import/Graphing of Text Files in Excel

我运行一台机器,它输出多个文本文件,我 plot,我有一个 VBA 脚本,它可以将我想要的所有文件导入 plot 从他们自己的文件夹中并将它们放在他们自己的工作表上。 我想知道是否有一种方法可以在导入它们时自动绘制它们? 我需要为每对测试提供一个单独的图表。 那就是我有“测试 A-1”和“测试 A-2”,它们是相互绘制的,“测试 B-1”和“测试 B-2”在新图表上等等。对不起,如果这令人困惑,我我对 VBA 仍然很陌生,并且会喜欢这样的工具来让我的生活更轻松一些。 我已经包含了我的代码,它完成了下面的所有导入。 然后,每个文本文件(只是 xy 散点图的数据)都有自己的表格,数据在 A 和 B 列中。提前为糟糕的格式道歉,我没有写!

Sub ImportTextToExcel()
'UpdatebyExtendoffice20180911
Dim xWb As Workbook
Dim xToBook As Workbook
Dim xStrPath As String
Dim xFileDialog As FileDialog
Dim xFile As String
Dim xFiles As New Collection
Dim I As Long
Dim xIntRow As Long
Dim xFNum, xFArr As Long
Dim xStrValue As String
Dim xRg As Range
Dim xArr

Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
xFileDialog.AllowMultiSelect = False
xFileDialog.Title = "Select a folder"
If xFileDialog.Show = -1 Then
xStrPath = xFileDialog.SelectedItems(1)
End If
If xStrPath = "" Then Exit Sub
If Right(xStrPath, 1) <> "\" Then xStrPath = xStrPath & "\"
xFile = Dir(xStrPath & "*.txt")
If xFile = "" Then
MsgBox "No files found", vbInformation
Exit Sub
End If
Do While xFile <> ""
xFiles.Add xFile, xFile
xFile = Dir()
Loop
Set xToBook = ThisWorkbook
On Error Resume Next
Application.ScreenUpdating = False
If xFiles.Count > 0 Then

For I = 1 To xFiles.Count
Set xWb = Workbooks.Open(xStrPath & xFiles.Item(I))
xWb.Worksheets(1).Copy after:=xToBook.Sheets(xToBook.Sheets.Count)

ActiveSheet.Name = xWb.Name

xWb.Close False
xIntRow = ActiveCell.CurrentRegion.Rows.Count
For xFNum = 1 To xIntRow
Set xRg = ActiveSheet.Range("A" & xFNum)
xArr = Split(xRg.Text, " ")
If UBound(xArr) > 0 Then
For xFArr = 0 To UBound(xArr)
If xArr(xFArr) <> "" Then
xRg.Value = xArr(xFArr)
Set xRg = xRg.Offset(ColumnOffset:=1)
End If
Next
End If
Next
Next
End If
Application.ScreenUpdating = True
End Sub
Option Explicit

Sub ImportTextToExcel()
    'UpdatebyExtendoffice20180911
    Dim xWb As Workbook, xToBook As Workbook, ws As Worksheet
    Dim xFile As String, xStrPath As String, xStrValue As String
    Dim xRg As Range, cht As Chart
    Dim xFiles As New Collection
    Dim i As Long
    
    ' choose folder
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .Title = "Select a folder"
       If .Show = -1 Then
           xStrPath = .SelectedItems(1)
       End If
    End With
    If xStrPath = "" Then
        Exit Sub
    ElseIf Right(xStrPath, 1) <> "\" Then
        xStrPath = xStrPath & "\"
    End If

    ' build collection of text files
    xFile = Dir(xStrPath & "*.txt")
    Do While xFile <> ""
        xFiles.Add xFile, xFile
        xFile = Dir()
    Loop
    If xFiles.Count = 0 Then
        MsgBox "No files found", vbCritical
        Exit Sub
    End If

    ' import data
    Set xToBook = ThisWorkbook
    Application.ScreenUpdating = False
    For i = 1 To xFiles.Count
        
        ' import data into new sheet
        Set xWb = Workbooks.Open(xStrPath & xFiles.Item(i))
        xWb.Worksheets(1).Copy after:=xToBook.Sheets(xToBook.Sheets.Count)
        Set ws = ActiveSheet
        ws.Name = xWb.Name
        xWb.Close False

        ' split on space
        Set xRg = ws.Range("A1")
        xRg.CurrentRegion.TextToColumns Destination:=xRg.Cells(1, 1), _
                          DataType:=xlDelimited, _
                          ConsecutiveDelimiter:=True, _
                          Space:=True, FieldInfo:=Array(Array(1, 1), Array(2, 1))
        
        ' create chart
        Set cht = ws.Shapes.AddChart.Chart
        With cht
            .ChartType = xlXYScatter
            .SetSourceData Source:=xRg.CurrentRegion
        End With
        
    Next
    Application.ScreenUpdating = True
    MsgBox xFiles.Count & " files imported", vbInformation
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM