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)
Related
I want to write VBA code that will compare two columns from two different sheets.
I have data in Sheet1 column B and in Sheet2 column B.
The formula to compare both columns is in Sheet2: =B2=Sheet1!B2.
Could you please help me to write VBA code for the above formula.
I am not sure how to use the above formula in VBA code.
The basic code to compare is
If Sheet1.Range("B1").Value = Sheet2.Range("B1").Value Then
'Code to execute when criteria is met
Else
'Code to execute when criteria is not met
End If
The else part is optional and can be omitted if you don't need it
If you want to compare the full column there are a few ways to do it.
My favorite is following:
Dim iLastRow As Integer
iLastRow = Sheet1.Cells(Sheet1.Rows.Count, 2).End(xlUp) 'Gets the last row
For i = 1 To iLastRow 'Compares each row and executes the code if
If Sheet1.Range("B" & i).Value = Sheet2.Range("B" & i).Value Then
'Code to execute when criteria is met
Else
'Code to execute when criteria is not met
End If
Next i
If you want to compare the displayed/formatted text of the cell and not the value behind it use .Text instead of .Value (e.g. "10th Sep. 2019" instead of 43718)
I have a file where I need to delete certain columns. I then need to insert a column called 'positive values' and add a formula so that only the positive values from another column are picked up in this new column.
So far I have pieced together the following code to delete the columns I do not need, but I am stuck at how to insert a new column next to an existing column called "net" and then have this column only show the positive values from column net in the relevant cells.
Current code
Sub ArrayLoop()
Dim ColumnsToRemove As Variant
Dim vItem As Variant
Dim A As Range
Sheets("sheet 1").Select
ColumnsToRemove = Array("acronym", "valueusd", "value gbp")
For Each vItem In ColumnsToRemove
Set A = Rows(8).Find(What:=vItem, LookIn:=xlValues, lookat:=xlPart)
Debug.Print vItem, Not A Is Nothing
If Not A Is Nothing Then A.EntireColumn.Delete
Next
End Sub
Currently I manually insert the new column and enter the formula max(E9,0) so the new column either shows 0 or a value if the value in the other column is greater than 0. Is it possible to automate this part as well.
Thanks in advance.
For Insertion locate the cell and issue:
If Not A Is Nothing Then A.EntireColumn.Insert
To insert a formula, use cell.formula= with the coresponding string value, e.g.
Cells(1, A.column - 1).Formula = "=max(" & cells(9, A.column - 2).Address & ",0)"
Note A as a range of the found value will shift to the right when inserting a column that's why you need - 1 nad - 2 in cell references.
The macro I am using opens an input box where you can enter what you are searching for and when it finds the value it highlights the entire row. The only problem is that it searches the entire sheet and not just one column. I want it to only search in Column B otherwise the search is pointless as it picks up values matching in other columns.
Here is the code I am using.
Sub Seroquel_25000_1mod1()
Dim SrchRng3 As Range
Dim c3 As Range, f As String
Set SrchRng3 = ActiveSheet.Range("B8", ActiveSheet.Range("B65536").End(xlUp))
AGAIN:
srchItem = InputBox("Please Enter the product CODE", "Product Search")
Set c3 = SrchRng3.Find(srchItem, LookIn:=xlValues)
If Not c3 Is Nothing Then
f = c3.Address
With Range("B" & c3.Row & ":M" & c3.Row)
.Select
End With
End If
End Sub
Because you are using a table, you will want to use the table name and field name to specify the search range:
Set SrchRng3 = ActiveSheet.Range("tablename[fieldname]")
If you don't know the table name, one way to find it is by clicking any cell in the table and then looking at the Design tab. The field name is the text of the header cell above that column in the table. Here is a decent reference on how to refer to tables and table data in VBA.
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
I have multiple spreadsheets that I need to copy column starting at "S4", on a specifically named worksheet, and I need only the cells in that column (starting at S4 and everything below it) that contain data.
I need to copy that data and paste it into my "main" spreadsheet starting at A2 on a specific worksheet. I can do this with one spreadsheet, but the problem I'm running into I need VBA to find the last cell in column A that has a value and start pasting new data into the cell below it, etc... Otherwise, when it's looking at the other spreadsheets, it's just overwriting the data in my main spreadsheet.
You'll notice the specified range of S4:S2000 - its purpose was for a single spreadsheet, which worked fine because I never had data past 2000, but I really just need it looking for cell values and grabbing those.
This is the chunk of code where I'm having the trouble. I want it to search starting at A2 (skipping the column header), look for the last cell that has a value and paste cells with values starting at S4 on the other worksheet.
On Error Resume Next
Set wbkCS = Workbooks.Open(strCutSheetFile(i))
On Error GoTo 0
If Not wbkCS Is Nothing Then
With wbkVer.Sheets("Cutsheets")
.Range(.Cells(2,1)).End(xlUp).Row = wbkCS.Worksheets("Cut Sheet").Range("S4:S2000").Value
I had to tweak yours a little because I'm working with protected files, but this worked:
Set copyRng1 = Worksheets("Cutsheets").Range("A2")
If copyRng1 = "" Then
wbkCS.Worksheets("Cut Sheet").Range("S4:S2000").Locked = False
wbkCS.Worksheets("Cut Sheet").Range("S4:S2000").Copy Destination:=wbkVer.Worksheets("Cutsheets").Range("A2")
Else
wbkCS.Worksheets("Cut Sheet").Range("S4:S2000").Copy Destination:=wbkVer.Worksheets("Cutsheets").Range("A" & wbkVer.Worksheets("Cutsheets").Range("A65536").End(xlUp).Row + 1)
End If
Here's an example that may push you in the right direction...
Suppose I want to aggregate my data in Sheet1 using data from S4:S2000 in all other worksheets in the workbook.
Sub CopyAndStack()
Dim wkShtIndex As Integer, copyRng As Range
Set copyRng = Worksheets(1).Range("A2")
For wkShtIndex = 2 To Worksheets.Count
If copyRng = "" Then
Worksheets(wkShtIndex).Range("S4:S2000").Copy Destination:=copyRng
Else
Worksheets(wkShtIndex).Range("S4:S2000").Copy Destination:=Range("A" & copyRng.End(xlDown).Row + 1)
End If
Next wkShtIndex
End Sub
I check if A2 is empty and if so I paste the first lot of data.
If A2 is not empty I get the next empty cell in column A and paste it there.
Here's how you can get exactly the rows that are populated in S:
Dim StrRange As String
. . .
'Get range
strRange = "S4:S" & Worksheets(wkShtIndex).UsedRange.Columns("S:S").Rows.Count
'Do something with range
Worksheets(wkShtIndex).Range(strRange)