I have 2 columns. First contains 10,15,30,45,35,1,2,3,1000,,,and so on. The 2nd column contains 86,55,,,,and so on. The first combination should be 30,55 & 1 for 86, and the next result should be 10 & 45 for 55 . But I want to get their index instead of 30,55 & 1, Please help..(I have seen similar to this but in C# not in VBA. I want in excel VBA)
Related
I am a beginner VBA coder and have been trying to figure out how to work a loop within my spreadsheet. I am attempting to declutter a collection of loan information I have, but just as a basic framework for the specifics of my question, I have generated the following random numbers in Column ("A:A") in the first Worksheet of the Workbook:
5
241
4
5
648
87
65
74
52
1
The loop I'm using is:
Sub LoopTest()
Dim i As Integer
For i = 1 To 10
Range(i,1).Select.Copy Destination:=Range(i,5)
Next i
So essentially I am trying to copy the first cell in Row 1, Column 1 and paste it to Row 1, Column 5 and then loop to Row 2, Column 1 and paste it to Row 2, Column 5, etc. etc. until I loop through Row 10. Preferably, I would like to loop from the first row data to whatever is the last row of data (and I'm aware that is possible) but I am unaware how to do so.
Thanks.
I am unsure why you would need to do this by row. Couldn't you copy A1:A:10 and paste it to E1:E10 instead? This would keep you from having to loop. If you were to copy then you could do it with a Range.Copy call:
Range("A1").Copy Range("E1")
This copies A1 to E1.
I have an excel file which contain information of the composite in 1 row and in below rows information of the components of this composite. Number of component rows below a composite are varied between 2 - 20 and there can be many composites in a file.
My question is: is it possible somehow to define how many rows are in the components and to extract information from each component in to one cell(concatenate). Problem I face is that number of rows are different each time and there can be multiple composites in the file containing components. So i do not know how to stop my loop and start a new composite aggregation.
Maybe there are ways to loop from Request1(ColumnA) and assign "Request1" as a text to every empty column below until it reaches Request2, after that is finished to concatenate based on Request"n"
Example what i want the data to look like
~~~~~~~~~~~~EDIT~~~~~~~~~~~~~~~~~~~~
I might have over complicated my question
I was just looking to concatenate information from different set of rows(for simplicity just 1 consistent cell from every row) in to 1 cell(for example last cell in the first column) for each specific composite(which contains components) My problem is I do not know how to stop the concatenation and start a new one when i am working with a new composite(new set of rows).
So as an example from the first picture, I would like to have "Request 1 Green Yellow White" (cells: A1, F1, F2,F3) populated in cell J1, and "Request 2 Amber Red White Blue" (cells: A4,F4,F5,F6,F7)populated i cell J4
#######EDIT
I have established another way of doing but still struggle with concatenation formula.
In this picture example
https://i.stack.imgur.com/iQdNu.jpg
If my table stars from row 2
=IF(A2="",J1,A2) - by putting this in column J and dragging down i will get his Request 1
Request 1
Request 1
Request 2
Request 2
Request 2
Request 2
Then deleting duplicates i will be left only with
Request 1
Request 2
Then I can concatenate columns i want going by Request 1 or request 2 criteria(index match), but I cant figure out how to do it...
You can use array formula to work out the start and end rows, like =SMALL(IF($A$2:$A$20<>"",ROW($A$2:$A$20)),ROW()) to find the next populated cell in A1:A20, where this would be in the cell G1. So in G1, I have a fixed 1, then in G2 down, I have =H1+1, then in each H filled down I have =SMALL(IF($A$2:$A$20<>"",ROW($A$2:$A$20)),ROW()) this gives the following
Unfortunately we cant do the concat using what we have in Excel, so this will help with your loop start and ends. Number of products, is the difference in the 2
If I am reading your question correctly, then the following code may help. You want to be able to add element rows beneath categories rows, and when you do that it changes the row number for every row beneath the new row. This code will show you that it doesn't matter which row the category is on because you can find it's row number any time, and also the number of elements beneath it.
The trick is to add a word in col A of each category that will not be found in any element A value. For example, A1 might read "Category: Apples"," and there may be ten element rows under "Category: Apples" And then under those rows another category in col A will be "Category: Bananas." The code below looks for the value "Category:" in col A and gets the row number of each category line and how many elements are under it. With a little math you can figure out where to insert a new line for a new element row or what row to concatenate. And you won't need to hard code the rows numbers of the category. Just run this simple code and it will give you those row numbers to get and concatenate all rows beneath any category.
Sub findCategoryRows()
Dim lastRowColA As Long, myArray() As Variant
Dim rowOfCategoryNameArray, nameOfCategoryNameArray, categoryCounter As Long
lastRowColA = ActiveSheet.Range("A65536").End(xlUp).Row
myArray = Range("A1:A" & lastRowColA)
categoryCounter = 1
ReDim rowOfCategoryNameArray(1 To 1)
ReDim nameOfCategoryNameArray(1 To 1)
For i = 1 To UBound(myArray)
If InStr(1, Range("A" & i).Value, "Category: ") Then
rowOfCategoryNameArray(categoryCounter) = i
nameOfCategoryNameArray(categoryCounter) = Range("A" & i).Value
categoryCounter = categoryCounter + 1
ReDim Preserve rowOfCategoryNameArray(1 To categoryCounter)
ReDim Preserve nameOfCategoryNameArray(1 To categoryCounter)
End If
Next i
For i = 1 To UBound(rowOfCategoryNameArray) - 1
If i <> UBound(rowOfCategoryNameArray) - 1 Then
Debug.Print nameOfCategoryNameArray(i) & " has " & (rowOfCategoryNameArray(i + 1) - rowOfCategoryNameArray(i)) - 1 & " element rows under it."
Else
Debug.Print nameOfCategoryNameArray(i) & " has " & (lastRowColA - rowOfCategoryNameArray(i)) & " element rows under it."
End If
Next i
End Sub
I have been trying to compare two sheets. The sheets are versions, one made in August, the other in September. In Sheet1, column C I have a unique ID that could also be in sheet 2, but could also be not present. On the other hand, I could have NEW ID's in sheet 2, that are not present in Sheet1.
I am trying to:
Identify IDs not in "other" sheet, copy entire row to sheet3
Check if C-column value exists in other sheet, then it has to find
the differences in THOSE two rows, 12 columns out
Example, in sheet1:
ID Jan Feb Mar Apr May
14578596 125 125 125 0 10
22345697 10 10 10 10 20
12563654 150 150 75 75 75
85745896 890 890 890 890 790
and in sheet 2:
ID Jan Feb Mar Apr May
14578596 125 125 125 0 10
12563654 150 150 75 75 75
85745896 890 890 790 890 790
87544545 0 0 0 0 10
In sheet 3, it should copy over the newly added ID 87544545 and all the values in the following columns. It should copy over the ID's 22345697 entire row as well, as being non-existing in the other sheet is considered a difference.
For the others, that exist in both sheets, it should Take "Jan-Jan" and return the difference value. So it should lookup if "ID" exists in other sheet, if it does, compare the Jan-Feb-Mar with each other. Note that ID's are NOT in the same position in the sheets. With ID 85745896 it would return:
ID Jan Feb Mar Apr May
85745896 0 0 100 0 0
I have tried to look at topics such as
Compare data from 2 sheets and find mismatches
and
Check if two rows are the EXACT SAME in MS Excel but can't seem to make them work for my challenge here.
Sub compare()
For i = 1 To last_cell_mainSheet
For j = 1 To last_cell_sheet2
If Worksheets("main_sheet").Range("a" & i).Value = Worksheets("sheet2").Range("a" & j).Value
Then
Worksheets("main_sheet").Range("C" & i).Value = Worksheets("sheet2").Range("b" & j).Value
End If
Next j
Next i
End Sub
The easiest way to solve this is with excel formulas. (if you only want to do it once, or occasionally.) If you need to repeat if often (or have massive amounts of data) use VBA>
You will need to find out about VLOOKUP and IF and ISNA.
Essentially you can add a column to both sheets that looks up the ID in the other sheet and returns Y or N if it is found or not.
You formaula will be something like:
=IF(ISNA(VLOOKUP(MyIDCell,TheTableInTheOtherSheet, 1, false)),"N","Y")
TheTableInTheOtherSheet is a range starting with the ID column (and only neededing one column)
Do the Jan-Jan bit.
Again use a vlookup formula column on sheet1 to get the value of Jan from sheet2. The add a formula to compare them.
You formula will be something like:
=VLOOKUP(MyIDCell,TheTableInTheOtherSheet, 2, false)
TheTableInTheOtherSheet is a range starting with the ID column (and two columns wide)
2 get the second column value
Once you have the data you can sort or filter to reduce the list ot thsoe you need to copy to sheet 3 (best to sort). Then copy and paste them.
I am trying to create a vba script that can find numbers greater than 5, 10 & 15 in a specific column then take count of them i.e. how many numbers are greater than 5 (let's say and so for 10 and 15 as well).
Sample Output (needed):
Gt5 Gt10 Gt20
23 34 90
I am writing a macro in excel and I have a 10 by 10 grid that I need to input a vlookup equation into. So for the first column of data I wrote a for loop
For i = 2 to 11
Cells (i, 30) = "=vlookup (E2" & i & ", B:C, 2, False)"
Next i
And it works the way I want it. Now I would like to write an outer for loop that will allow me to go across the columns.
I started with
For j = 30 to 39
For i = 2 to 11
Cells (i, 30) = "=vlookup (E" & i & ", B:C, 2, False)"
Next i
Next j
But I need E to change to F and I can't figure out how to do that.
I also tried using the Range (Cells(__,__),Cells(__,__)).FormulaR1C1 =...
But I couldn't get that to work either, since it is moving the table as well as fixing E2 across the columns.
If anyone can help I would greatly appreciate it. If not I will just write 10 for loops one for each column.
I think all you need to do is create the first formula and add "$" to appropriately anchor the reference rows/columns at which point you can simply copy the formula across the desired rows/columns.
here is a quick synopsis of anchoring: http://excelonthegrid.wordpress.com/2012/09/26/anchor-with-dollars/
i can give you the final answers if you post the desired formula for
a) column 1 row 1
b) column 1 row 2
c) column 2 row 1
d) column 2 row 2