vba Entire Column should copy - vba

" find " cell value in header will keep changing in raw file, i need " find " cell value ENTIRE column should copy and paste in sheet2
Sub Macro3()
Cells.Find(What:="FSP Center", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate`
Cells.FindNext(After:=ActiveCell).Activate`
Columns("A:A").Select 'i want to select entire column
Selection.Copy
Sheets("Sheet2").Select
ActiveSheet.Paste
End Sub

Sub Macro3()
Dim f As Range
Set f = Rows(1).Find(What:="FSP Center", After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext)
If Not f Is Nothing Then
f.EntireColumn.Copy Sheets("Sheet2").Range("A1")
End If
End Sub
'fixed the misspelling "If"

Related

Searching and Selecting multiple data in Worksheets w/ common header

I just started using VBA for making my life easier, programming is not my background at all. When I run codes I may write too much.
So I have two questions, check the code below.
Sub Find()
'
' Find Macro
'
'
'L.NAM.O
Worksheets("LAC").Select
Cells.Select
Selection.Find(What:="forecast_quarter", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("NewForecast").Select
Range("K2").Select
ActiveSheet.Paste
'L.NAM.M
Worksheets("EMEA").Select
Cells.Select
Selection.Find(What:="forecast_quarter", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("NewForecast").Select
Range("K" & Rows.Count).End(xlUp).Offset(1).Select
ActiveSheet.Paste
I want to be able to find the forecast_quarter in both sheets(I have 3 in total) and paste in one Worksheet(New Forecast), one below other. The thing is, I think this is too much, might have an easier way than run all the process all over again.
My idea would be, "search the forecast_quarter quarter in the worksheets I want and paste on below the other). As I have all criterias to do that, this could me massive. Any easier, better way to run it?
Thanks!
Something like this (untested) should work.
Sub CopyAll()
CopyDataByHeader "LAC", "forecast_quarter"
CopyDataByHeader "EMEA", "forecast_quarter"
End Sub
'Look for a specific header on a sheet, and if found copy
' the data below it to "NewForecast" sheet
Sub CopyDataByHeader(shtName As String, hdrText As String)
Dim f As Range
With ActiveWorkbook.Sheets(shtName)
'search for the header
Set f = .Cells.Find(What:=hdrText, After:=.Cells(1), _
LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Not f Is Nothing Then
'found the header: copy the data below it
.Range(f.Offset(1, 0), .Cells(.Rows.Count, f.Column).End(xlUp)).Copy _
ActiveWorkbook.Sheets("NewForecast").Cells( _
Rows.Count, "K").End(xlUp).Offset(1, 0)
Else
'header not found...
MsgBox "Header text '" & hdrText & "' not found on sheet '" & shtName & "' !"
End If
End With
End Sub

Looking for multiple terms (sorted by priority)

Would it be possible to use the find method to search for back-up options?
Here's my code right now:
Set foundCell = Cells.Find(What:="RCP 1", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If Not foundCell Is Nothing Then
foundCell.Activate
foundCell.Rows("1:1").EntireRow.Select
Selection.Copy
Range("A" & (PLcount + 8)).Select
ActiveSheet.Paste
Else
Set foundCell = Cells.Find(What:="RCP- 1", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If Not foundCell Is Nothing Then
foundCell.Activate
foundCell.Rows("1:1").EntireRow.Select
Selection.Copy
Range("A" & (PLcount + 8)).Select
ActiveSheet.Paste
End If
End If
I would like to be able to do something like below. Note the text after .Find(What:=)
Set foundCell = Cells.Find(What:="RCP 1" "RCP- 1" "RCP 1", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If Not foundCell Is Nothing Then
foundCell.Activate
foundCell.Rows("1:1").EntireRow.Select
Selection.Copy
Range("A" & (PLcount + 8)).Select
ActiveSheet.Paste
End If
Where the first term is the first priority, the second term is the second priority, the third term is the third priority, etc.
EDIT - there is only limited support for wildcards in Find() - you would probably not class it as "regex-level" functionality:
* - zero or more characters
? - single character
~ - escapes * or ? if you want to find those literal characters
Alternatively can put the Find into a separate function:
Sub Tester()
Dim foundCell, PLCount As Long
PLCount = 3
Set foundCell = FindFirst(Cells, Array("RCP 1", "RCP- 1"))
If Not foundCell Is Nothing Then
'no need for any select/activate
foundCell.EntireRow.Copy Destination:=Range("A" & (PLCount + 8))
End If
End Sub
'return the first match to a value in the array "arrWhat"
' Returns Nothing if no match
Function FindFirst(rngWhere, arrWhat) As Range
Dim v, f As Range
For Each v In arrWhat
Set f = rngWhere.Find(what:=v, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not f Is Nothing Then Exit For
Next v
Set FindFirst = f
End Function

VBA - how to go about the automation of checking the inputs (newbie here)

Specifically what I would want to do is find a way to check if a certain input in Sheet1 cell, is also found in sheet2.
Not knowledgeable with VBA so I tried recording macro
Sheets("Sheet2").Select
Cells.Find(What:="asd", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
Now instead of string "asd", I want the input in sheet1, below
Sheets("Sheet1").Select
Range("B1").Select
I tried changing "asd" to input in sheet1,
Sheets("Sheet2").Select
Cells.Find(What:=
Sheets("Sheet1").Select
Range("B1").Select, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
but it's giving me an error. Any one can please help how to go about this or recommend a different approach to resolve my problem.
If you want to compare strings in different sheets, just create a new button and add fallowing code:
If Sheets("sheet1").Range("b1") = Sheets("sheet2").Range("b1") then
msgbox " string match"
Else
msgbox " string don't match"
End If
This code will compare cell b1 from sheet1 to cell b1 from sheet2
Try this:
Cells.Find(What:=ThisWorkbook.Sheets("Sheet1").Range("B1").Value, _
LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=True, SearchFormat:=False).Select
EDIT: As I told you in the comment
Dim MyCell as Range
Set MyCell = ThisWorkbook.Sheets("Sheet2").Cells.Find(What:=ThisWorkbook.Sheets("Sheet1").Range("B1").Value, _
LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=True, SearchFormat:=False)
'This will check if it is found or not
If MyCell Is Nothing Then
MsgBox "Did not find it"
Else
MsgBox "Found it"
End If
Use the Instr function
Dim pos As Integer
pos = InStr(Sheets("sheet2").Range("b1"), Sheets("sheet1").Range("b1"))
If pos<> 0 then
msgbox " string match"
Else
msgbox " string don't match"
End If

Excel VBA Highlight search

I'm trying to use a word in one cell, and then go to another sheet and find that word in another cell. The tricky part is that the second sheet has this word in many places, and I only want the cell that has the word highlighted in blue.
I've tried the following, but the loop keeps on passing over the blue highlighted word and continuing. What am I doing wrong?
Sub TryingIt()
Dim r As Excel.Range
Dim strName As String
Dim strFirstFound As String
strName = ActiveCell.Text
Sheets("Waiting For").Select
Range("A1").Select
Cells.Find(What:=strName, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Set r = ActiveCell
If r.Interior.color = vbBlue Then
r.Offset(1, 0).Select
Else:
Do
Cells.Find(What:=strName, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Set r = ActiveCell
Loop While r.Interior.color <> vbBlue
r.Offset(1, 0).Select
End If
End Sub
Here's the answer:
You have to be careful to be more specific about the color. In my case, it was RGB(0,176,240), not just vbBlue.

need be more dynamic when using search function in vba

I have the following code and is stuck when wondering how to make the search result more dynamic, that is after searching the "price", i need to copy the "price" and the cell at the right of it to cell A1, any help is appreciated.
Sub Macro1()
Cells.Find(What:="price", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Range("L14:M14").Select
Selection.Copy
Range("A1").Select
ActiveSheet.Paste
Range("A1").Select
End Sub
Sub Macro1()
Dim f as Range
Set f = Activesheet.Cells.Find(What:="price", After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:= False, SearchFormat:=False
if not f is nothing then
f.resize(1,2).copy Activesheet.Range("a1")
end if
End Sub