Find multiple words and paste them in different sheets - excel

I am currently using the code below to find multiple values and then to copy/paste into another sheet. The purpose of this code is to retrieve a list of genes (small words) from huge data set.
This code is for one gene list (a list of small keywords) only, which is present in Sheet 3 Column A (beginning with A2). All the data retrieved from the huge data set (present in Sheet 1) is copied to Sheet 2.
Now I have 6 gene lists (6 lists of small keywords). I have to make 6 separate sheets for all these gene lists. Kindly tell me the code for this problem.
Sub OrderFinder()
Dim srchLen, gName, nxtRw As Integer
Dim g As Range
'Clear Sheet 2 and Copy Column Headings
Sheets(2).Cells.ClearContents
Sheets(1).Rows(1).Copy Destination:=Sheets(2).Rows(1)
'Determine length of Search Column from Sheet3
srchLen = Sheets(3).Range("A" & Rows.Count).End(xlUp).Row
'Loop through list in Sheet3, Column A. As each value is
'found in Sheet1, Column B, copy it too the next row in Sheet2
With Sheets(1).Columns("B")
For gName = 2 To srchLen
Set g = .Find(Sheets(3).Range("A" & gName), lookat:=xlWhole)
If Not g Is Nothing Then
nxtRw = Sheets(2).Range("B" & Rows.Count).End(xlUp).Row + 1
g.EntireRow.Copy Destination:=Sheets(2).Range("A" & nxtRw)
End If
Next
End With
End Sub

Related

Cut and paste using an offset

I am building a web scraping tool that obtains particular data. Once the data has been extracted the next step is to summarize it into a report thus i need some guidance on the final part of the project.
I have a column (Column A) that contains the following data set
Description of product
$3000
Description of product
$5000
etc
I would like to find a value (in this case the common value is $) and cut this value next to the description (into Column B). There could be hundreds of rows thus a loop would be required.
My initial thought is to use code that will find a value ($), then once the value is found, cut the row and using an offset paste the value (into column B)
Any help would be appreciated
sub test()
dim usedrows,i as integer
usedrows = activesheet.range("A" & activesheet.rows.count).end(xlup).row
for i=0 to usedrows
if instr(range("A" & i+1),"$") <> 0 then
'Checks if the looped cell has "$" sign
range("B" & i+1)=range("A" & i+1)
range("A" & i+1)=""
end if
next
end sub
Copy ColumnA into ColumB. Delete B1 with Shift cells up. Series fil1 a column with 1 in odd rows, 2 in even, then filter to select the 2s delete those rows and then the column of 1s.

Wildcard search on Excel

Im creating a macro in excel that has search function. so basically. i will put the list of servers in Sheet3 Column A and it will search the servers ing Sheet1 column A. Once done, it will display the result in Sheet 2 column A. However, im having issue in searching for wildcard. for example. i want to seach server "ABC123" but it will not display the result because it needs to be the whole name of the server "ABC123.def" can anyone help me with the codes?
Option Explicit
Sub HostNameFinder()
Dim srchLen, hName, nxtRw As String
Dim g As Range
'Clear Sheet 2 and Copy Column Headings
Sheets(2).Cells.ClearContents
Sheets(1).Rows(1).Copy Destination:=Sheets(2).Rows(1)
'Determine length of Search Column from Sheet3
srchLen = Sheets(3).Range("A" & Rows.Count).End(xlUp).Row
'Loop through list in Sheet3, Column A. As each value is 'found in Sheet1, Column A, copy it top the next row in Sheet2
With Sheets(1).Columns("A")
For hName = 2 To srchLen
Set g = .Find(Sheets(3).Range("A" & hName), lookat:=xlWhole)
If Not g Is Nothing Then
nxtRw = Sheets(2).Range("A" & Rows.Count).End(xlUp).Row + 1
g.EntireRow.Copy Destination:=Sheets(2).Range("A" & nxtRw)
End If
Next
End With
End Sub
If you fire up the macro recorder and perform a search for "ABC123" with the option to look in formulas and the option "Match entire cell contents" NOT selected, Excel will find the cell with the value ABC123.def
You could then study the code created by the macro recorder and would notice the difference to your code. You use lookat:=xlWhole, i.e. you're looking at the whole cell. Change it to LookAt:=xlPart. This is the equivalent of using a wildcard.
Or build the search term by wrapping it in a set of asterisks.
Set g = .Find("*" & Sheets(3).Range("A" & hName) & "*", lookat:=xlWhole)

