Vba 将文本文件导入新工作表,执行一些操作,然后关闭工作表

Vba 将文本文件导入新工作表,执行一些操作,然后关闭工作表,vba,excel,import,text-files,Vba,Excel,Import,Text Files,我有个问题需要帮助解决。我想将文本文件导入新的临时工作表,找到一些数据,将它们放入当前工作表中,然后关闭新的临时工作表。这可能吗?我如何做到这一点?要创建新工作表,然后将其删除: 要打开文本文件,请读取其内容并查找特定字符串: 所以现在它应该起作用了。这是不想工作的潜艇 次级进口() End Sub您能否提供一个示例,说明数据如何显示在文本文件中,以及您希望对其执行什么操作?这是我不知道的。目前,我只需要知道如何打开一个新工作表,导入一个文本文件,然后再次关闭该工作表并返回到我开始使用的工

我有个问题需要帮助解决。我想将文本文件导入新的临时工作表,找到一些数据,将它们放入当前工作表中,然后关闭新的临时工作表。这可能吗?我如何做到这一点?

要创建新工作表,然后将其删除:



要打开文本文件,请读取其内容并查找特定字符串:



所以现在它应该起作用了。这是不想工作的潜艇

次级进口()


End Sub

您能否提供一个示例,说明数据如何显示在文本文件中,以及您希望对其执行什么操作?这是我不知道的。目前,我只需要知道如何打开一个新工作表,导入一个文本文件,然后再次关闭该工作表并返回到我开始使用的工作表。要轻松打开一个新工作表,我使用Sheets.Add.Name=“Test”。但问题似乎是我想关闭它并返回到原始工作表的部分。要将文本文件导入工作表,我将使用查询表,您可以从宏记录器获得一些合适的代码或起点。完成后请确保关闭连接。谢谢!openWorkSheet和closeWorkSheet工作正常。我还没有尝试过searchFile,但我在下面尝试过。它导入了我想要的文本文件,但由于某些原因,我无法进行任何计算。您知道为什么吗?Sub Import()将DestBook设置为工作簿,将SourceBook设置为工作簿,将DestCell设置为范围,将Dim RetVal设置为布尔集合DestBook=ActiveWorkbook Set DestCell=ActiveCell RetVal=Application.Dialogs(xlDialogOpen)。显示(“*.txt”,True)如果RetVal=False,则退出子集SourceBook=ActiveWorkbook Range(Range(“A1”)、Range(“A1”)。SpecialCells(xlLastCell))。复制DestBook.Activate DestCell.Paste特殊粘贴:=xlValues SourceBook。关闭注释中该子项的假结束子项。您应该打开一个新问题。让我知道,如果答案提供了足够的细节,你原来的职位
Option Explicit

Sub openWorkSheet()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets.Add(, ThisWorkbook.ActiveSheet)
End Sub

Sub closeWorkSheet(ByRef ws As Worksheet)
    If Not ws Is Nothing Then
        With Application
            .DisplayAlerts = False
            ws.Delete
            .DisplayAlerts = True
        End With
    End If
End Sub
Public Sub searchFile(ByVal filePathAndName As String)

    Const TYPICAL_START = "FIRST search string"
    Const TYPICAL_END = "LAST search string"

    Dim fso As Object
    Dim searchedFile As Object
    Dim fullFile As String
    Dim foundStart As Long
    Dim foundEnd As Long
    Dim resultArr() As String
    Dim i As Long

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set searchedFile = fso.OpenTextFile(filePathAndName)

    fullFile = searchedFile.ReadAll 'read entire file

    i = 1
    foundStart = 1
    foundStart = InStr(foundStart, fullFile, TYPICAL_START, vbTextCompare)

    If foundStart > 0 Then

        foundEnd = InStr(foundStart, fullFile, TYPICAL_END, vbTextCompare)

        While foundStart > 0 And foundEnd > 0
            ReDim Preserve resultArr(i)
            resultArr(i) = Mid(fullFile, foundStart, foundEnd - foundStart + 1)
            foundStart = InStr(foundStart + 1, fullFile, TYPICAL_START, vbTextCompare)
            If foundStart > 0 Then foundEnd = InStr(foundStart, fullFile, TYPICAL_END)
            i = i + 1
        Wend
    End If
End Sub
   Dim DestBook As Workbook, SourceBook As Workbook
   Dim DestCell As Range
   Dim RetVal As Boolean

   ' Set object variables for the active book and active cell.
   Set DestBook = ActiveWorkbook
   Set DestCell = ActiveCell

   ' Show the Open dialog box.
   RetVal = Application.Dialogs(xlDialogOpen).Show("*.txt", , True)

   ' If Retval is false (Open dialog canceled), exit the procedure.
   If RetVal = False Then Exit Sub

   ' Set an object variable for the workbook containing the text file.
   Set SourceBook = ActiveWorkbook

   ' Copy the contents of the entire sheet containing the text file.
   Range(Range("A1"), Range("A1").SpecialCells(xlLastCell)).Copy

   ' Activate the destination workbook and paste special the values
   ' from the text file.
   DestBook.Activate
   DestCell.PasteSpecial Paste:=xlValues

   ' Close the book containing the text file.
   SourceBook.Close False