简体   繁体   English

如何通过 VBA 将数据从文本文件传输到 Excel 中的特定单元格?

[英]How to transfer data from a text file to a specific cell in Excel via VBA?

I have this code which transfers data from a textfile to an Excel sheet into column C. Here is a code snippet.我有将数据从文本文件传输到 Excel 工作表到 C 列的代码。这是一个代码片段。

Dim my_file As Integer
Dim text_line As String
Dim file_name As String
Dim i As Integer

file_name = "C:\Users\" & Environ("Username") & "\Desktop\Test.txt"

my_file = FreeFile()
Open file_name For Input As my_file

i = 1

While Not EOF(my_file)
    Line Input #my_file, text_line
    Cells(i, "C").Value = text_line
    i = i + 1
Wend

But I want to transfer the data to C4.但我想将数据传输到 C4。 How can I tweak the code to achieve my objective?如何调整代码以实现我的目标?

  • Wrtiting the whole file starting from a certain cell C4 and going down with every line从某个单元格 C4 开始编写整个文件,并随着每一行向下写

在此处输入图片说明

Sub TestMe()

    Dim myFile As Long
    Dim textLine As String
    Dim fileName As String

    fileName = "C:\Users\" & Environ("Username") & "\Desktop\Test.txt"

    myFile = FreeFile()
    Open fileName For Input As myFile

    Dim writing As String
    Dim i As Long: i = 4
    While Not EOF(myFile)
        Line Input #myFile, textLine
        Worksheets(1).Cells(i, "C").Value = textLine
        i = i + 1
    Wend

End Sub
  • Wrtiting the whole file to a single cell将整个文件写入单个单元格

This one takes all the lines from the Test.txt and writes them into Range("C4") of the first worksheet:这个从Test.txt获取所有行并将它们写入第一个工作表的Range("C4")中:

Sub TestMe()

    Dim myFile As Long
    Dim textLine As String
    Dim fileName As String

    fileName = "C:\Users\" & Environ("Username") & "\Desktop\Test.txt"

    myFile = FreeFile()
    Open fileName For Input As myFile

    Dim writing As String

    While Not EOF(myFile)
        Line Input #myFile, textLine
        writing = writing & vbCrLf & textLine
    Wend

    Worksheets(1).Cells(4, "C").Value = writing        

End Sub

It is done through the loop:它是通过循环完成的:

While Not EOF(myFile)
    Line Input #myFile, textLine
    writing = writing & vbCrLf & textLine
Wend

which writes every line to the variable writing , adding a new line whenever needed ( VbCrLf is the new line).它将每一行写入变量writing ,在需要时添加新行( VbCrLf是新行)。 At the end Worksheets(1).Cells(4, "C").Value = writing writes the text of the file to the worksheet at C4 .最后Worksheets(1).Cells(4, "C").Value = writing将文件的文本写入工作表C4

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

相关问题 通过VBA将数据从Word传输到excel - transfer data from word to excel via vba Excel VBA 将单元格数据传输到另一个工作表中的特定部分或范围 - Excel VBA Transfer cell data to specific parts or ranges in another sheet VBA将数据从Excel单元格写入文本/批处理文件 - VBA Write Data From Excel Cell to Text/Batch File 如何计算 excel vba 中特定单元格数据的行数 - how to count the number of rows with data from a specific cell in excel vba 如何将输入文本作为日期传输到 VBA 中的 Excel 单元格 - How can I transfer an input text as a date to an Excel cell in VBA 如何从Excel工作表中导出数据以创建以特定格式排列的单元格数据的文本文件? - How can I export data from an excel sheet to create a text file of cell data arranged into a specific format? 如何优化通过VBA从excel中的超大文本文件提取数据的性能 - How to optimize the performance of data pulling from a very large text file in excel via VBA VBA-用于将数据从Excel导出到文本(带有分隔符的逐个单元格) - VBA - For exporting data from Excel to text (cell by cell with separators) 创建Excel VBA以从一栏中的单元格中删除特定文本 - Create Excel VBA to delete specific text from cell in one column Excel VBA代码如何向特定单元格添加文本 - Excel VBA code how to add text to specific cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM