VBA to check for blank cells in columns based on value in another column - vba

Given
O 1 2 3 A
A 4 5 6 B
B 7 8 9 D
O 3
C 15
T 18
I'm looking for VBA code to validate that when column A contains a value that the remaining columns also contain values and when it doesn't contain a value, that columns 2 & 5 also contain values but 3 & 4 don't.
I've simplified the example, in a real sheet there will be many more columns and rows to check.
I've considered COUNTIF and INDEX/MATCH and array forumlas but from my understanding these all work on single columns at a time.
I want to do something like WHEN A1:An<>"" THEN COUNTBLANK(B:E) ELSE COUNTA (C:D)
Is the best way to use autofilter using blanks in A and then countblank and then a second autofilter for values in A.
Thanks

You can do it with a couple of nested IF formulae as follows:
=IF(A1<>"",
"A not empty, "&IF(COUNTBLANK(B1:E1)=0,
"B:E not blank",
"B:E have blanks"),
"A blank, "&IF(AND(COUNTBLANK(B1)+COUNTBLANK(E1)=0,
COUNTBLANK(C1)+COUNTBLANK(D1)=2),
"Columns 2&5 have values and Columns 3&4 don't",
"but condition not met"))

The reason for going down the VBA route is that I want a generic reusable function as opposed to a formula I copy between cells and sheets changing the columns etc along the way ending up with a lot of duplicate code.
So something that takes a column to test and a value to test it with. Third parameter would be a range of columns to validate, and the fourth parameter the validation.
I don't want any solution to have the columns hard coded and I don't want intermediate totals at the end of rows. This is fairly easily achieved in Excel itself...
The reason for trying to use countblank is that I can apply it to a range.
After a lot of searching I discovered this (the columns don't match the original example)
=SUMPRODUCT((A2:A19<>"")*(B2:D19=""))
=SUMPRODUCT((A2:A19="")*(D2:D19=""))
=SUMPRODUCT((A2:A19="")*(B2:C19<>""))
Nice huh? I just need to convert it into VBA now.
Thanks

Related

Creating new rows by combining existing rows excel

I am fairly new here, so if this go against the rules please tell me.
I have an issue that seems pretty simple but I wanted to check to make sure. I have been trying to see if I could create a new row by combining every variable from one column with another, like so:
Column 1 Column 2 Combined
A 1 A1
B 2 A2
3 A3
B1
B2
B3
But instead of typing the combinations manually, I wanted the combined column make this combination without user input and to update automatically whenever column 1 or 2 has a row added or removed. I have been trying to figure out if there is some way to use the concatenate function in excel or the & sign but neither methods seems to work. I was thinking trying to code this in visual basics.
The main question: is this possible to do in excel? If so which function(s) could I use?
This assumes your data has one header row (row 1), Column 1 is column 'A' and Column 2 is Column 'B'. Place the formula below in an empty cell and copy down as far as your data permits.
=INDEX(A:A,INT((ROW(A2)+1)/(COUNTA(B:B)-1))+1)&INDEX(B:B,MOD(ROW(A2)-2,3)+1+1)
now if you want to add a little flag to let you know you have more row than you need for your data you could add the following:
=IF(ROW(A2)-1>(COUNTA(A:A)-1)*(COUNTA(B:B)-1),"Data Exceeded",INDEX(A:A,INT((ROW(A2)+1)/(COUNTA(B:B)-1))+1)&INDEX(B:B,MOD(ROW(A2)-2,3)+1+1))
According to: https://www.extendoffice.com/documents/excel/3097-excel-list-all-possible-combinations.html
You can use this formula:
=IF(ROW()-ROW(**$D$1**)+1>COUNTA(**$A$1:$A$4**)*COUNTA(**$B$1:$B$3**),"",INDEX(**$A$1:$A$4**,INT((ROW()-ROW(**$D$1**))/COUNTA(**$B$1:$B$3**)+1))&INDEX(**$B$1:$B$3**,MOD(ROW()-ROW($D$1),COUNTA(**$B$1:$B$3**))+1))
In the above formula, $A$1:$A$4, are the first column values, and
$B$1:$B$3 are the second list values which you want to list all their
possible combinations, the $D$1 is the cell that you put the formula,
you can change the cell references to your need.
In your case, you should use:
=IF(ROW()-ROW($C$2)+1>COUNTA($A$2:$A$3)*COUNTA($B$2:$B$4),"",INDEX($A$2:$A$3,INT((ROW()-ROW($C$2))/COUNTA($B$2:$B$4)+1))&INDEX($B$2:$B$4,MOD(ROW()-ROW($C$2),COUNTA($B$2:$B$4))+1))

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.

copy only those rows whose string len is > 6 in b column cell values

is it possible to copy only those rows which has string length more than 6 characters and paste them to a new sheet? The task is I have to remove all the unwanted characters in the rows. the range is 2 to 4000 something like that
I was using looping and mid function to do that which i found on google because I found only looping solution to that on google, because I only need numbers in each cell of B column and remove all other character except &,/, ,,. Then after cleaning up all ce
lls in B column I need to check the number of digits in each row of B column and if it contains more than 6 it should be copied to new sheet and it may contain blank cells too in between
a b c
6451 1234567 somevalue
4563 12345 somevalue
3245 123456789 somevalue
2345 1234 somevalue
Now I have to copy the 1 and 3 rd row but not the other rows as length of string is less than 7. I have to check only the B column and copy the entire row if it has more than 6 digits
I think what your boss is trying to say (not very well, btw) about using variant arrays, etc. instead of worksheets is to keep it in VBA. Read in the data once and manipulate it within VBA instead of pasting and manipulating data on a worksheet to get the desired result. This of course extends to anything outside of making calls to Excel, not just variant arrays. Perhaps you are often looping through cells one by one and he is trying to say he'd prefer if you dumped a range into a variant array and worked on it inside of VBA instead.
Of course, the best method depends on the task on hand, in my opinion.
As for your row question, it's a little unclear what you're trying to do (try to be careful how you use the words 'row' 'cell' and 'column'). A row cannot have a 'string length' so I am assuming you mean cell. Please confirm.

Excel 2007 several columns with different values, find where which number is

I Have 6 columns where I have up to 320 values. I say up to because the number of values differ in each column and the values also differ in the columns.
Like this:
Column A has the following values, one in each cell: 1,2,3,4,6,8,9
Column B has the following values, one in each cell: 1,3,4,6,7,8,10
etc.
I would like to know what numbers that differ between the columns, so I would like to know that 2 is missing in the B column and that 9 is missing from the A column.
Optimal would be if it were possible to have one line for each number and when there is missing I just get a blank cell on that line in that specific column.
Keep in mind that there are 6 columns.
Is this possible? Is there a workaround?
I would prefer to do this in Excel but I can use other solutions as well as long as it solves my problem.
If I understand your question correctly the following might help. (I'm assuming no header row here)
In column G put the numbers 1 to 320, e.g. 1,2,3,4,5,6,...320 - so this takes up 320 rows.
In cell H1 put the following:
=VLOOKUP($G1,A:A,1,0)
Drag the above formulae across 6 columns - so H1 to M1 and then select the 6 cells (H1:M1) and drag them down to row 320.
In the missing numbers cells you will get a "#N/A" display so you could replace the above vlookup and use an if condition if you want. Something like:
=IF(ISERROR(VLOOKUP($G1,A:A,1,0)),"",VLOOKUP($G1,A:A,1,0))
Hope this helps.

Excel countif Pulling apart a cell to do different things

Excel 2007
I have a row of cells with variation of numbers and letters (which all mean something.. not random.)
It's basically a timesheet. If they take a sick day they put in S, if they take a partial sick day they put in PS. The problem is they also put in the hours they did work too. They put it in this format: (number)/PS.
Now if it were just letters I could just do =countif(range,"S") to keep track of how many s / ps cells there are. How would I keep track if they are PS where it also has a number separated by a slash then PS.... I also still need to be able to use that number to add to a total. Is it even possible or will I have to format things different to be able to keep track of all this stuff.
Assuming this is something like what your data looks like:
A B C D E
1 1 2 S 4/PS 8
...then you could do this:
1- add a column that just totals the "S" entries with a COUNTIF function.
2- add a hidden row beneath each real data row that will copy the numerical part of the PS entries only with this function in each column:
=IF(RIGHT(B1,2)="PS",IF(ISERROR(LEFT(B1,LEN(B1)-SEARCH("/",B1)-1)),"",INT(LEFT(B1,LEN(B1)-SEARCH("/",B1)-1))),"")
3- add another column to the right that just totals the "PS" entries by summing the hidden row from step 2.
3- add another column that totals everything by just summing the data row. that will ignore the text entries automagically.
4- have a grand total column that adds those three columns up
If you don't want to see the "S" and "PS" total columns, you can of course just hide them.
So in the end, the sheet would look like this:
A B C D E F G H I J
1 1 2 S 4/PS 8 1 4 11 16
2 4 <--- hidden row
HTH...
My quick take on this is:
pass the cell value into a CSTR function, so no matter what is entered you will be working with a string.
parse the information. Look for S, PS, or any other code you deem to be valid. Use Left or Right functions if you need to look at partial string.
check for number by testing the ascii value, or trying a CINT function, which will only work if the string can be converted to integer.
If you can show a sample of your cells with variation of numbers and letters I can give you more help. Hope this works out.
-- Mike