作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
以下vba
将解析后的输出保存在工作簿中而不是 myDir
中目录为 xlsx
我似乎无法弄清楚。它似乎除此之外都起作用,我需要一些专家的帮助来弄清楚最后一部分。基本上,每个 txt
myDir
中的文件被解析,然后 txt
文件被解析为 xlsx
.目前,正在发生的是第一个 txt
文件在 myDir
正在被解析并保存在工作簿中,然后 vba
退出。
编辑
下面的 vba 运行,但在工作簿的工作表中显示解析的输出,而不是在 myDir 中保存为 xlsx。
`ActiveWorkbook.SaveAs Filename:=Replace(fn, ".txt", ""), FileFormat:=xlOpenXMLWorkbook'
stepping-through the vba I can see that fn has the full path and the filename but not sure why it does not save to myDir as an xlsx.
Option Explicit
Private Sub CommandButton21_Click()
Dim myDir As String, fn As String
myDir = "C:\Users\cmccabe\Desktop\EmArray\"
fn = Dir(myDir & "*.txt")
Do While fn <> ""
CreateXLSXFiles myDir & fn
fn = myDir
Loop
End Sub
Sub CreateXLSXFiles(fn As String)
Dim txt As String, m As Object, n As Long, myDir As String
Dim i As Long, x, temp, ub As Long, myList
myList = Array("Display Name", "Medical Record", "Date of Birth", "Order Date", _
"Gender", "Barcode", "Sample", "Build", "SpikeIn", "Location", "Control Gender", "Quality")
myDir = "C:\Users\cmccabe\Desktop\EmArray\"
Sheets(1).Cells.Clear
Sheets(1).Name = CreateObject("Scripting.FileSystemObject").GetBaseName(myDir & fn)
On Error Resume Next
n = FileLen(fn)
If Err Then
MsgBox "Something wrong with " & fn
Exit Sub
End If
On Error GoTo 0
n = 0
txt = CreateObject("Scripting.FileSystemObject").OpenTextFile(fn).ReadAll
With CreateObject("VBScript.RegExp")
.Global = True: .MultiLine = True
For i = 0 To UBound(myList)
.Pattern = "^#(" & myList(i) & " = (.*))"
If .test(txt) Then
n = n + 1
Sheets(1).Cells(n, 1).Resize(, 2).Value = _
Array(.Execute(txt)(0).submatches(0), .Execute(txt)(0).submatches(1))
End If
Next
.Pattern = "^[^#\r\n](.*[\r\n]+.+)+"
x = Split(.Execute(txt)(0), vbCrLf)
.Pattern = "(\t| {2,})"
temp = Split(.Replace(x(0), Chr(2)), Chr(2))
n = n + 1
For i = 0 To UBound(temp)
Sheets(1).Cells(n, i + 1).Value = temp(i)
Next
ub = UBound(temp)
.Pattern = "((\t| {2,})| (?=(\d|"")))"
For i = 1 To UBound(x)
temp = Split(.Replace(x(i), Chr(2)), Chr(2))
n = n + 1
Sheets(1).Cells(n, 1).Resize(, ub).Value = temp
Next
End With
Sheets(1).Copy
ActiveWorkbook.SaveAs Filename:=Replace(fn, ".txt", ""), FileFormat:=xlOpenXMLWorkbook
ActiveWorkbook.Close False
最佳答案
您正在传递 myDir & fn
作为 的参数创建 XLSX 文件 程序。该参数称为fn
在那个过程中。您没有在任何地方声明或分配 myDir
中的变量创建 XLSX 文件 程序。
“最佳实践”可能是完全删除扩展名并允许 文件格式 Workbook.SaveAs method 的参数通过适当的XlFileFormat Enumeration设置它持续的。在这种情况下, xlOpenXMLWorkbook (例如 51 )将是合适的。
ActiveWorkbook.SaveAs Filename:=Replace(fn, ".txt", ""), FileFormat:=xlOpenXMLWorkbook
ActiveWorkbook.Close False
Option Explicit
在模块代码表的顶部以避免这些类型的错误或使用 VBE 的工具、选项、编辑器、需要变量声明。如果你设置 FileFormat 和那个参数来确定文件扩展名,你应该很好。
Private Sub CommandButton21_Click()
Dim myDir As String, fn As String
myDir = "C:\Users\cmccabe\Desktop\EmArray\"
fn = Dir(myDir & "*.txt")
Do While fn <> ""
CreateXLSXFiles myDir & fn
fn = Dir '<~~ get the next filename from DIR, not reassigned to myDir!!!
Loop
End Sub
Private Sub CommandButton1_Click()
Dim myDir As String, fn As String
myDir = "C:\Users\cmccabe\Desktop\EmArray\"
fn = Dir(myDir & "file*.txt")
Do While fn <> ""
CreateXLSXFiles myDir & fn
fn = Dir
Loop
End Sub
Sub CreateXLSXFiles(fn As String)
Dim txt As String, m As Object, n As Long, fp As String
Dim i As Long, x, temp, ub As Long, myList
myList = Array("Display Name", "Medical Record", "Date of Birth", _
"Order Date", "Gender", "Barcode", "Sample", "Build", _
"SpikeIn", "Location", "Control Gender", "Quality")
fp = "C:\Users\cmccabe\Desktop\EmArray\"
With Worksheets(1)
.Cells.Clear
.Name = CreateObject("Scripting.FileSystemObject").GetBaseName(fn)
'RegEx stuff going on here
.Copy
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=fp & .Name, _
FileFormat:=xlOpenXMLWorkbook
ActiveWorkbook.Close False
Application.DisplayAlerts = True
End With
End Sub
关于vba - 在工作簿中保存不在目录中为 xlsx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34724631/
我是一名优秀的程序员,十分优秀!