Excel VBA Index Match Only Blank Row [duplicate] - excel

So I have a column with blanks randomly throughout...I need a macro that would select only the blanks, and then in those blank cells paste in the value of the cell above it. The select part is obviously easy, but I keep getting errors about 'too many continuations' when trying to fill the formula down into the blanks. I included a picture, the first column is a 'before' and the second is how I want the column to look after the application of the macro.
If the macro needs to create a second column or something that's fine too, as long as the end result looks like it does in the picture. Thanks!
Picture to illustrate.

try,
sub fillblankfromabove()
dim blnks as range
with worksheets("Sheet1").Columns("E").Cells
set blnks = .specialcells(xlcelltypeblanks)
if not blnks is nothing then
blnks.formular1c1 = "=r[-1]c"
end if
.value = .value
end with
end sub

Another way to do this is to select all cells that you want included in this process, press CTRL + G, select Special, then select 'Blank Cells'. This will select all blank cells within your selected range. Then enter =[cell above] and press CTRL + ENTER and it will enter that formula into all selected cells.

Related

Excel selecting a number of cells below selected cell shortcut options

In Excel, I would like to select a cell and then copy the contents to n number of cells below. Instead of using the "fill" drag option since the number of rows will be fairly large and require scrolling and then stopping at the correct cell I was looking for other options.
I am currently doing the the following:
In excel I select a cell and then on the top left corner it shows the cell (i.e. A1). Then to select the number of cells below it, I modify the top left box to a range such as A1:A10 which selects the range of cells. See attached image. I then use the shortcut key "Ctrl-D" which copies the first cell to the other cells.
Is there a way instead of mentally calculating the ending cell number, I can do something that effectively works "A1 + 10" in this box to select 10 cells below to select the A1:A10 range?
How about this way? Enter your formula in cell 1. Enter the number of copies (including the original) you want of it in the cell below it. Double-click on the number you entered and the formula is filled down.
To try, install the procedure below in the code module of the worksheet on which you want the action. (That's a module Excel set up when you created the sheet. Don't install the code in a standard code module that you have to insert yourself.)
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Const TriggerClm As String = "A"
Dim R As Long
With Target
If (.Column = Columns(TriggerClm).Column) And (.Row > 1) Then
R = Val(.Value)
If R Then
With .Offset(-1)
If .HasFormula Then
.Resize(R).FillDown
.Select
Cancel = True
End If
End With
End If
End If
End With
End Sub
You may wish or have to tweak the code. For example, the action is limited to column A. Change the constant at the top of the code to identify the column you prefer, or change the limits to better suit your requirements. The code also checks if Cell #1 contains a formula. If that isn't what you want to fill down that condition will have to be revised.
Last, but not least, the code selects Cell#1 (the one that contained the number is over-written). That may not be the best choice. You can modify the code to select a place in your worksheet where you will continue working.

Apparently "blank" cells still counted by COUNTA until updating the cell - is this a bug or feature?

