gpt4 book ai didi

excel - 编辑在多个excel工作簿中搜索字符串并返回字符串所在行的vba代码

转载 作者:行者123 更新时间:2023-12-04 20:52:19 25 4
gpt4 key购买 nike

我找到了一个在多个工作簿中搜索字符串(“James”)并返回以下输出的代码:

  • 找到该字符串的工作簿名称,
  • 细胞
  • 以及它正在搜索的字符串 ("James")

  • 我希望代码返回找到字符串的行条目,而不仅仅是输出编号 4 中的字符串。您能帮我编辑代码吗?

    代码来源: https://www.extendoffice.com/documents/excel/3354-excel-search-multiple-sheets-workbooks.html
          Dim xFso As Object
    Dim xFld As Object
    Dim xStrSearch As String
    Dim xStrPath As String
    Dim xStrFile As String
    Dim xOut As Worksheet
    Dim xWb As Workbook
    Dim xWk As Worksheet
    Dim xRow As Long
    Dim xFound As Range
    Dim xStrAddress As String
    Dim xFileDialog As FileDialog
    Dim xUpdate As Boolean
    Dim xCount As Long
    On Error GoTo ErrHandler
    Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
    xFileDialog.AllowMultiSelect = False
    xFileDialog.Title = "Select a forlder"
    If xFileDialog.Show = -1 Then
    xStrPath = xFileDialog.SelectedItems(1)
    End If
    If xStrPath = "" Then Exit Sub
    xStrSearch = "James"
    xUpdate = Application.ScreenUpdating
    Application.ScreenUpdating = False
    Set xOut = Worksheets.Add
    xRow = 1
    With xOut
    .Cells(xRow, 1) = "Workbook"
    .Cells(xRow, 2) = "Worksheet"
    .Cells(xRow, 3) = "Cell"
    .Cells(xRow, 4) = "Text in Cell"
    Set xFso = CreateObject("Scripting.FileSystemObject")
    Set xFld = xFso.GetFolder(xStrPath)
    xStrFile = Dir(xStrPath & "\*.xls*")
    Do While xStrFile <> ""
    Set xWb = Workbooks.Open(Filename:=xStrPath & "\" & xStrFile, UpdateLinks:=0, ReadOnly:=True, AddToMRU:=False)
    For Each xWk In xWb.Worksheets
    Set xFound = xWk.UsedRange.Find(xStrSearch)
    If Not xFound Is Nothing Then
    xStrAddress = xFound.Address
    End If
    Do
    If xFound Is Nothing Then
    Exit Do
    Else
    xCount = xCount + 1
    xRow = xRow + 1
    .Cells(xRow, 1) = xWb.Name
    .Cells(xRow, 2) = xWk.Name
    .Cells(xRow, 3) = xFound.Address
    .Cells(xRow, 4) = xFound.Value
    End If
    Set xFound = xWk.Cells.FindNext(After:=xFound)
    Loop While xStrAddress <> xFound.Address
    Next
    xWb.Close (False)
    xStrFile = Dir
    Loop
    .Columns("A:D").EntireColumn.AutoFit
    End With
    MsgBox xCount & "cells have been found", , "Kutools for Excel"
    ExitHandler:
    Set xOut = Nothing
    Set xWk = Nothing
    Set xWb = Nothing
    Set xFld = Nothing
    Set xFso = Nothing
    Application.ScreenUpdating = xUpdate
    Exit Sub
    ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
    End Sub

    当前代码结果:
    Current code output

    How I want it to look after code update:

    Data

    最佳答案

    基本上你需要找出数据中最后使用的列工作簿 然后只需遍历列并将数据写入 新的工作簿。 我添加了 xCol and i as long并做了一个for循环来写入数据。

    Option Explicit        
    Sub OpenWBCopyData()
    Dim xFso As Object
    Dim xFld As Object
    Dim xStrSearch As String
    Dim xStrPath As String
    Dim xStrFile As String
    Dim xOut As Worksheet
    Dim xWb As Workbook
    Dim xWk As Worksheet
    Dim xRow As Long
    Dim xCol as Long
    Dim i as Long
    Dim xFound As Range
    Dim xStrAddress As String
    Dim xFileDialog As FileDialog
    Dim xUpdate As Boolean
    Dim xCount As Long

    On Error GoTo ErrHandler

    Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
    xFileDialog.AllowMultiSelect = False
    xFileDialog.Title = "Select a forlder"
    If xFileDialog.Show = -1 Then
    xStrPath = xFileDialog.SelectedItems(1)
    End If
    If xStrPath = "" Then Exit Sub
    xStrSearch = "James"
    xUpdate = Application.ScreenUpdating
    Application.ScreenUpdating = False
    Set xOut = Worksheets.Add
    xRow = 1
    With xOut
    .Cells(xRow, 1) = "Workbook"
    .Cells(xRow, 2) = "Worksheet"
    .Cells(xRow, 3) = "Cell"
    .Cells(xRow, 4) = "Text in Cell"
    Set xFso = CreateObject("Scripting.FileSystemObject")
    Set xFld = xFso.GetFolder(xStrPath)
    xStrFile = Dir(xStrPath & "\*.xls*")
    Do While xStrFile <> ""
    Set xWb = Workbooks.Open(Filename:=xStrPath & "\" & xStrFile, UpdateLinks:=0, ReadOnly:=True, AddToMRU:=False)
    For Each xWk In xWb.Worksheets
    Set xFound = xWk.UsedRange.Find(xStrSearch)
    If Not xFound Is Nothing Then
    xStrAddress = xFound.Address
    xCol = xWk.xFound(xFound.Cell & .Columns.Count).End(xlToLeft).Column
    End If
    Do
    If xFound Is Nothing Then
    Exit Do
    Else
    xCount = xCount + 1
    xRow = xRow + 1
    .Cells(xRow, 1) = xWb.Name
    .Cells(xRow, 2) = xWk.Name
    .Cells(xRow, 3) = xFound.Address
    .Cells(xRow, 4) = xFound.Value
    For i = 1 To xCol
    .Cells(xRow, 4 + i) = xFound.Value
    Next i
    End If
    Set xFound = xWk.Cells.FindNext(After:=xFound)
    Loop While xStrAddress <> xFound.Address
    Next
    xWb.Close (False)
    xStrFile = Dir
    Loop
    .Columns("A:D").EntireColumn.AutoFit
    End With
    MsgBox xCount & "cells have been found", , "Kutools for Excel"
    ExitHandler:
    Set xOut = Nothing
    Set xWk = Nothing
    Set xWb = Nothing
    Set xFld = Nothing
    Set xFso = Nothing
    Application.ScreenUpdating = xUpdate
    Exit Sub
    ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
    End Sub

    关于excel - 编辑在多个excel工作簿中搜索字符串并返回字符串所在行的vba代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56097081/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com