VBA to add missing numbers in a column and add a zero in next to it in column B - excel

Can anyone write a code for the following task.
I have a series of numbers in column A and an associated value in column B.
e.g
A B
1 144
2 33
5 4
6 56
8 1
I need to run a macro that add in the missing consecutive numbers in column A and adds an associated zero value in column b.
e.g
1 144
2 33
3 0
4 0
5 4
6 56
7 0
8 1
can anyone help

If you can present the information on a different sheet, you don't need vba.
On the 2nd sheet, put your numbers in column A, using autofill to get the progression, or use =A1+1 as a formula to create the progression.
in column B, use =IFERROR(VLOOKUP(A1,Sheet1!$A$1:$B$5000,2,FALSE),0) to find the number, or to put 0 if no number is found
(use =IF(ISERROR(VLOOKUP(A1,Sheet1!$A$2:$B$5,2,FALSE)),0,VLOOKUP(A1,Sheet1!$A$2:$B$5,2,FALSE)) if your version of excel does not support ISERORR)

Related

Sum across columns based on 3 criteria

I'm trying to get a Sumif working across multiple rows.
For example, say my data source is below:
A B C
1 1 10 11
2 2 9 12
3 3 8 13
4 4 7 14
5 5 6 58
I want to sum from Columns A to C, in row 3. Output would be 24.
Ideally my criteria for the Horizontal Start, Horizontal End and Vertical would all be referenced in different cells which can be updated to get a new result depending on the criteria.
Is this possible so that the criteria cells can be updated and the formula will update accordingly?
Use a SUMIF, and betweem the logical criteria, simply use 3 criterias by seperating each with "&"'. This says that excel will sum if a happens, and b and c.

Find maximum of row, return column name

I have four rows and six columns of random numbers between 1 and 10. The headers atop are named A through F accordingly. I want to populate a range (A1:A6) on another sheet with the maximum number for each row. That is easy with the MAX function. However, in a another range (B1:B6), I want to put the column name to which this number belongs.
An HLOOKUP() won't work because a maximum value in one row is likely not unique number across the entire sheet. I am thinking a MATCH INDEX type function, but my understanding of those functions, especially in conjunction, is poor.
A B C D E F
1 0 2 10 9 8
9 3 7 6 9 10
10 3 0 2 1 4
9 4 7 8 6 3
Assuming your array is in Sheet1 and the columns are labelled, please try in another sheet, copied down to suit (to Row4 since there are only four rows of numbers in your data):
=INDEX(Sheet1!A$1:F$1,MATCH(MAX(Sheet1!A2:F2),Sheet1!A2:F2,0))
This will return only the first column label from a row where the maximum for that row occurs more than once.

Index & Match formula using IF and ISERROR

I have data in columns C & D. A range of ten students in A1 to A10 are identified with roll numbers. In Column C and corresponding Column D, there are 8 students from roll number 1 to 8 (Column C) and their marks (Column D).
I need a formula in column B to automatically extract the marks from column D against the roll numbers in Column A and the two cells in column B (marks of roll number 9 & 10) may remain blank.
Any Excel formula or VBA macro solution is much appreciated.
Column-A Column-B Column-C Column-D
1 50 1 50
2 55 2 55
3 35 3 35
4 60 4 60
5 78 5 78
6 45 6 45
7 39 7 39
8 82 8 82
9
10
Try using VLOOKUP with IFERROR, i.e. this formula in B2 copied down
=IFERROR(VLOOKUP(A2,C$2:D$9,2,0),"")
VLOOKUP will return the required marks when the roll number exists.....or an error which IFERRORconverts to a blank.
The simplest solution would seem to be copy and paste. Alternative solutions would seem to be relatively simple (eg #barry's) but if you really want INDEX, MATCH, IF and ISERROR then maybe:
=IF(ISERROR(INDEX(D:D,MATCH(A2,C:C,0))),"",INDEX(D:D,MATCH(A2,C:C,0)))

Way to count last values before each zero?

I am trying to count the last value before it resets back to zero multiple times per column. Here is my example
1
2
3
4
5
6
7
8
0
1
2
3
4
5
6
0
1
0
0
1
2
3
0
And the list goes on but for this example I would be looking to do something like a LARGE or SMALL where I could get the answers like this:
8
6
1
3
Ultimately I would like them to be in the descending order, but if that isn't part of the formula I can take care of that if I can just figure out a way to capture them.
Can this be done?
You can try the following in column B try the following =IF(A2=0,IF(A1=0,"";A1),"")
then filter column B on non blank value
Put your original data in column B
In cell A2 enter:
=IF(AND(B2<>0, B3=0),MAX($A$1:A1)+1,"")
and copy down. Finally in C1 enter:
=VLOOKUP(ROW(),$A$1:$B$23,2,FALSE)
and copy down till you see errors. Should look like:
Basically column A is a "helper" column used to mark all the "good" values to facilitate easy pick-up

EXCEL match 2 columns against each other

I have two columns of data, they look something like this:
A B C D
1 2 SOME RECORD
2 6 SOME RECORD
3 10 SOME RECORD
4
5
6
7
8
9
10
So basically column A is a list of indices, where some of them appear in column C with corresponding records saved in column D. Column B is currently empty, and what I want to do is if say index 2 appears in both column A and column C (they matches), then put the record beside C2 in the cell B2. So essentially I want it to look like this:
A B C D
1 2 SOME RECORD
2 SOME RECORD 6 SOME RECORD
3 10 SOME RECORD
4
5
6 SOME RECORD
7
8
9
10 SOME RECORD
Can someone help please?!! Thanks!!!
UPDATE: I tried this and it doesn't work. The data in column D is calculated using a UDF and is refreshing every 1 second. The VLOOKUP function fails even though I can see the 2 indices are the same!! Is it because of the format of the cell or column? I.e. does vlookup compare data type as well?
Assuming your data in A starts from A1 - put in B1 the following and autofill:
=IFERROR(VLOOKUP($A1,$C:$D,2,0),"")
This includes handling of missing values.
You'll want this:
B1=VLOOKUP(A1, C:D, 2, FALSE)
This will look up the value in column A within the array spanning columns C and D. It will give you the value found in the second column (D). FALSE makes it an exact match, otherwise you might get 2 and 20 matching because hey, they're kind of similar...

Resources