how to select rows adjacent to blank cells in excel? - excel

In the excel sheet, I have two columns A and B. For every value in rows in column A there are values in column B. But some of the rows in column B are blank because of unavailability of value for the value in column A.
Now I want to pick only the row values in column A for which corresponding row in column B are blank. How do i do that? Thanks a lot.

Option Explicit
Public Sub filterBlanks()
With ActiveSheet.UsedRange
.AutoFilter Field:=2, Criteria1:="="
.Columns(1).SpecialCells(xlCellTypeVisible).Select
End With
End Sub

Use the Range.SpecialCells method to find hte blanks in column B and then Range.Offset property to select their corresponding cells from column A.
with activesheet
with .columns("B").specialcells(xlcelltypeblanks)
with .offset(0, -1)
.select 'do something with the cells in column A
end with
end with
end with
The above can have problems if you have cells merged across columns A and B (e.g. A1:B1 are merged). It also depends upon the cells being truly blank; not cells with formulas returning zero-length strings (which are not blank cells).

Related

Autofill Copy down up until next empty row- Excel VBA Macro

i would like to paste a value in a given cell in Column A (empty column), i would like to autofill copy this down up until the next empty row or data in column B.
I can do this by specifying a range to copy down to but i would like for this to be dynamic since the amount of rows copied can be different. There are blocks of data, with a blank row in between. I would like to essentially copy and paste a value for each block of data in column A. Thank you!
Selection.AutoFill Destination:=Range(Selection, Selection.End(xlDown)), Type:=xlFillCopy
Since Column A is empty end(xldown) will return row 1048576. You need to look at Column B for the end then refer back to Column A.
You can use offset to grab Column B from your Selection then back to Column A.
Selection.AutoFill Destination:=Range(Selection, Selection.Offset(0, 1).End(xlDown).Offset(0, -1)), Type:=xlFillCopy

Delete row (column A to K) if cell blank in column D (or filter and delete all (Blanks)

Looking for some help here since most answers delete the entire row. Thanks in advance!
This is what my table looks like, I'm looking to delete a row (but only from column A to K, not entire row), if a cell is blank in column D.
Data is dynamic, will change each time, but it should always have a blank in cell D. If not, looking for the code to just continue on.
So highlighted D3 is blank, I want it to delete A3 to K3.
.
.
You could filter cell D2 for (Blanks) to have them all -- mind you there most likely will be a blank cell in column B, but I essentially want to delete all this data filtered.
Is is ColumnK or ColumnF. Something like this, maybe. Just change to suit your needs.
Option Explicit
Sub BlankRowDeletion()
'Declaring variables
Dim LastRow As Long
Dim Rng As Range
'Getting row number of last cell
LastRow = Range("A3").SpecialCells(xlCellTypeLastCell).Row
'Selecting all data
Set Rng = Range("A3:K" & LastRow)
'Selecting Blank cells
Rng.SpecialCells(xlCellTypeBlanks).Select
'Deleting complete row
Selection.EntireRow.Delete
Range("A3").Select
End Sub

Find next empty cell in a range?

I am trying to find the next empty cell in a range, starting from a specific column and then trying to put in todays date.
Code:
ThisWorkbook.ActiveSheet.Cells(ThisWorkbook.ActiveSheet.Rows.Count, "U").End(xlLeft).Offset(0, 1).Value = Now()
My Range to check for blank cells is V to X.
So for instance if i had a spreadsheet that looked like below:
Column U Column V Column W Column X
Start checking blanks from here but don't check this column itself I'm Not a blank Cell so move to the next cell and check I'm a blank cell. Put value here I am a blank cell. I will have a value placed in me next time
At the moment my code is putting the date in the first blank cell in a row starting from column A. This is obviously not correct.
Please can someone show me where I am going wrong?
Change xlleft to xlToLeft
I recommend to used usedrange rows count than simple sheet.rows.count for your requirement.
ThisWorkbook.ActiveSheet.Cells(ActiveSheet.UsedRange.Rows.Count,"U").End(xlToLeft).Offset(0, 1)

How to lineup Data from Column A to Data in Column B?

Column A is inventory that I should have. Column B is inventory I received.
I am missing a lot of inventory and have had to manually do this. What I want is the numbers in each column to match up & the numbers that are not present in B that are present in A to add a space in column B to show thats it's missing:
What I have:
What I want:
This is a pretty lightweight VBA routine that will do what you need:
Sub matchCells()
Dim colARange As Range
Dim colACell As Range
'Change this to your Column A range size
Set colARange = Sheet1.Range("A1:A5000")
'Iterate through each cell in your Column A Range
For Each colACell In colARange.Cells
'Check if the cell in Column A doesn't match the cell in Column B
If Not (colACell.Value = colACell.Offset(0, 1).Value) Then
'It doesn't match so shift Column B's cell down one
colACell.Offset(0, 1).Insert Shift:=xlShiftDown
End If
'Now we loop again and we will continue shifting Column B down
' a cell until it matches.
Next
End Sub
You can try below formula on Column C... Should give you the result you need in Column C.
=IF(ISNA(VLOOKUP(A1,$B$1:$B$10,1,FALSE)),"",VLOOKUP(A1,$B$1:$B$10,1,FALSE))
$B$1:$B$10 is the Range of data (no of rows in column B)

Excel vba Autofilter top 10 rows, copy result based on criteria

I have data in my spreadsheet in range A6:H105
I want to filter the first ten rows (A6:H15) with fixed criteria on the H-column and copy only column A, D, E from result
So far I have the following code:
Sub Filter()
ActiveSheet.Range("A6:H15").AutoFilter Field:=8, Criteria1:="xxx"
End Sub
Question 1: Why is first row not filtered?
Question 2: How to copy only column A,D,E from range after applied filter?
Answer 1: It is bacuse Autofilter treats the first row as table header. Set the range to start from one row higher, from row 5.
Answer 2: You can copy a combined range like this
Range("A6:A15, D6:D15, E6:E15").Copy
and then paste it into three adjacent columns wherever you like.
You may need to modify the range to select only filtered or non-blank cells first.
You can do something like this:
Sub Filter()
ActiveSheet.Range("A6:H15").AutoFilter Field:=8, Criteria1:="xxx"
Range("A:A,B:B,D:D").Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
Range("J1").PasteSpecial Paste:=xlPasteValues
End Sub

Resources