Excel VBA Compare Three Excel List and Findout All Common Parts

I have a three excel files (A, B and C). And I have to compare A list to other lists to find if B and C list have same parts than A. Then have a separate list of parts that can be found list A and (B or C). Also it would be good if in the list have info Which one list (b or C) part have found.
In the lists column 1 is ID number and column 2 is part name.
Now I have tried following code:
Sub lookup()
Dim TotalRows As Long
Dim rng As Range
Dim i As Long
'Copy lookup values from sheet1 to sheet3
Sheets("Sheet1").Select
TotalRows = ActiveSheet.UsedRange.Rows.Count
Range("A1:B" & TotalRows).Copy Destination:=Sheets("Sheet3").Range("A1")
'Go to the destination sheet
Sheets("Sheet3").Select
For i = 1 To TotalRows
'Search for the value on sheet2
Set rng = Sheets("Sheet2").UsedRange.Find(Cells(i, 1), LookAt:=xlWhole)
'If it is found put its value on the destination sheet
If Not rng Is Nothing Then
Cells(i, 4).Value = rng.Value
End If
Next
End Sub
But it's not very good for me. Because now I have to copy B and C list in sheet 2 and then this code copies all A list to sheet 3 and next column what are common with sheet2.
And do I have to say there are thousands of parts.
Even easy way to comparing B to A list would be good.
So I really need your help.
Microsoft Query to the rescue (Data->From Other Sources->Microsoft Query).
Example for comparing Sheets A and B
Step 1: Add a Row Number column to sheets A and B
Step 2: Create the query:
SELECT SheetA.A, SheetB.B FROM [SheetA$] as SheetA,
LEFT OUTER JOIN [SheetB$] as SheetB ON SheetA.A = SheetB.B
What if they are in different files?
SELECT SheetA.A, SheetB.B FROM `C:\FileA.xlsx`.`SheetA$` as SheetA,
LEFT OUTER JOIN `C:\FileA.xlsx`.`SheetA$` as SheetB ON SheetA.A = SheetB.B
As always I recommend using my AddIn to create the queries: link or use the Wizard (Data->From Other Sources->Microsoft Query).

Macro to insert blank cells below if value >1 and copy/paste values from cell above

This site already has something similar: Copy and insert rows based off of values in a column
but the code doesn't take me quite where I need to go, and I haven't been able to tweak it to make it work for me.
My user has a worksheet with 4 columns, A-D. Column A contains specific contract numbers, column B is blank, column C has part numbers, and column D has the entire range of contract numbers. My user wants to count the number of times the entire range contract numbers has duplicates so I entered the formula =countif($D$2:$D$100000,A2) in cell E2 and copied down, giving me the number of times the specific contract in column A appears in column D. The numbers range from 1 to 11 in this workbook but the number may be higher in other workbooks this method will be used in.
The next thing I need to do is to enter blank cells below all values in column E that are greater than 1, very much like the example in the previously asked question. I then also need to copy in the same row and insert copied cells exactly to match in the same row in column A. Example: Cell E21 has the number 5 so I need to shift cells in column E only so that there are 4 blanks cells directly below it. In column A, I need to copy cell A21 and insert copied cells in four rows directly below.
Just trying to get the blank cells to insert has been a trial, using the code as given in the previous question.
Dim sh As Worksheet
Dim lo As ListObject
Dim rColumn As Range
Dim i As Long
Dim rws As Long
Set sh = ActiveSheet
Set lo = sh.ListObjects("Count")
Set rColumn = lo.ListColumns("Count").DataBodyRange
vTable = rColumn.Value
For i = rColumn.Rows.Count To 1 Step -1
If rColumn.Cells(i, 1) > 1 Then
rws = rColumn.Cells(i, 1) - 1
With rColumn.Rows(i)
.Offset(1, 0).Resize(rws, 1).Cells.Insert
.EntireRow.Copy .Offset(1, 0).Resize(rws, 1).Cells
.Offset(1, 0).Resize(rws, 1).EntireRow.Font.Strikethrough = True
End With
End If
Next
I would be very grateful for any help as I have been fighting with this monster for a week.
While this is indeed possible to do, it might be a good idea to look into moving the list of all contract numbers from column D to a different sheet. Even though it is quite simple to loop through a range and insert rows based on cell values - it'll also create holes in columns D and E.
Here's code for simply adding the rows and copying the values as you specified.
Sub Main()
'---Variables---
Dim source As Worksheet
Dim startRow As Integer
Dim num As Integer
Dim val As String
Dim i As Long
'---Customize---
Set source = ThisWorkbook.Sheets(1) 'The sheet with the data
startRow = 2 'The first row containing data
'---Logic---
i = startRow 'i acts as a row counter
Do While i <= source.Range("E" & source.Rows.Count).End(xlUp).Row
'looping until we hit the last row with a value in column E
num = source.Range("E" & i).Value 'Get number of appearances
val = source.Range("A" & i).Value 'Get the value
If num > 1 Then 'Number of appearances > 1
Do While num > 1 'Create rows
source.Range("A" & i + 1).EntireRow.Insert 'Insert row
source.Range("A" & i + 1) = val 'Set value
num = num - 1
i = i + 1 'Next row
Loop
End If
i = i + 1 'Next row
Loop
End Sub
Of course you could also remove the holes from column D after inserting the new rows and modify the formula in column E so that it remains copyable and doesn't calculate for the copied rows.
Generally it makes things easier if a single row can be thought of as a single object, as creating or deleting a row only affects that one single object. Here we have one row represent both a specific contract and a contract in the all contracts list - this could end up causing trouble later on (or it could be totally fine!)

