Using VBA to create a grid from 3 spreadsheets with the row, column, and values - vba

I have three excel spreadsheets. The first has the values that are to be assigned to a new excel spreadsheet. The second has the column that the data belongs in. The third has the row that the data belongs in.
It looks something like this:
Data Value:
1 5 7 9
2 2 6 8
Column Number:
1 2 3 1
2 3 1 2
Row Number:
1 2 3 2
4 4 3 1
How can I combine all of this information to create a single spreadsheet that contains the values in a format like this:
Column
1 2 3
Row
1 1 8 0
2 9 5 0
3 6 0 7
4 0 2 2
I have tried to do it by using loops in vba, but I am a beginner and I am having some difficulty.
I know that I need to use a loop that checks the row and column that the data is supposed to be in against the row and column for each iteration. I am just not sure how to go about doing that.

Assuming these are different sheets in the same workbook (if not - create a new workbook and copy the sheets over). I assume that on each sheet, the same range of cells is used (for example A1:D2 in all 3 cases with the values on the first sheet, the column numbers in the second and the row numbers in the third). You can dispense with VBA entirely at the expense of using some complicate formulas (inspired by this excellent article: http://exceluser.com/blog/1043/how-to-create-two-dimensional-lookups-in-excel-formulas.html )
Step 1. Add a fourth sheet and in A1 add the formula
=CONCATENATE(Sheet3!A1,"_",Sheet2!A1)
and copy it over the appropriate range (e.g. A1:D2). This will give you things like 2_1 which tell you that the corresponding entry in sheet 1 belongs in row 2 column 1. Name this range "location" (formula tab - define names option)
Step 2 - Decide where you want to hold the data (I'm assuming it is in sheet 4 for simplicity) And add the row numbers (1-4) and the column numbers (1-3) as labels. In my case the row labels are in A5:A8 and the column labels in B4:C4 (see the screenshot below). Then in the upper left corner of the values to be filled in (B5 in my case) enter the following formula (suitably adjusted to match your ranges):
=IFERROR(INDIRECT(ADDRESS(SUMPRODUCT(ROW(location)*(location = CONCATENATE($A5, "_",B$4))),SUMPRODUCT(COLUMN(location)*(location = CONCATENATE($A5, "_",B$4))),,,"Sheet1")),0)
and copy it over the intended range. Be careful with the dollar signs - this formula mixes row absolute and column absolute references in an essential way. Somewhat oddly, it actually works:

Related

VBA Excel: How to add items for duplicates data in range excel with vba

I want to use VBA to insert items in ITEM column with data source is in Number column like below, count number of items and add number from 1 to number counts. can you help me?
Number Item
24811395 1
24811396 1
24811401 1
24811401 2
24811402 1
24811408 1
24811408 2
24811408 3
24811408 4
Use the formula in B2 as in image below and drag throughout the range,
=COUNTIF($A$2:$A2,A2)

How to compare a list of rows to another list of rows in Excel?

I am trying to figure out if there are any differences between a list of data with another. In order for a row of data to "match" with another row, the row must have the same values in their corresponding column. The rows themselves do not have to be in any particular order. In particular, I am dealing with a parts list, where there are part numbers, descriptions, etc. I am trying to figure out if any rows of data are different from rows of data from another list.
I found Compare two sheets using arrays, which may have the answer to my problem, but I am having trouble figuring out how to adapt to my code due to inexperience in Visual Basic.
I was able to get it to work for a single column of data, comparing one column of data from one sheet to another, but cannot get it to compare entire rows of data.
Here is an example of I want this to work:
Sheet 1 Sheet 2
Column 1 Column 2 Column 1 Column 2
Row 1 22a 33 11 11
Row 2 22a 33a 22a 33
Row 3 55 22b 55 23b
The code in the link will tell you what is not in sheet 1 but in sheet 2 and vice versa. In this example, I would like the code to tell me Sheet 1 Row 2 and Sheet 1 Row 3 are not in Sheet 2, and Sheet 2 Row 1 and Sheet 2 Row 3 are not in Sheet 1 (Sheet 1 Row 1 and Sheet 2 Row 2 match).
If that is ok by you, you can do it without VBA using the following formula:
={IF(IFERROR(MATCH(A1&"|"&B1;Sheet7!$A$1:$A$3&"|"&Sheet7!$B$1:$B$3;0);-1)=-1;"Unique";"")}
Assuming that each of your tables start in A1 (so that the tables with three entries span A1:B3), and entering this formula into C1 (and copying it down), press CTRL+SHIFT+ENTER when entering the formula to create an array formula, this will show the word "Unique" in column C if the pair in that row on that sheet is not in any of the row-pairs on sheet 2.
You can then use conditional formatting to highlight unique rows, filter on the tables to include only unique rows, or some other way of doing what you need.
NOTE 1: I have entered my numbers in Sheet6 and Sheet7 instead of 1 and 2. The formula written above goes into Sheet6.
NOTE 2: My language use ; instead of , as function separator, so if yours use , you need to change that.
NOTE 3: You will need to expand the ranges Sheet7!$A$1:$A$3 and Sheet7!$B$1:$B$3 if your set grows (this will happen automatically if new rows are inserted in between the old ones). The best is still probably to create named ranges for each of the 4 columns, exchange the references with those, and manage the named ranges instead of the formulas.
NOTE 4: If your data set contains the character "|", you need to change that as well, to match some character that you for sure do not have there.
Alternatively you could in column C on each cheet enter (assuming first entry in C1)
=A1&"|"&B1"
and copy this down, then run the solution from your copied example using that C column instead of on A1 and B1.

Apply Count formula in a Excel Row with conditions True/False

I have to apply a logical count formula on a column which contains some value. It contains identical and non-identical numbers.
What I want is a way to apply a formula to the next column which will lookup the whole column and decide on below condition:
whether A1=A2; if True, if A1=A3 and so on till it returns False (not equal value) then count all the true results in a row and reflect the value against the first cell.
Then if there was three match then it should reflect 3 against the first cell and the next two cells should be left blank. Below is the example column:
Numbers No of Days
47.76
-429.98
-429.98
-429.98
-1328.98
-1328.98
-1328.98
-833.32
-643.7
-1328.98
-580.26
-556.76
-556.76
-1181.78
-1139.99
-1124.49
-1103.59
-1103.59
-1091.39
-1048.94
-1048.94
-451.38
-451.38
-321
-321
438.5
477.5
698.76
795.93
795.93
804.83
What I want should be like below after formula:
Numbers No of Days
47.76 1
-429.98 3
-429.98
-429.98
-1328.98 3
-1328.98
-1328.98
-833.32 1
-643.7 1
-1328.98 1
-580.26 1
-556.76 2
-556.76
-1181.78 1
-1139.99 1
-1124.49 1
-1103.59 2
-1103.59
-1091.39 1
-1048.94 2
-1048.94
-451.38 2
-451.38
-321 2
-321
438.5 1
477.5 1
698.76 1
795.93 2
795.93
804.83 1
Please help guys as whatever I tried has failed here. Need some excel experts to share some advise or a resolve.
In Cell B2 use =IF(A2=A1,"",MATCH(0,--(A2=$A2:$A$50),0)-1) entered using ctrl+shift+enter
The if statement checks to see if the number is the same as the one above it and if it is it leaves the cell blank.
Inside the match statement --(A2=$A2:$A$50) returns an array of 0s (if the cell doesn't match A2) or 1s (if it does match). The match statement finds the first 0 in the array (the first number that doesn't match). Since this returns the place in the array that starts at the current row it will give the number of rows that match+1. We just need to subtract that 1 to get the number we need.

how to write vba code for selecting rows from a sheet?

I am a beginner in vba...i am doing a task which consist of 6 sequences..
Sequence 1
Sequence 2
Sequence 3
Sequence 4
Sequence 5
Sequence 6
In sheet-1 i have done all the calculations for all 6 sequences and tabulated the results..
The order of sequence changes according to constraints...i.e, "1,2,3","1,2,4","1,3,4","1,3,5"..)...
Is it possible to pick the required sequence along with its contents(tabulated results) from sheet-1 and display it in another sheet?
(i.e., For sequence order "1,2,4",only sequences 1,2 and 4 from sheet 1 should be displayed in another sheet)
Kindly help me to solve my problem..
I would like to add more data..
In Sheet 1 i have created a table which gives the order of sequence to be followed
Input data
Enter A
Enter B
Enter C (True or False)
Constraints
If (AND(A=B,C="TRUE"),"1,2,3",IF(AND(A<>B,C="TRUE"),"1,2,4",IF(AND(A=B,C="FALSE"),"1,3,4",IF(AND((A<>B,C="FALSE"),"1,3,5",""))))
RESULT DISPLAYED
1,2,3 OR 1,2,4 OR 1,3,4 OR 1,3,5 (DEPENDING ON MY INPUT DATA)
IN SHEET 2 I HAVE DONE THE CALCULATIONS FOR ALL THE SIX SEQUENCES
SEQUENCE 1
A) VERIFICATION OF X
B) VERIFICATION OF Y
C) VERIFICATION OF Z
SEQUENCE 2
A) VERIFICATION OF P
B) VERIFICATION OF Q
C) VERIFICATION OF R
.
.
.
.
TILL SEQUENCE 6
NOW, i want to filter my sequence according to the result displayed in sheet 1
example: If the sequence displayed in sheet 1 is 1,3,4 either only these sequences should be displayed in sheet 2 or the sequences 1,3,4 selected from sheet 2 shuold be displayed in sheet 3...
I am assuming that each sequence is a formula that produces a number (if not, post some sample data). Also assuming that the number is the order you want things displayed. Assuming sample data like this in Range("A1:A4):
=D1+D2
=D3+D4
=E4+E4
=F4+F5
Values like this in Range("A1:A4"):
5
4
1
2
So the order you want is A3, A4, A2, A1. Here is the VBA code:
'Copy the data so that we can maintain the original (optional)
For i = 1 to 4
ActiveSheet.Range("B1").Offset(i-1,0).Value = ActiveSheet.Range("A1").Offset(i-1,0).Value
next i
'Sort the new data
ActiveSheet.Columns("B").Sort key1:=Range("B1"), _
order1:=xlAscending, header:=xlNo
'Copy the new sorted rows to another worksheet
For i = 1 to 4
Sheets("Sheet1").Range("A1").Offset(i-1,0).Value = ActiveSheet.Range("B1").Offset(i-1,0).Value
next i
I made a lot of assumptions, so not sure if I got it right. Posting sample data makes it easier to answer.

Using VLookup to find corresponding row

I have 5 sheets in an excel file named
roll 1to2.5
roll2.5to5
roll5to7
roll 7to9.5
roll 9.5to12
Each sheet has two columns with the following data:
A B
1 22
2 25
3 29
4 20
5 18
6 26
7 19
8 16
9 21
10 20
Now I have been able to do the following: In column C, if I enter a number from Col A, say "7", i get the corresponding value from Col B i.e. "19". I used the following formula
=VLOOKUP(C5,A1:B10,2,FALSE)
This works good uptill here.
Issue 1: Let's say I want to enter the roll in a cell say "5.5", it should automatically consider data from sheet 3 (roll5to7)
Issue 2: and then if I enter a value of ColB, say "20", it should pick up the corresponding value from ColA i.e. "4" (first match) in sheet 3.
How can I achieve this?
To get the right sheet name list your 5 sheet names in one column and in the previous column the lower bound for each (1, 2.5, 5, 7 and 9) and name that two column table Table
Now you can use this formula
=VLOOKUP(C5,INDIRECT("'"&LOOKUP(D5,Table)&"'!A1:B10"),2,FALSE)
where D5 contains the roll
LOOKUP finds the correct sheet name and INDIRECT converts text to a valid reference
Edit:
If you want to look for C5 in column B and find the corresponding value from column A then INDEX/MATCH would look like this:
=INDEX(INDIRECT("'"&LOOKUP(D5,Table)&"'!A1:A10"),MATCH(C5,INDIRECT("'"&LOOKUP(D5,Table)&"'!B1:B10"),0))