I would like to build an excel VBA program to find all the match values from a table and print the finding on a column... I tried to use vlookup but only gives me the first location that finds... and I would like all the finds.
here is an example:
my table starts on cell A2
column A (number)
0084
0084
0085
0085
0086
0087
column B (location)
12AC5
16AC5
02AC5
06AC5
01DC5
11DC5
on column D I paste the values I am looking for there location..... on column F would give me all the location found that matches my request... in ascending order or any order
for example I am looking for the location of 2 numbers:
column D (I paste the number that I what to see there location
0084
0087
I expect to see the results:
column F(found location)
12ac5
12bd5
16ac5
Any help with this would be very much appreciated..
Cheers,
JFFC
excel VBA
enter code here
Clarification required. Are you:
Wanting to search for multiple values (in column A), return all the corresponding values(in column B) into column F?
An example of this is:
A, B, D, F
1,loc1,1,loc1
1,loc2,2,loc2
2,loc3, ,loc3
3,loc4, ,
3,loc5, ,
4,loc6, ,
Showing that if you serach for 1 and 2 (in column D) you get back Loc1, loc2 and loc3 because these are all the locations that match the numbers 1 and 2?
If this isnt what u are trying to do then i am confused
Macro to achieve this:
Sub FindLocations()
Dim ValueCell As Range
Dim SearchCell As Range
Dim ResultCounter As Integer
Dim ValueCol, LocCol, SearchCol, ResultCol As Integer
ValueCol = 1 'Look for the values in this column'
LocCol = 2 'Locations are in this column'
SearchCol = 4 'The search values are in this column'
ResultCol = 6 'Spit out the results in this column'
ResultCounter = 2
For Each ValueCell In Range(Cells(2, ValueCol), Cells(ActiveSheet.UsedRange.Rows.Count, ValueCol))
For Each SearchCell In Range(Cells(2, SearchCol), Cells(ActiveSheet.UsedRange.Rows.Count, SearchCol))
If SearchCell.Value = ValueCell.Value Then
Cells(ResultCounter, ResultCol).Value = Cells(ValueCell.Row, LocCol).Value
ResultCounter = ResultCounter + 1
Exit For
End If
Next
Next
End Sub
Paste that into your Excel VBA screen (Alt+F11) and run it (or link it to a button) Whenever you want to search click it
The results of this look like this:
Excel Example http://img97.imageshack.us/img97/9310/excelexample.jpg
Related
I have two sheets :
Sheet 1 consist of :
Sheet 2 consist of :
And the output should show in M column in Sheet1. I am attaching the sample output here :
So,what I have here is ID in Sheet 1, for eg : ID 'US' has Abhay,Carl and Dev
and in Sheet3, I have names in column and ID in Rows.
What i want is my Sample output column should populate using macro based on matched values from Sheet3
I am using below logic but something is going wrong :
For i = 2 To 10
j = i + 1
If ThisWorkbook.Sheets("Input").Range("N" & i) = ThisWorkbook.Sheets("Sheet3").Range("A" & i) And ThisWorkbook.Sheets("Input").Range("K" & i) = ThisWorkbook.Sheets("Sheet3").Range("B1") Then
ThisWorkbook.Sheets("Input").Range("O" & i) = ThisWorkbook.Sheets("Sheet3").Range("B" & j)
End If
Next i
Since you asked for a VBA solution, please see the code below.
Dim colLen As Integer
Dim i As Integer
Dim colPt As Integer
Dim rowPt As Integer
' Counts number of rows on Sheet 1, column B.
colLen = Sheets(1).Cells(Rows.Count, "B").End(xlUp).Row
' Loops through all names on Sheet 1.
For i = 2 To colLen
' Retain US or NA ID for blank cells.
If Sheets(1).Cells(i, 1) <> "" Then
If Sheets(1).Cells(i, 1) = "US" Then
colPt = 2
Else
colPt = 3
End If
End If
' Find name on Sheet 2 and set row.
rowPt = Sheets(2).Range("A:A").Find(Sheets(1).Cells(i, 2)).Row
' Add ID from Sheet 2 to Sheet 3
Sheets(1).Cells(i, 3) = Sheets(2).Cells(rowPt, colPt)
Next i
Assumptions:
Sheet 1 is the main worksheet, sheet 2 has the lookup data.
All names in the lookup data are unique.
I would recommend including the ID in every row instead of treating it as a heading but that's preference. There are formula solutions that would work for this as well if you want to skip VBA.
There are a few ways to approach this. Below is one of them:
NOTE: for simplicity, I have kept my data on one sheet. You can amend the below formulas as your data is on 2 sheets. Saying that, I have used the same columns as you have in your query
Solution:
Have a "holding column". In my example, I used column J as the holding column (you can hide this column if you want). In J2, type the following formula: =IF(ISBLANK($K2), $J1,$K2). Copy the formula down to all used rows. Then copy the following formula in M2: =VLOOKUP($L2,$A$3:$C$8,IF($J2="US",2,3),FALSE). As per before, copy the formula down to all used rows. This should give you your results
I have a spreadsheet in which there are multiple rows that have three columns (K, L M) that contain text (inserted manually from a dropdown). The inserted text includes a 'score'. For the row shown in the image that score is 3 + 2 + 2 = 7.
What I'd like to be able to do is to have that score automatically calculated and shown in column N. I'm happy to do the score extraction given the text, but I'm completey unfamiliar with Excel's object model, and how to write a VBA macro that can be triggered across all of the rows. I assume it would be passed a range somehow, or a string designating a range, but how to do that is beyond me just now. Perhaps I just need a formula? But one that calls a function to strip non-numerical data from the cell?
Any help appreciated.
Put this formula in N2 cell and drag it all the way down.
=LEFT(K2, FIND("-", K2) - 2) + LEFT(L2, FIND("-", L2) - 2) + LEFT(M2, FIND("-", M2) - 2)
For more information see reference. It sum all numbers, that are present before the hyphen (-) in a cell.
Try:
N2 = LEFT(TRIM(K2),1) + LEFT(TRIM(L2),1) + LEFT(TRIM(M2),1)
As I said in comments, this solution does not scale so well if it is more than three columns and / or the scores are more than single digit [0-9]
A VBA solution to do all of your rows and enter the values into Column N:
Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
LastRow = ws.Cells(ws.Rows.Count, "K").End(xlUp).Row
'get the last row with data on Column A
For rownumber = 1 To LastRow 'loop through rows
For i = 11 To 13 'loop through columns
strValue = ws.Cells(rownumber, i).Value 'get text string from cell
pos = InStr(strValue, " -") 'find the dash - in cell
If pos > 0 Then 'if dash found
Value = Value + Val(Left(ws.Cells(rownumber, i).Value, pos - 1)) 'remove everything after number
End If
Next i
ws.Cells(rownumber, 14).Value = Value 'write value to column N
Value = 0
Next rownumber
End Sub
My other macro scrapes mutual fund sizes from web and returns the values in Worksheets("Updater").Cells(xx,4) (so column C). The macro scrapes also the date in which the fund has been updated on morningstar and returns it in column F on the same sheet. I've got 83 fund names in column A.
On a sheet called "Tracker" I've got the fund names transposed. They start from cell (1,4) and go to (1,86). In column B I've got DATEVALUE of dates in column C. There're some issues with the date formats so I'll just hide the column B later.
The below macro works as it is, but when you uncomment lines 11 and 22, it doesn't work as intended anymore. Idea is to look at the fund sizes on sheet "Updater" and return the fund size to the corresponding date on sheet "Tracker". As said, this works.
However, with the nested if statement, I'd want to modify the macro so it would keep all the cells that contain other than "-" untouched. This way I would start to cumulate the fund size data when the macro is executed on a daily basis and doesn't overwrite the old fund sizes with "-".
Now the macro flips the funds who knows where and I simply can't solve this nested if statement problem. Is this even the correct/best way to get where I want?
Sub fundSizeTracker()
Dim f As Integer
Dim numRows As Integer
numRows = 86
f = 2 'Sheet fund names & sizes
For m = 2 To 4 'Sheet row number, starting at rowNo 2
For i = 4 To numRows 'Sheet column number (set to include all 83 funds)
' If Worksheets("Tracker").Cells(m, i) = "-" Then
' Are Datevalues same? 'Tracker datevalue 'Updater datevalue
If StrComp(Worksheets("Tracker").Cells(m, 2).Value, Worksheets("Updater").Cells(f, 13).Value) = 0 Then
Worksheets("Tracker").Cells(m, i) = Worksheets("Updater").Cells(f, 4).Value
Else
Worksheets("Tracker").Cells(m, i) = "-"
End If
If f = numRows - 1 Then
f = 2
End If
f = f + 1
' End If
Next i
Next m
End Sub
I have a list of usernames sorted alphabetically in column A, with some appearing numerous times.
I want to prnt the username in column B if it appears less than or equal to 4 times.
Do I need an array to go through all the different username values in the column to find the ones that appear less than or equal to 4 times?
Consider:
Sub dural()
Dim A As Range, B As Range, v As String, K As Long
Set A = Intersect(Range("A:A"), ActiveSheet.UsedRange)
Set B = Range("B:B")
K = 1
With Application.WorksheetFunction
For Each aa In A
v = aa.Value
If v <> "" Then
If .CountIf(A, v) <= 4 Then
If .CountIf(B, v) = 0 Then
Cells(K, "B").Value = v
K = K + 1
End If
End If
End If
Next aa
End With
End Sub
Add a helper column and place the following formula in the second row:
=IF(AND(COUNTIF(A:A,A2)<=4,COUNTIF($A$2:A2,A2)=1),MAX($B$1:B1)+1,"")
And copy down:
At this point you can filter on the non Blank Cells and copy them to another range.
If you want to use a formula to get the list then Put this in another column row 2:
=IFERROR(INDEX(A:A,MATCH(ROW(1:1),B:B,0)),"")
And copy down.
No need for helper columns or VBA, just a bit of finely-tuned IF functions :)
=IF(COUNTIFS(BE:BE,BE2)<=4,IF(COUNTIFS($BE$1:BE2,BE2)=1,BE2,"0"),"0")
^Here, BE is where all your data is, starting on Row 2
What it does:
If the Name appears 4 or fewer times,
If this is the first time the Name appears in the column
Print the Name
(Otherwise insert 0)
To remove the 0 values i.e empty rows:
Paste over the formula with the same column as values (this is so you can..)
.. Replace All (Ctrl-H) "0"s with nothing "" (so that you can..)
.. Select the blank rows using Go To (Ctrl-G) > Special > Blanks
Delete (Shift Cells Up)
You can also simply Filter out the Blank/0 values
I am trying to add category codes to text categories in an excel spreadsheet. I have a column A with all the category names in text, including duplicates. I have column B with all the category names in text, not including duplicates and I have column C which has the category codes not including duplicates. Each code in column C alligns with its category in column B
For example:
A:
Domestic Ware
B:
Agri Tools
C:
051
051 is the code for agri tools. I want to take the string in column A, Search for this string in column B and then replace the string that was in column A with the code in C.
Is there anyway to do this using VBA or excels built in functions?
Since you want to replace values in column A, give this small macro a try:
Sub FixColumnA()
Dim nA As Long, nB As Long, v As Variant
Dim i As Long, j As Long
nA = Cells(Rows.Count, "A").End(xlUp).Row
nB = Cells(Rows.Count, "B").End(xlUp).Row
For i = 1 To nA
With Cells(i, "A")
v = .Value
For j = 1 To nB
If v = Cells(j, "B").Value Then
.Value = Cells(j, "C").Value
End If
Next j
End With
Next i
End Sub
If column A contains a list of Category names including duplicates, and columns B & C contain (the same) category names and Category ID's without duplicates (i.e. a "category code table"), you can
in column D (or maybe better insert a new column between A and B)
for each entry in A you enter =VLOOKUP(Ax,$B$y:$C$z,2,FALSE)
whereby
x = current row number
y = starting row of "category code table"
z = end row of "category code table"
You make the coord's of the lookup table absolute to prevent y & z being changed as you copy the formula down.