Find, Select and Copy multiples random cells VBA Macro - vba

i'm having troubles with a Macro that i can't figure it out how to do it, i need a Macro that can scan trough a workbook, Find 3 values, "Data: ", "N°" and "Rodovia:", make a offset of 1 column from them and select that value, and paste it in another sheet like this:
Data: | 10/03/2014
N°: | L02.020.22C
Rodovida: | GO-020
So it must select "10/03/2014", "L02.020.22C" and "GO-020"
I can do it using find and Resize i know, but they are not in the same row or column, they are located in random rows and columns, thats the problem, i tried to use a Range().Select with multiples .Find().offset() inside but it didn't worked
After that i need it to do it with all the cases in the workbook, so i need it to give me that
Case1Data|Case1N°|Case1Rodovia
Case2Data|Case2N°|Case2Rodovia
Case3Data|Case3N°|Case3Rodovia
UPDATE, Code so far:
Sub Gather_Values()
Dim Rng As Range
With Sheets("01").Range("A:AJ")
Set Rng = .Find(What:="Data: ", _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False).Offset(0, 1)
Selection.Copy
Sheets.Add.Name = "New"
Worksheets("New").Activate
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End With
End Sub

This subroutine will collate the 3 values for a single sheet named "01", searching the UsedRange of that sheet:
Sub Gather_Values()
Dim Rng As Range
Dim Sht As Worksheet
'create new worksheet, name it "New"
Set Sht = Sheets.Add
Sht.Name = "New"
'set column titles in the new sheet
Sht.Range("B1").Value = "Data"
Sht.Range("C1").Value = "N°"
Sht.Range("D1").Value = "Rodovia"
'search the entire UsedRange of sheet 01
With Sheets("01").UsedRange
Set Rng = .Find(What:="Data: ", _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False).Offset(0, 1)
Sht.Range("B2").Value = Rng.Value 'put value from the Find into B column of new sheet
Set Rng = .Find(What:="N°", _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False).Offset(0, 1)
Sht.Range("C2").Value = Rng.Value 'put value from the Find into C column of new sheet
Set Rng = .Find(What:="Rodovia:", _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False).Offset(0, 1)
Sht.Range("D2").Value = Rng.Value 'put value from the Find into D column of new sheet
End With
End Sub
If the values appear more than once on a single sheet you will need to determine either what ranges of cells will always contain separate records or come up with a different way of parsing the data to make sure the values you find in sequence are of the same "case" or "record".
It would be helpful to see a sampling of the raw data that you are parsing to see if there is a better way to collate it than using Find(). It would be best to see an instance where there is more than one "case" in the raw data.

Related

copy sheet content to another sheet

