【发布时间】:2021-07-01 20:52:04
【问题描述】:
我运行一台机器,它可以输出多个要绘制的文本文件,我有一个 VBA 脚本,它可以从文件夹中导入我想要绘制的所有文件并将它们放在自己的工作表上。我想知道是否有一种方法可以在导入它们时自动绘制它们?我需要为每对测试提供一个单独的图表。那就是我有“测试A-1”和“测试A-2”,它们是相互绘制的,“测试B-1”和“测试B-2”在一个新的图表上等等。对不起,如果这令人困惑,我我对 VBA 还是很陌生,并且会喜欢这样的工具来让我的生活更轻松一些。我已经包含了我的代码,它完成了下面的所有导入。然后,每个文本文件(只是 x-y 散点图的数据)都有自己的表格,数据在 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
【问题讨论】:
-
“提前为糟糕的格式道歉” - 你知道你可以解决它,对吧?所以我们不必尝试那样阅读它?