Given the following code, I would like to understand how it works:
Columns("A:A").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.FormulaR1C1 = "=R[-1]C"
So what it currently does is it takes the top cell of the row and auto fills it down with formula =A1 relative to that cell on all blank cells below it.
However it stops at the next cell that contains a value and does the same treatment down all the cells below it till the next.
How does this work exactly? How does vba knows to take the formula from the cell above the blanks and not just from the first cell of the column?
Much help is appreciated!
This is how the code works:
Select the entire column A
Columns("A:A").Select
Now select just the blank cells in column A
Selection.SpecialCells(xlCellTypeBlanks).Select
Now for every cell we've selected (all the blank cells in column A), input a formula.
The formula roughly equates to: "Equals to the cell immediately above this cell."
Selection.FormulaR1C1 = "=R[-1]C"
The formula is using a relative cell reference, and it literally translates to "Equals Row-1, same Column."
Related
I am trying to build a macro and I want to copy a row after selecting a specific cell with the filter. So I filter a cell in column "A" and then I want to copy all the cells in that row. The problem is that the rownumber corresponding to the selected cell may change, due to different datasets.
Below is the code:
Selection.AutoFilter
ActiveSheet.Range("$A$1:$DO$46").AutoFilter Field:=2, Criteria1:= _
"NAME"
Range("A5:DO5").Select
Selection.Copy
The point is that in VBA the selection is set at "A5:DO5", because in this specific dataset "NAME" is on "A5".
But in a different dataset "NAME" might be on "A9", but in VBA the selection of the row is still on "A5:DO5".
How can I make the selection of "NAME" and the copy of all the cells in the row of "NAME" linked to eachother?
I think the question could be a bit clearer, but here goes.
As I understand it you want to copy a row, say 5th row down, after a filter has been applied to hide some rows in between (5th visible row).
Or, you want to find "NAME" and copy the corresponding row, irrespective of what's hidden and what isn't.
(If I haven't got the above right, please let me know and/or consider clarifying the question)
Try something like this;
Dim C as Cell
Dim RowNum as Integer 'If you're looking for the 5th visible cell.
For each C in Range("A:A").SpecialCells(xltypeVisible)
RowNum = RowNum+1 'Count how many visible cells you've looped
If C.Value = "NAME" Then
'Or for the 5th visible cell, use 'If RowNum = 5 Then'
C.EntireRow.Copy
Exit For
End If
Next
Thank you for your answer. Unlukily, the code is not working.
To clarify:
I want to find "NAME" in Column "B" and copy the corresponding row of "NAME", irrespective of what's hidden and what isn't.
Now I have this code:
Dim C As Range
For Each C In Range("B:B")
If C.Value = "NAME" Then
C.EntireRow.Copy
Exit For
End If
Next
The code does copy a row, but not the row that corresponds with "NAME". Instead, it copies the first row in the active sheet.
I want to edit the range in Autofill code below but I'm not sure how.
'find last used cell in the row to the right
Range("B59").End(xlToRight).Select
'auto fill the formula for "Total TV"
ActiveCell.AutoFill Range(ActiveCell, ActiveCell.Offset(0, 1))
At present, the code finds the last valid cell in a row, then it autofills one cell to the right.
What I want to do:
I would to find the last valid cell, then select the active cell and the cells from 3 rows below it too and then autofill one cell to the right.
In pictures it would look like this:
find last valid cell in row 59:
select active cell and cells 3 rows below:
Autofill one cell to the right
Here is one way. No need to Select
Sub x()
Dim r As RANGE
Set r = RANGE("B59").End(xlToRight)
r.Resize(4).AutoFill r.Resize(4, 2)
End Sub
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.
Well, my question is, What is the formula for matching multiple row cell, if Match TRUE else FALSE then show the different row cell value in next column also sheet have blanks cell too so formula avoid blank cell and result is TRUE.
Please follow the sheet
I am using this formula:
{=AND(EXACT(A2:M2,A2))}
and this formula:
=IF(COUNTIF(A2:M2,A2)=13,"TRUE","FALSE")
But it match with blank cell, I need formula to only for existed matching row cell value then result is TRUE and FALSE. and if False then show that row cell column value in next column
Thanks
BN
The three standard formulas use in M2:O2 in the following image are,
=COUNTIF(A2:L2, A2)=COUNTA(A2:L2)
=IFERROR(INDEX(A2:L2, AGGREGATE(15, 6, COLUMN(A:L)/((A2:L2<>A2)*SIGN(LEN(A2:L2))), 1)), "")
=IFERROR(INDEX(A$1:L$1, AGGREGATE(15, 6, COLUMN(A:L)/((A2:L2<>A2)*SIGN(LEN(A2:L2))), 1)), "")
Fill down as necessary.
The conditional formatting rule for B2:L9 was based upon the following formula,
=AND(LEN(B2), B2<>$A2)
I have a project on excel macro, I need to highlight the next last row that has an empty value. example cell A1:A100 have data and the next cell is A101 is empty.
when user click a button it should highlight the cell A101...
If you are certain that you only need column A, then you can use an End function in VBA to get that result.
If all the cells A1:A100 are filled, then to select the next empty cell use:
Range("A1").End(xlDown).Offset(1, 0).Select
Here, End(xlDown) is the equivalent of selecting A1 and pressing Ctrl + Down Arrow.
If there are blank cells in A1:A100, then you need to start at the bottom and work your way up. You can do this by combining the use of Rows.Count and End(xlUp), like so:
Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Select
Going on even further, this can be generalized to selecting a range of cells, starting at a point of your choice (not just in column A). In the following code, assume you have values in cells C10:C100, with blank cells interspersed in between. You wish to select all the cells C10:C100, not knowing that the column ends at row 100, starting by manually selecting C10.
Range(Selection, Cells(Rows.Count, Selection.Column).End(xlUp)).Select
The above line is perhaps one of the more important lines to know as a VBA programmer, as it allows you to dynamically select ranges based on very few criteria, and not be bothered with blank cells in the middle.
try this:
Sub test()
With Application.WorksheetFunction
Cells(.CountA(Columns("A:A")) + 1, 1).Select
End With
End Sub
Hope this works for you.
This does it:
Do
c = c + 1
Loop While Cells(c, "A").Value <> ""
'prints the last empty row
Debug.Print c