【问题标题】:opening workbook after searching for a string搜索字符串后打开工作簿
【发布时间】:2015-09-11 11:19:15
【问题描述】:

在我解释这段代码是关于什么之前稍微了解一下,首先用户将打开一个空的microsoft excel,然后用户将在一个宏上打开多个工作簿进入当前活动的excel,例如,如果用户选择要同时打开“book1”和“book2”,当前活动的 Excel 将打开它们并将它们拆分为一个以当前命名的工作簿命名的新工作表,例如工作表“book1”和工作表“book2”。

所以基本上这个程序使用户能够搜索一个字符串(在所有工作表中),然后在找出字符串的位置后,它将整个行+标题复制到一个以搜索字符串命名的新工作表。

例如,如果我搜索苹果,它将复制包含单词“apple”的整行并粘贴到名为“apple”的新工作表中,行和标题将被复制到那里,我需要现在要做的是创建一个新工作簿并创建一个以搜索字符串和之前命名的工作簿命名的新工作表。

就像我说的,我打开了工作簿“book1”和“book2”,如果搜索的单词来自工作表“book1”,宏会将搜索到的字符串复制到一个新工作簿中,一个名为“book1”的新工作表" 与信息。

我知道我已经以非常冗长的方式解释了这一点,如果您需要任何澄清,请告诉我。

    Private Sub CommandButton5_Click()
   Dim i As Long, nRowsAddePerSheet As Long, nRows As Long, _
    nRowsMax As Long, nSheets As Long
  Dim strSearch, strSearch2
  Dim rg As Range, rgF As Range
  Dim wks
  Dim x

  strSearch = Application.InputBox("Please enter the search string")
  strSearch2 = Replace(strSearch, "*", "")
  If Len(strSearch2) <= 0 Then
    MsgBox "ABandon: Search string must not be empty."
    Exit Sub
  End If

  Application.ScreenUpdating = False

  nSheets = Sheets.Count
  nRowsMax = ActiveSheet.Rows.Count

  For x = 1 To nSheets

    On Error Resume Next
    Set wks = Worksheets(strSearch2)
    If (Err) Then
      Set wks = Worksheets.Add(After:=Sheets(Sheets.Count))
      wks.Name = strSearch2
      Err.Clear
    End If
    On Error GoTo 0

    Sheets(x).Activate
    Set rg = ActiveSheet.Cells(1).CurrentRegion

    nRows = rg.Rows.Count
    nRowsAddePerSheet = 0
    For i = 1 To nRows
      Set rgF = rg.Rows(i).Find(strSearch, , xlValues, xlWhole)

      If Not rgF Is Nothing Then

        If (nRowsAddePerSheet <= 0) Then
          If (i <> 1) Then
            rg.Rows(1).Copy wks.Range("A" & nRowsMax).End(xlUp).Offset(1, 0)
          End If
        End If

        rg.Rows(i).Copy wks.Range("A" & nRowsMax).End(xlUp).Offset(1, 0)
        nRowsAddePerSheet = nRowsAddePerSheet + 1
      End If
    Next
  Next

  Set rgF = Nothing
  Set rg = Nothing
  Set wks = Nothing

  Application.ScreenUpdating = True

End Sub

【问题讨论】:

  • 您在哪里搜索apple?工作表或特定列中的任何位置?
  • @SiddharthRout 工作簿中的任何位置,包括所有工作表。

标签: excel vba search excel-2010


【解决方案1】:

这是你正在尝试的吗?

来自 cmets 的跟进:想法是保存文件,然后检查该文件是否存在。如果存在,则找到最后一行,然后在那里输入数据。 显式选项

Dim HeaderExists As Boolean

Sub Sample()
    Dim wb As Workbook, wbNew As Workbook
    Dim ws As Worksheet, wsNew As Worksheet
    Dim strSearch As String
    Dim aCell As Range, bCell As Range
    Dim LRow As Long, nCol As Long

    strSearch = Application.InputBox("Please enter the search string")

    If strSearch = "" Then
        MsgBox "ABandon: Search string must not be empty."
        Exit Sub
    End If

    '~~> Check if a workbook with the name already exists
    For Each wb In Application.Workbooks
        If InStr(1, wb.Name, strSearch & ".xl", vbTextCompare) Then
            Set wbNew = wb
            On Error Resume Next
            Set wsNew = wbNew.Sheets(strSearch)
            On Error GoTo 0
            Exit For
        End If
    Next

    If Not wsNew Is Nothing Then
        If Application.WorksheetFunction.CountA(wsNew.Cells) <> 0 Then
            HeaderExists = True
            LRow = wsNew.Cells.Find(What:="*", _
                    After:=wsNew.Range("A1"), _
                    Lookat:=xlPart, _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious, _
                    MatchCase:=False).Row + 1

        End If
    End If

    '~~> Add the new workbook
    If wbNew Is Nothing Then
        Set wbNew = Workbooks.Add
        wbNew.SaveAs "C:\" & strSearch & ".xls", FileFormat:=56
        Set wsNew = wbNew.Sheets(1)
        wsNew.Name = strSearch
    End If

    If LRow = 0 Then LRow = 1

    '~~> Loop through all workbooks and worksheets to find the word
    For Each wb In Application.Workbooks
        If wb.Name <> wbNew.Name Then
            For Each ws In wb.Worksheets
                Set aCell = ws.Cells.Find(What:=strSearch, LookIn:=xlValues, _
                    Lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                    MatchCase:=False, SearchFormat:=False)

                If Not aCell Is Nothing Then
                    Set bCell = aCell

                    nCol = aCell.Row

                    If HeaderExists = False Then
                        ws.Rows(1).Copy wsNew.Rows(1)
                        LRow = LRow + 1
                    End If


                    ws.Rows(aCell.Row).Copy wbNew.Sheets(1).Rows(LRow)

                    LRow = LRow + 1

                    Do
                        Set aCell = ws.Cells.FindNext(After:=aCell)

                        If Not aCell Is Nothing Then
                            If aCell.Address = bCell.Address Then Exit Do

                            If nCol <> aCell.Row Then
                                ws.Rows(aCell.Row).Copy wsNew.Rows(LRow)
                                LRow = LRow + 1
                            End If
                        Else
                            Exit Do
                        End If
                    Loop
                End If
            Next
        End If
    Next
End Sub

【讨论】:

  • 几乎在那里,第一次,当我进行搜索时,它会打开一个新的工作簿(book1),但第二次,我希望搜索也出现在 book1 上......谢谢。
  • 不,这不是我的想法,例如,如果用户第一次搜索苹果(在 book1 上),它将打开一个工作簿并将信息传递到该新工作簿(book2) ,如果我在主工作簿(book1)上进行第二次搜索,找到的任何内容都将传递给工作簿(book2),而是一个新的工作表......谢谢。
  • 但是你怎么能有两张同名的床单呢?
  • 对不起,新的搜索将是一个新词,不会是苹果。
  • 然后整个逻辑发生变化 :) 我要睡觉了。当我醒来时会喜欢它。
猜你喜欢
  • 2020-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多