Hi Guys,
I am looking for a little bit of help. I have managed to write some code which enables me to search for some text via an input box. I can then copy the relevant column and also copy columns A and B on the same worksheet. I then paste this information into a new worksheet named "SEARCH". I then loop through all of the additional worksheets and paste any further results to the right of any previously pasted columns.
All of the above is working fine.
What I would like to do is paste the information above into a new workbook rather than a sheet named "SEARCH"in the existing workbook.
Please find my code below
Option Explicit
Option Compare Text '< ignore case
'
Sub Searchcolumns()
'
Dim FirstAddress As String, WhatFor As String
Dim Cell As Range, Sheet As Worksheet
'
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
WhatFor = InputBox("What are you looking for?", "Search Criteria")
If WhatFor = Empty Then Exit Sub
'
For Each Sheet In Sheets
If Sheet.Name <> "SEARCH" Then
With Sheet.Rows(2)
Set Cell = .Find(WhatFor, LookIn:=xlValues, LookAt:=xlPart)
If Not Cell Is Nothing Then
FirstAddress = Cell.Address
Do
Sheet.Range("A1").EntireColumn.Copy Destination:=Sheets("SEARCH").Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1)
Sheet.Range("B1").EntireColumn.Copy Destination:=Sheets("SEARCH").Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1)
Cell.EntireColumn.Copy Destination:=Sheets("SEARCH").Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1)
Set Cell = .FindNext(Cell)
Loop Until Cell Is Nothing Or Cell.Address = FirstAddress
End If
End With
End If
Next Sheet
'
Set Cell = Nothing
'AutoFit All Columns on Worksheet
ThisWorkbook.Worksheets("Search").Cells.EntireColumn.AutoFit
End Sub
I have tried the following bit of code
Dim wkb As Workbook
Set wkb = Workbooks.Add ' Will add new workbook
but what that does is create a new workbook for every occurrence of the search term.
Many thanks
I am looking for a little bit of help. I have managed to write some code which enables me to search for some text via an input box. I can then copy the relevant column and also copy columns A and B on the same worksheet. I then paste this information into a new worksheet named "SEARCH". I then loop through all of the additional worksheets and paste any further results to the right of any previously pasted columns.
All of the above is working fine.
What I would like to do is paste the information above into a new workbook rather than a sheet named "SEARCH"in the existing workbook.
Please find my code below
Option Explicit
Option Compare Text '< ignore case
'
Sub Searchcolumns()
'
Dim FirstAddress As String, WhatFor As String
Dim Cell As Range, Sheet As Worksheet
'
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
WhatFor = InputBox("What are you looking for?", "Search Criteria")
If WhatFor = Empty Then Exit Sub
'
For Each Sheet In Sheets
If Sheet.Name <> "SEARCH" Then
With Sheet.Rows(2)
Set Cell = .Find(WhatFor, LookIn:=xlValues, LookAt:=xlPart)
If Not Cell Is Nothing Then
FirstAddress = Cell.Address
Do
Sheet.Range("A1").EntireColumn.Copy Destination:=Sheets("SEARCH").Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1)
Sheet.Range("B1").EntireColumn.Copy Destination:=Sheets("SEARCH").Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1)
Cell.EntireColumn.Copy Destination:=Sheets("SEARCH").Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1)
Set Cell = .FindNext(Cell)
Loop Until Cell Is Nothing Or Cell.Address = FirstAddress
End If
End With
End If
Next Sheet
'
Set Cell = Nothing
'AutoFit All Columns on Worksheet
ThisWorkbook.Worksheets("Search").Cells.EntireColumn.AutoFit
End Sub
I have tried the following bit of code
Dim wkb As Workbook
Set wkb = Workbooks.Add ' Will add new workbook
but what that does is create a new workbook for every occurrence of the search term.
Many thanks