如何计算 VBA 中 .txt 文件中的列数
How can I Count the number of columns in a .txt file in VBA
我正在尝试使用宏计算 .txt 文件的列数。我的代码如何永远 运行 而罪魁祸首是这段代码
Set wb = Workbooks.Open(FName)
Dim lastColumn As Integer
Dim rng As Range
Set rng = wb.Worksheets(1).Cells
lastColumn = rng.Find(What:="*", After:=rng.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column
wb.Close False
我认为基本上是打开 .txt 文件,计算它们,将其存储在一个值中,然后关闭它,这需要永远,有没有更好的方法来做到这一点?
我喜欢把解决这些类型的事情当作晨练,所以我制作了一个函数来计算“csv”的列数,而无需将文件打开到 excel 应用程序中。
VBA 具有 I/O 接口,允许它打开文本文件,而无需花费过多资源将其作为工作簿加载到应用程序中。使用这个方法应该比Workbooks.Open
.
快很多
要将其从“csv”更改为制表符分隔,请将 SEPARATOR
更改为文件的特定字符。
Sub test2() 'Example usage of the function
Const FilePath As String = "C:\Users\Me\Desktop\Book1.csv"
MsgBox CSVColumnCount(FilePath)
End Sub
Function CSVColumnCount(FileName As String, Optional CheckAll As Boolean = True) As Long
Const SEPARATOR As String = ","
Dim FileNum As Long
FileNum = FreeFile
Open FileName For Input As #FileNum
Dim LineText As String, ColCount As Long, LineCol As Long
Do While Not EOF(FileNum)
Line Input #FileNum, LineText
LineCol = CountOccur(SEPARATOR, LineText) + 1
If LineCol > ColCount Then ColCount = LineCol
If Not CheckAll Then Exit Do
Loop
CSVColumnCount = ColCount
Close FileNum
End Function
Function CountOccur(look_for As String, within_text As String, Optional CompareMethod As VbCompareMethod = vbBinaryCompare) As Long
'Count the number of times the given string appears within the larger text body.
Dim Count As Long, Start As Long
While InStr(Start + 1, within_text, look_for, CompareMethod) <> 0
Start = InStr(Start + 1, within_text, look_for, CompareMethod)
Count = Count + 1
Wend
CountOccur = Count
End Function
我让函数检查每行的长度并报告最大值,因为我认为可能有短行或长行,但我意识到大多数“csv”类型文件不应该是这种情况.所以我添加了一个选项来只检查第一行。
您也可以使用 ADO 进行调查。
Function ColumnCount(strPath As String, strFileName As String) As Long
Dim c As New ADODB.Connection
Dim r As New ADODB.Recordset
c.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath & ";Extended Properties='text;HDR=Yes;FMT=Delimited';"
c.Open
r.Open "select * from [" & strFileName & "]", c
ColumnCount = r.Fields.Count
r.Close
c.Close
Set c = Nothing
Set r = Nothing
End Function
像这样调用ColumnCount("c:\dev","test_csv.csv")
可以做一些工作来检查文件是否存在、拆分路径等。只是一个想法。
我正在尝试使用宏计算 .txt 文件的列数。我的代码如何永远 运行 而罪魁祸首是这段代码
Set wb = Workbooks.Open(FName)
Dim lastColumn As Integer
Dim rng As Range
Set rng = wb.Worksheets(1).Cells
lastColumn = rng.Find(What:="*", After:=rng.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column
wb.Close False
我认为基本上是打开 .txt 文件,计算它们,将其存储在一个值中,然后关闭它,这需要永远,有没有更好的方法来做到这一点?
我喜欢把解决这些类型的事情当作晨练,所以我制作了一个函数来计算“csv”的列数,而无需将文件打开到 excel 应用程序中。
VBA 具有 I/O 接口,允许它打开文本文件,而无需花费过多资源将其作为工作簿加载到应用程序中。使用这个方法应该比Workbooks.Open
.
要将其从“csv”更改为制表符分隔,请将 SEPARATOR
更改为文件的特定字符。
Sub test2() 'Example usage of the function
Const FilePath As String = "C:\Users\Me\Desktop\Book1.csv"
MsgBox CSVColumnCount(FilePath)
End Sub
Function CSVColumnCount(FileName As String, Optional CheckAll As Boolean = True) As Long
Const SEPARATOR As String = ","
Dim FileNum As Long
FileNum = FreeFile
Open FileName For Input As #FileNum
Dim LineText As String, ColCount As Long, LineCol As Long
Do While Not EOF(FileNum)
Line Input #FileNum, LineText
LineCol = CountOccur(SEPARATOR, LineText) + 1
If LineCol > ColCount Then ColCount = LineCol
If Not CheckAll Then Exit Do
Loop
CSVColumnCount = ColCount
Close FileNum
End Function
Function CountOccur(look_for As String, within_text As String, Optional CompareMethod As VbCompareMethod = vbBinaryCompare) As Long
'Count the number of times the given string appears within the larger text body.
Dim Count As Long, Start As Long
While InStr(Start + 1, within_text, look_for, CompareMethod) <> 0
Start = InStr(Start + 1, within_text, look_for, CompareMethod)
Count = Count + 1
Wend
CountOccur = Count
End Function
我让函数检查每行的长度并报告最大值,因为我认为可能有短行或长行,但我意识到大多数“csv”类型文件不应该是这种情况.所以我添加了一个选项来只检查第一行。
您也可以使用 ADO 进行调查。
Function ColumnCount(strPath As String, strFileName As String) As Long
Dim c As New ADODB.Connection
Dim r As New ADODB.Recordset
c.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath & ";Extended Properties='text;HDR=Yes;FMT=Delimited';"
c.Open
r.Open "select * from [" & strFileName & "]", c
ColumnCount = r.Fields.Count
r.Close
c.Close
Set c = Nothing
Set r = Nothing
End Function
像这样调用ColumnCount("c:\dev","test_csv.csv")
可以做一些工作来检查文件是否存在、拆分路径等。只是一个想法。