Keeping first two instances in a column of duplicates in excel

I have a long list of items where some are duplicate identification numbers in one column. The records are not duplicates all across the spreadsheet. I am looking to extract the first two rows from the first two iterations of duplicate numbers when sorted by a different value (time/date).
I've seen topics on keeping the first instance of duplicate items, but not keeping the first two instances in records. I'm looking for a formula or vba.
Thanks
First sort your records so that the ones you want to keep are higher up the column.
Add a column where you'll put the formula (I'm assuming the first ID number is in cell A1):
=COUNTIF($A$1:A1, A1)
Drag the formula to the bottom of the table and copy/paste values in place to remove the formula.
Insert a filter and you can filter on only the results of 1 and 2 to get the first two instances of the ID numbers. Copy to a fresh spreadsheet to get only those in a sheet.
Here is a subroutine that should do what you are asking, you will need to alter it to your specific data as it assumes that columns A to G hold the data you want to extract and that column A has the duplicate data, column B holds the other data you want to sort by and that there are no empty cells in the data for column A.
Sub SortAndExctract()
Dim wsInputWorksheet As Worksheet
Dim wsOutputWorksheet As Worksheet
Dim lInputRowNumber As Long
Dim lOutputRowNumber As Long
Dim sLastExtract As Variant 'A variant as I don't know what type of value you are looking for
Dim iColumnCounter As Integer
'Sort the worksheet, assumes that the columns are in the range A:G and that you
'Want to sort according to column A and then column B
Range("A:G").Select
Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, _
Key2:=Range("B1"), Order2:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Set wsInputWorksheet = ThisWorkbook.ActiveSheet
Set wsOutputWorksheet = ThisWorkbook.Worksheets.Add
lInputRowNumber = 1
lOutputRowNumber = 1
'Until an empty cell is found check for duplicate values in column A
'Assumes that you don't have empty cells in column A within your data
'and that the duplicate values are in column A
Do While wsInputWorksheet.Cells(lInputRowNumber, 1).Value <> Empty
If wsInputWorksheet.Cells(lInputRowNumber, 1).Value <> sLastExtract Then
If wsInputWorksheet.Cells(lInputRowNumber, 1).Value = wsInputWorksheet.Cells(lInputRowNumber + 1, 1).Value Then
For iColumnCounter = 1 To 6 'Assuming againg that colum G is the last column
'copy cells to output worksheet
wsOutputWorksheet.Cells(lOutputRowNumber, iColumnCounter).Value = _
wsInputWorksheet.Cells(lInputRowNumber, iColumnCounter).Value
wsOutputWorksheet.Cells(lOutputRowNumber + 1, iColumnCounter).Value = _
wsInputWorksheet.Cells(lInputRowNumber + 1, iColumnCounter).Value
Next iColumnCounter
lInputRowNumber = lInputRowNumber + 1 'Will be incremented again later
lOutputRowNumber = lOutputRowNumber + 2
End If
End If
lInputRowNumber = lInputRowNumber + 1
Loop
End Sub

Resources