I want to copy the content of one sheet at the end of the other, i have tried this vba code and it works,
Private Sub CommandButton1_Click()
Sheets("B").Select
Range("A1:H14").Select
Range("A1:H14").Copy
Sheets("A").Select
' Find the last row of data
Range("B48:I48").Select
ActiveSheet.Paste
Sheets("A").Select
End Sub
but what i want is to copy without having to specify the range of the data, because i have many files and many data and it's gonna be hard to do all of that manually and change the range a each time.
Below will copy entire content in Sheet B to Sheet A
Sheets("B").Cells.Copy Destination:=Sheets("A").Range("A1")
You do not need to select cells while copying.
There's no need to use so many Select, which slows down the code, you can use the 1 line below will copy the entire contents of Sheet("B") to the first empty row at Column "A" in Sheet("A").
Dim Rng As Range
Dim lRow As Long
Dim lCol As Long
Dim lPasteRow As Long
With Sheets("B")
lRow = .Cells.Find(What:="*", _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
lCol = .Cells.Find(What:="*", _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
lPasteRow = Sheets("A").Cells.Find(What:="*", _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
.Range(.Cells(1, 1), .Cells(lRow, lCol)).Copy Destination:=Sheets("A").Range("A" & lPasteRow + 1)
End With
I usually do something like this, assuming sheet1 is the sheet to be updated by data from sheet2;
dim destLen as Long 'rows used in sheet 1
dim sourceLen as Long 'rows used in sheet 2
Open directory with source files and loop through each file and do the following
destLen = Sheet1.Range("A"&Rows.Count).End(xlUp).Row
sourceLen = Sheet2.Range("A"&Rows.Count).End(xlUp).Row
Sheet2.Range("B1" & ":I" & sourceLen).copy
Sheet1.Range("A" & destLen + 1).pasteSpecial xlValues
Application.CutCopyMode = False

Find a range in another Sheet

I have the piece of code below:
Selection.Find(What:="4", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Dim cl As Range, rTest As Range
Set rTest = Range("a1", Range("a1").End(xlToRight))
For Each cl In rTest
If Not cl.Value > 0 Then
cl.EntireColumn.Hidden = True
End If
Next cl
End Sub
Where says What=4, i would like to search the Range(e15) of another worksheet. Search the value of E15 in one sheet and look for it in a specific range in another sheet. I have all the other piece set, but I dont know how I can reference the value of e15, this can 4 or any other number. After finding, hide all columns that are not my specific value. Many thanks!
you should act like follows
Dim f As Range
Set f = Selection.Find(What:=Worksheets("otherWorksheetName").Range("e15").Value, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If Not f Is Nothing Then
f.Activate '<--| what do you need this for?
Range("A1", Range("A1").End(xlToRight)).EntireColumn.Hidden = True '<--| hide all columns in wanted range
f.EntireColumn.Hidden = True '<--| unhide found range column
End If
where you have to change "otherWorksheetName" to you actual "other " worksheet name

Editing code to only copy values and not formulas

I have some code that searches for the text I need copied but I realized that is carrying over formulas. I need it to simply paste the values of the cells. I am not sure how to edit the following to make that happen. Any thoughts?
With Initiatives.Range("B3:B500")
Rcount = 0
For I = LBound(MyArr) To UBound(MyArr)
Set Rng = .Find(What:=MyArr(I), _
After:=.Cells(.Cells.Count), _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
FirstAddress = Rng.Address
Do
Rcount = Rcount + 1
Rng.EntireRow.Copy NewSh.Rows(Rcount)
Set Rng = .FindNext(Rng)
Loop While Not Rng Is Nothing And Rng.Address <> FirstAddress
End If
Next I
End With
Try modifying the copy code to:
Rng.EntireRow.Copy
NewSh.Cells(RCount,1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
This gives you the option of copying the whole row, and chosing what method of paste you want.
Instead of coping the from a source one range to a destination range
Rng.EntireRow.Copy NewSh.Rows(Rcount)
Have the destination range value = the source ranges value. Just like filling a ranges values using an array.
NewSh.Rows(Rcount).Value = Rng.EntireRow.Value

How to copy the Data form the Specified address and copy it to the next specified location using excel vba

I have a worksheet which contains the details of the each product.
Here i have crested a button (ADD), by clicking on it i want to copy all the details of the CONTROL POWER TRANSFORMERS block and copy it to below (i mean copy it from B20).
I have written a code to pinpoint the CTPT (which is the unique id for that product) keeping it as a reference i have copied whole block till the row ends using the below code.
Set cF = .Find(what:="CTPT", _
lookat:=xlPart, searchorder:=xlByRows, searchdirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
num = cF.Address ' Here we will the get the cell address of CTPT ($A$14)
WsEPC.Range(cF.Offset(-1, 3), cF.Offset(-1, 1).End(xlDown)).Copy
Now while pasting the cell i need to do couple of things
I need to insert an row by finding the cell address of the clicked button
Paste the copied Data
Code any one help me out in achieving these couple of task.
Any help is Appreciated!
Sub test_Karthik()
Dim WbEPC As Workbook, _
WbCPT As Workbook, _
WsEPC As Worksheet, _
WsCPT As Worksheet, _
FirstAddress As String, _
WriteRow As Long, _
cF As Range, _
num As String
Set WbEPC = Workbooks("EPC 1.xlsx")
Set WbCPT = Workbooks("Control Power Transformers.xlsm")
Set WsEPC = WbEPC.Sheets("Sheet1")
Set WsCPT = WbCPT.Sheets("Sheet2")
With WsEPC
.Activate
With .Range("A1:A10000")
'First, define properly the Find method
Set cF = .Find(What:="CTPT", _
After:=ActiveCell, _
LookIn:=xlValues, _
Lookat:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
'If there is a result, keep looking with FindNext method
If Not cF Is Nothing Then
FirstAddress = cF.Address
Do
num = cF.Address ' Here we will the get the cell address of CTPT ($A$14)
WsEPC.Range(cF.Offset(0, 1).End(xlUp), cF.Offset(0, 3).End(xlDown)).Copy
WriteRow = WsCPT.Range("E" & WsCPT.Rows.Count).End(xlUp).Row + 1
WsCPT.Range("E" & WriteRow).PasteSpecial (xlPasteValues)
cF.EntireRow.Insert xlDown, False
Set cF = .FindNext(cF)
'Look until you find again the first result
Loop While Not cF Is Nothing And cF.Address <> FirstAddress
End If
End With
End With
End Sub
Private Sub CommandButton21_Click()
Dim WbEPC As Workbook, _
WbCPT As Workbook, _
WsEPC As Worksheet, _
WsCPT As Worksheet, _
FirstAddress As String, _
WriteRow As Long, _
cF As Range, _
num As String
Set WbEPC = Workbooks("EPC 1.xlsm")
Set WbCPT = Workbooks("Control Power Transformers.xlsm")
Set WsEPC = WbEPC.Sheets("Sheet1")
Set WsCPT = WbCPT.Sheets("Sheet1")
Dim b As Object, RowNumber As Integer
Set b = ActiveSheet.Shapes("CommandButton21")
With b.TopLeftCell
RowNumber = .Row
End With
Rows(RowNumber + 1 & ":" & RowNumber + 1).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
With WsEPC
.Activate
With .Range("A1:A10000")
Set cF = .Find(what:="CTPT", _
lookat:=xlPart, searchorder:=xlByRows, searchdirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
num = cF.Address ' Here we will the get the cell address of CTPT ($A$14)
WsEPC.Range(cF.Offset(-1, 3), cF.Offset(-1, 1).End(xlDown)).Copy
WsEPC.Range("B" & RowNumber + 1).Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
End With
End With
MsgBox " Successfully added the product to EPC"
End Sub

Excel VBA how vlookup or an alternative way of getting the dyanmic value

I spend the last month making a program that finds specified strings, copies the data underneath it, and builds a Rawdata table from it. Problem is, all it does is copies and pastes, so if the value were to change, the value in the Rawdata table would not. I'm familiar with the =vlookup function but I don't know how to use it in VBA, and I'm not even sure if that's the right function to use in this instance.
Here is the code for looking up a string and copying the data underneath it.
SearchString = "#Regimen"
Application.FindFormat.Clear
' loop through all sheets
For Each sh In ActiveWorkbook.Worksheets
' Find first instance on sheet
Set cl = sh.Cells.Find(What:=SearchString, _
After:=sh.Cells(1, 1), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If Not cl Is Nothing Then
' If found, remember location
FirstFound = cl.Address
' Dim RangeToCopy As Range, DestRow As Long
Set RangeToCopy = Range(cl.Offset(1, 0), cl.Offset(numRowsForProducts, 0))
RangeToCopy.Copy
DestRow = Sheets("Template").Range("B" & Rows.Count).End(xlUp).Row + 1
Sheets("Template").Range("B" & 3 + iCounter).PasteSpecial xlPasteValues
End If
Next
Tl:DR, I want copy a dynamic range from one Sheet and put it into another, so that when the original changes, it also changes in the other sheet.
I'm not sure where you want the destination row to start. You set destination row, but then use iCounter. I used iCounter as you did. Please let me know if you want it to paste at the bottom of the sheet.
SearchString = "#Regimen"
Application.FindFormat.Clear
' loop through all sheets
For Each sh In ActiveWorkbook.Worksheets
' Find first instance on sheet
Set cl = sh.Cells.Find(What:=SearchString, _
After:=sh.Cells(1, 1), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If Not cl Is Nothing Then
' If found, remember location
for i = 0 to numRowsForProducts - 1
Sheets("Template").Range("B" & 3 + iCounter + i).formula = "=" & sh.name & "!" & cl.offset(i,0).address
next
End If
Next