如何将整个txt文件复制到Excel
我有一个不完全有效的代码:如何将整个txt文件复制到Excel,excel,vba,copy-paste,txt,Excel,Vba,Copy Paste,Txt,我有一个不完全有效的代码: Sub Import_TXT() Dim FileToOpen As Variant Dim OpenBook As Workbook Application.GetOpenFilename ("Text Files (*.txt), *.txt") If FileToOpen <> False Then Set OpenBook = Application.Workbooks.Open
Sub Import_TXT()
Dim FileToOpen As Variant
Dim OpenBook As Workbook
Application.GetOpenFilename ("Text Files (*.txt), *.txt")
If FileToOpen <> False Then
Set OpenBook = Application.Workbooks.Open(FileToOpen)
OpenBook.Sheets(1).Range("A1").Copy
ThisWorkbook.Worksheets("BOM").Range("C1").PasteSpecial xlPasteValues
OpenBook.colse False
End If
End Sub
到
说我只能在“A1”中粘贴
通过简单的改变
.cells
到
我可以将单元格“C1”定义为粘贴所有内容的范围问题是
FileToOpen
总是False
,因为Dim FileToOpen as Variant
之后,您不会将任何值放入该变量
你可能是有意的
FileToOpen = Application.GetOpenFilename ("Text Files (*.txt), *.txt")
另外,OpenBook.colse-False
必须是OpenBook.Close-False
(请参见colse
中的打字错误)
编辑问题后更新: 由于使用
OpenBook.Sheets(1.cells.copy
复制整个工作表的所有单元格,因此会收到该错误消息。因此,如果从A1开始,它们只能适合粘贴的图纸,否则将超出图纸的边界。解决方案不是复制所有单元格,而是只复制所需的单元格
可能使用
OpenBook.Sheets(1).UsedRange.Copy可以解决此问题(取决于数据的外观)。我经常将txt文件导入我的工作簿,以下代码可能会帮助您:
sub Copy_TXT_To_My_Plan()
dim TextFile as integer
dim FilePath as string
dim FileContent as string
FilePath = "C:/Files/MyExampleTextFile.txt" 'put the complete path to your textfile here
TextFile = FreeFile
Open FilePath for Input as TextFile
FileContent = Input(LOF(TextFile), TextFile)
Close TextFile
'now you have your txt in the FileContent string
'you could just use Plan1.cells(1,1).Value = FileContent or
'you could split it in a line array, something like:
dim MyLineArray() as String
MyLineArray = Split(FileContent, VbCrlf) 'or VbNewLine
'then you can loop through the content of your file
End Sub
此代码将循环遍历文件夹中的所有文本文件,并将一个文件导入一个单元格,然后将另一个文件导入另一个单元格,依此类推
Sub Import_All_Text_Files()
Application.ScreenUpdating = False
Dim thisRow As Long
Dim fileNum As Integer
Dim strInput As String
Const strPath As String = "C:\your_path_here\" 'Change as required
Dim strFilename As String
strFilename = Dir(strPath & "*.txt")
With ActiveSheet
thisRow = .Range("A" & .Rows.Count).End(xlUp).Row
Do While strFilename <> ""
fileNum = FreeFile()
Open strPath & strFilename For Binary As #fileNum
strInput = Space$(LOF(fileNum))
Get #fileNum, , strInput
Close #fileNum
thisRow = thisRow + 1
.Cells(thisRow, 1).Value = strInput
strFilename = Dir
Loop
End With
Application.ScreenUpdating = True
End Sub
子导入所有文本文件()
Application.ScreenUpdating=False
把这一排调暗一样长
Dim fileNum作为整数
像弦一样的模糊的条纹
Const strPath As String=“C:\your\u path\u here\”,根据需要更改
将strFilename设置为字符串
strFilename=Dir(strPath&“*.txt”)
使用ActiveSheet
thisRow=.Range(“A”&.Rows.Count).End(xlUp).Row
当strFilename“”时执行此操作
fileNum=FreeFile()
将二进制文件的strPath和strFilename作为#fileNum打开
strInput=Space$(LOF(fileNum))
获取#fileNum,strInput
关闭#fileNum
thisRow=thisRow+1
.Cells(此行,1)。值=strInput
strFilename=Dir
环
以
Application.ScreenUpdating=True
端接头
如果您只需要导入一个文件,它将只导入该文件。请注意,“不工作”是一个非常无用的错误描述。你得到了什么错误,我们需要的信息在哪里,或者你的代码做了什么,或者你期望它做什么post@Eduards查看我编辑的答案。这是有道理的,但是没有回答如何在我需要的范围内获取整个txt的问题。我刚才确实是自己想出来的,只是把.cells改成了。UsedRange@Eduards嗯,我无法回答更详细的问题,因为我不知道您的文件在Excel中打开后是什么样子如果这回答了你的问题,请投票/将其标记为已解决:@Eduards我编辑了答案,因此你可以使用此标记将其标记为已解决。非常感谢你的回答,这是一段有用的代码。如果答案对你有帮助,请将其标记为有用。
.UsedRange
FileToOpen = Application.GetOpenFilename ("Text Files (*.txt), *.txt")
sub Copy_TXT_To_My_Plan()
dim TextFile as integer
dim FilePath as string
dim FileContent as string
FilePath = "C:/Files/MyExampleTextFile.txt" 'put the complete path to your textfile here
TextFile = FreeFile
Open FilePath for Input as TextFile
FileContent = Input(LOF(TextFile), TextFile)
Close TextFile
'now you have your txt in the FileContent string
'you could just use Plan1.cells(1,1).Value = FileContent or
'you could split it in a line array, something like:
dim MyLineArray() as String
MyLineArray = Split(FileContent, VbCrlf) 'or VbNewLine
'then you can loop through the content of your file
End Sub
Sub Import_All_Text_Files()
Application.ScreenUpdating = False
Dim thisRow As Long
Dim fileNum As Integer
Dim strInput As String
Const strPath As String = "C:\your_path_here\" 'Change as required
Dim strFilename As String
strFilename = Dir(strPath & "*.txt")
With ActiveSheet
thisRow = .Range("A" & .Rows.Count).End(xlUp).Row
Do While strFilename <> ""
fileNum = FreeFile()
Open strPath & strFilename For Binary As #fileNum
strInput = Space$(LOF(fileNum))
Get #fileNum, , strInput
Close #fileNum
thisRow = thisRow + 1
.Cells(thisRow, 1).Value = strInput
strFilename = Dir
Loop
End With
Application.ScreenUpdating = True
End Sub