deleting columns from a range in vba which may have NA - excel

I have ranges of data which have been downloaded from a third party API. The data comes out in ranges like this where it periodically gets #N/A back from the database. I want to delete the data in columns ABCH (not the entire column) and shift them over to have only DEFG left. How can I do that kind of iteration. When I tried to loop over the cells in the range, as I delete the columns, the index gets mixed up because the range is now shortened. How can I loop over the range given that as I delete the columns, the range size changes. Thanks

Something like this may work. Please check that out
Range("C18:C25,G18:G25,I18:I25").Select ' select multiple ranges - in your case columns A, B, C H
Selection.Delete Shift:=xlToLeft

So the data table might look something like this
A B C D E
7 8 9 10 11
NA NA 4 5 7
9 10 11 12 NA
10 11 12 13 14
11 12 13 14 15
In this example, I'd like to be able to delete columns A,B and E and have columns C&D be where columns A&B were. I ended up doing a workaround but the initial code I tried was something like this
for i =1 to datarange.columns.count
for j = 1 to datarange.rows.count
if iserror(datarange.cells(j,i)) then
' delete column shift cells
end if
next
next
The problem was that as I deleted columns in the range, the column count was dropping. I wasn't iterating over the original range. Anyhow, I managed a simple workaround. Thanks for having a look.

Related

Removing several desired rows from excel sheet using the second sheet

How can I remove specific rows which has the same values in another excel sheet? of course there are many columns but I filter such that specific rows remained in the second sheet and we have those values in first sheet as well as other values. for example below I want to delete rows from second set with the same value in the first column of second set. rest does not matter
A B C
1 2 3
4 5 6
7 8 9
10 1 1
second set
A B C
1 5 6
3 3 9
7 7 6
This checks if the value in A2:A4 is in the other sheet in the A-column:
Formula used (I replaced the ´;´ with ´,´)
ISNUMBER(XLOOKUP(A2,Sheet2!$A:$A,Sheet2!$A:$A,"NOPE"))
The only thing left to do is filter by color and delete the rows. Though do remember that deleting rows in larger quantities, if they're not continuous, can be quite slow.

extract ranges from column values

I've column of values 1-10 missing 4 and 7 can I extract 1-3,5-6,7-10.
Currently I'm using this formula =IF(A3=A2+1,C2,C2+1) which gives me helper column sort of help
but my list is long If I could extract ranges that would be helpful.
There are no duplicates
I'm not sure if I understand exactly what you mean, but this is what I have done until now: I have copied the same columns A and B, and I have added following columns:
Column C : =COUNTIF(B$2:B$16,B2)
Column D : =IF(AND(C2=C3,C3<>C4),"End",IF(AND(C2<>C3,C3=C4),"Begin"))
The result looks as follows:
As you can see:
The number 1 from column B ends at row 6, and D6 indeed indicates "End".
The number 2 from B starts at row 7 (D7="Begin") and ends at row 8 (D8="End").
The numbers 3 and 4 are not correctly handled but:
As far as 5 is concerned: it starts at row 11 (D11="Begin") and ends at row 15 (D15="End").
There still is some finetuning to do but I guess you see how the ranges start being unfold.

How can I use a relative range in an excel function

newbie with advance excel here. :)
I'm looking for a solution how to use dynamic or relative range with the expression I came up with. I have this excel sheet with multiple columns, but I only need to check 4 columns for info. The sheet has like 1000 rows and growing, and I need to go through each row and check for blank cells in each row. Obviously dragging the cell with my formula using the mouse automatically updates the cell reference on the expression. However, I'm going to use the expression in an automation tool where it looks one row at a time and captures the value into another "array" or "collection" variable.
Col A B C D
Student1 7 8 0
Student2 2 1 2
Student3
Student4 1 0 2
Student5
Student6 8 2 4
Student7
Student8 3 1 2
I have this excel expression
=IF(AND(A2 <> "", A3<>"", A4<>""),A1,"empty")
Expected results should be like the below and placed in another Sheet (Same workbook)
Student3
Student5
Student7
Thanks in advance!

Excel find next match in table column

I am trying to find the next matching row in a table (not just a range) that matches the current cell's value. I have tried to pass in the column from the next row to the end of the column but cannot get it to work. All ideas are welcome! E.g.
Current idea that doesnt work: =MATCH([#Value],OFFSET([#Value],1,0):[Value])
Example data:
Row Value Next_Match
1 1 #N/a
2 2 4
3 3 6
4 2 5
5 2 #N/a
6 3 #N/a
Try this
=OFFSET(B3,MATCH(B3,B4:B$100,0),-1)
assuming B100 is the last row and you want to match only afterwards... Otherwise use B2:B100 etc
Oh and data is in Columns A,B,C
Although the perfectionist in me rather's
=IF(ISNA(MATCH(B3,B4:B16,0)),"Last",OFFSET(B3,MATCH(B3,B4:B16,0),-1))

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