I've noticed a behaviour in Excel which doesn't intuitively make sense.
If any formula causes a cell to evaluate as a blank string, such as ="", then you copy that cell and paste as value so that the formula disappears - the cell will still count when included in a COUNTA formula. Until you press F2 to edit the cell, and then press Enter - then the cell will no longer be counted.
I can understand why COUNTA counts cells including a formula, but once you copy and paste a cell as a value, if the formula evaluated to blank, the cell should also be blank, at least intuitively. What's weird is that updating the formula causes the COUNT to decrease. This seems like a bug, but I wanted to put it on here to be sure this wasn't a weird feature I was missing.
Here are the steps I took to reproduce this (also in the image below):
Enter ="" into cells A1:A10
Select cells A1:A10 and Copy
Select cell A1 and Paste as Value
Select any cell within the range and press F2
Press Enter
The count in cell A11 will decrease by one
Alternatively to Step 4, if you select any cell within the range and press Delete, the count will also decrease.
The count in the image is 7 because I've tested Steps 4 and 5 several times - the count started at 10.
I am using the Office Insider program, so if it is a bug this may be the reason why.
Can anyone shed some light on this?
Not a bug.
="" returns an empty string, which is not the same as a truly empty cell. Copy/pasting values maintains the empty string.
Editing the cell with F2 and then Enter effectively renders the cell blank.
Here's an interesting little VBA test for more detail:
Sub Test()
With ActiveCell
.Formula = "=""""" ' enter ="" in the ActiveCell
Debug.Print TypeName(.Value) ' returns String
.Copy
.PasteSpecial xlPasteValues
Debug.Print TypeName(.Value) ' returns String
.Value = .Value
Debug.Print TypeName(.Value) ' returns Empty
End With
End Sub

Excel, Add auto Sequence number

I need to ad the next sequence number in Column A automatically when I fill enter the next value in Column B. This sounds confused. Just see the snap so you will get the clear picture.
This should be done without usual dragging option. Is there any way
Make the Value of A1 equal to 1 and then from the A2 use the formula:
=IF(B3<>"",A2+1," ")
Drag this formula for the whole column.
In my solution I have a drag, but it is to define the formula for each of the fields. (I'm not sure if is this you are trying to avoid when you say
without the usual dragging option
)
You may achieve this using macros. Formula will increase the file size and processing time.
Right click on the sheet name on sheet tab-->select view code-->paste below code--> save file as macro enabled workbook.
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo step
If Target.Column = 2 And Target.Value <> "" Then
Target.Offset(0, -1) = Target.Row - 1
End If
Exit Sub
step:
Exit Sub
End Sub
You can also do this without using macro and formula:
Type first 2 value to the cell to establish the pattern
highlight the cell range
Under Home tag -> select Fill -> choose Series
Follow the image and Select OK
If you convert your table to a List ("Table") and use a formula for the first column, that formula will "auto-extend" as new values are typed in under "Item"

Find first non-blank cell in a range

I am working with a list of data where one or multiple cells in a row can be blank.
Lets say the list is cells A1, A2, A3, A4. I am trying to create a function that will do the following:
IF A1 has a value I want the cell to return A1.
IF A1 is empty then I want it to return A2.
IF A1 and A2 are both empty I want it to return A3.
If A1, A2 and A3 are all empty I want it to return A4.
first result on google: http://chandoo.org/wp/2014/01/15/find-first-non-blank-item-in-a-list-excel-formulas/
This formula returns the first TEXT cell for a range B1:B100:
=VLOOKUP("*", B1:B100, 1,FALSE)
* is a wild card in Excel. When you ask VLOOKUP to find *, it finds the first cell that contains anything.
NOTE: This approach finds first cell that contains any TEXT. So if the first non-blank cell is a number (or date, % or Boolean value), the formula shows next cell that contains text.
If you need to find non-blank that url gives the following solution:
If you want to find first non-blank value, whether it is text or number, then you can use below array formula.
=INDEX(B1:B100, MATCH(FALSE, ISBLANK(B1:B100), 0))
Make sure you press CTRL+Shift+Enter after typing this formula.
How this formula works?
ISBLANK(B1:B100) portion: This gives us list of TRUE / FALSE values depending on the 98 cells in B1:B100 are blank or not. It looks like this:
{TRUE;TRUE;TRUE;FALSE;FALSE;FALSE;FALSE; ...}
MATCH(FALSE, ISBLANK(…), 0) portion: Once we have the TRUE / FALSE values, we just need to find the first FALSE value (ie, first non-blank cell). That is what this MATCH function does. It finds an exact match of FALSE value in the list.
INDEX(B1:B100, MATCH(…)) portion: Once we know which cell is the first non-blank cell, we need its value. That is what INDEX does.
As indicated in your comment on your question, you have 500 rows interspersed with blank cells. You want to fill blank cells with the value of the last non blank cell.
I'd write some VBA code that'd work as follows: select the range of cells you want to back fill and run this VBA:
Sub fillBlanks()
For Each c In Selection.Cells
If c.Value <> "" Then
lastVal = c.Value
Else
c.Value = lastVal
End If
Next c
End Sub
basically, if the cell is empty, use the value of the last non blank cell (if there were no blank cells above, it will remain blank). Else, if the cell is not empty, save this as the last non blank cell. Repeat for every cell in the selected range.
Step by Step instructions on using this vba code - for this sample worksheet:
Make sure the range is selected, press ALT+F11.
This should open the Visual Basic Editor:
Press F7, This should bring up the code for the activesheet. Paste the VB code from above:
Press F5 (or use the menu to run the code).
The end result should be as follows:
Select ColumnA:
HOME > Editing > Find & Select > Go To Special... > Blanks, OK, =, ↓, Ctrl+Enter.
You can just put a rank.eq formula in the column next to it, and do a vlookup to bring all of your data to the top. This will bring all of your data to the top.
For example, in the image below I am ranking using the percentage, I want to bring the cells with data to the top for presentation, I will hide all columns other than where my vlookups are.
This did the trick for me
=LOOKUP(2,1/(A1:A13<>""),A1:A13)
Source credit: here

Display value (result of formula) rather than formula in fomula bar

Here is the problem:
My table is very large and the column width is not enough to show all the text in it.
Since the text/value is generated by a formula in the cell, if I click on the cell, the Formula Bar will display the formula and not the value. Of course.
However, I think it is very useful to quickly know the content of the cell generated by the formula. If I expand the column width every time is not quick and clean.
Do you know if there is a way to solve the problem?
No there is not a way to show it in the formula bar if there is a formula in the cell.
The best thing I can suggest is to leave row 1 empty and merge A1:N1 together or similar, ,freeze row 1 if necessary
then put this formula into A1
=INDIRECT(CELL("address"))
When you select a cell on the sheet , press F9 on the keyboard and you can read the full value of the active cell in row 1
With a little VBA in the sheet you can also have it automatically update the cell
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
ActiveSheet.Calculate
End Sub
Here's another solution. Simply assign a key to the following macro:
Sub DislayCellValue()
Dim outp As String
outp = "The value in the active cell is:" & Chr(13) & Chr(13) & ActiveCell.Value
MsgBox Prompt:=outp
End Sub
To display the value in a cell, click on the cell and hit Ctrl-(key).This might be helpful.

Resources