VBA replace value with cell above until blank and loop - excel

I'm looking to fill values down based on the cell above until blank cell, this should continue until there is no more content, for example: refer the below table, copy the top value down until blank, therefore, 'petty msa' will copy down to 'inter' and stops, 'petty ksm' will copy down until 'general', 'comp' will copy down until 'motor'.
Column: A
petty msa
inter
petty ksm
welfare
water
prepay
general
comp
travel
motor
Edit: This is a long list of approx 15,000 rows
Thanks

Select the first cell where you have value 'petty msa' then-
1. press ctrl+shift+down -to select the range to be copied
2. press ctrd+down - to copy down the values
Now you have copied 'petty msa' upto the cell containing 'inter'
repeat this process for each range.

given your data "structure" as per your example, this should do:
Option Explicit
Sub main()
Dim area As Range
With Worksheets("filldown").Columns("A").SpecialCells(xlCellTypeConstants) '<--| change "filldown" to your actual worksheet name and "A" to actual index of column with data to be "filled down"
For Each area In .Areas
area.Value = area(1)
Next area
End With
End Sub

Insert a blank row in A1. In cell B2 paste the below formula and copy it down.
= if(A1="",A2,B1)
This should work

Related

Filter on Cell Value Inside a Table

I have a table from A2 to A7. The cell A1 doesn't belong to that table.
As long as the Cell A1 is empty the below macro works properly, but when I enter any value in cell A1 the macro stops working. The VBA debugger says: The AutoFilter Method of the Rang cannot be run (ErrorCode 1004).
Public Sub FilterOnCellValue()
Dim nField As Long
With ActiveCell
nField = .Column - .CurrentRegion.Cells(1).Column + 1
.CurrentRegion.AutoFilter Field:=nField, Criteria1:=.Value
End With
End Sub
The Question: How can I limit my macro to filter only inside the table?
Update:
I want to use this macro in all tables and all files and not in a certain table. Is there any solution?
just pick th one your heart prefers
Give a name to your table and use the table.range.filter, instead of CurrentRegion.
ActiveSheet.ListObjects("mytable").Range.AutoFilter
insert a blank and hidden row between A1 and your table

How to drag a formula down (autofill) using VBA after finding the first blank cell

I need some help dragging a string ("Decliners") down a column in excel. I know how to do this if I knew what cell was my starting point, but I first have to find the first blank row in my data set. Once I've found my first blank row, I need to drag my string down from there in column C3. This string is being dragged down just one column. I also don't know the range of this data set, given that it is dynamic.
Essentially I just need to recreate the action of double clicking the bottom right of the cell and the word "Decliners" fill to the bottom of the data set.
Code to select the first blank cell in worksheet:
Dim Pastesheet As Worksheet
Dim Decliners As String
Decliners = "Decliners"
Set Pastesheet = Worksheets("Ent Gainers_Decliners")
Pastesheet.Range("C3").End(xlDown).Offset(1, 0).Select
'Where I need the word "Decliners" dragged down from the cell selected
With Pastesheet
.Range(.Range("C3").End(xlDown).Offset(1),.Cells(.Rows.Count,4).End(xlUp).Offset(,-1)).Value = Decliners
End With
This piece of code will set the value of the variable Decliners from the row after the last data set from Range C3 down until the last corresponding row of used data in column D for column C.

Expand a copy Range

I have a button driven macro that copies a range of cells to the clipboard (so I can place in other documents):
Sub Button2_Click()
'
' Button2_Click Macro
Range("A1:p43").Copy
End Sub
Form time to time I need to either add or delete a row in that range in the worksheet. What I need is for the copy range to expand (or contract) to the number of rows containing the values. Note: the columns will not adjust, just the rows.
If the copy range is a contiguous range of values with a blank column in column Q and a blank row below row 43, you can use the CurrentRegion property like this:
Range("A1").CurrentRegion.Copy
It's basically the same as clicking A1 and then using Ctrl-Shift-Down arrow and Ctrl-Shift-Right arrow.

How can I duplicate different rows in Excel?

I have single values in Ad Group column that I need to duplicate in the blank cells in the rows below. There should be 3 of each Ad Group name.
My first screenshot shows how it currently is:
My second screenshot shows how I need it:
There are 30k rows so I need a formula or dynamic way to achieve this.
Here is my answer:
Using VBA:
Sub selectBlankCells()
Dim r 'to store the number of the last row of data
Dim rng As Range
r = Range("A1").End(xlDown).Row 'to find the last row
Set rng = Range(Cells(1, 2), Cells(r, 2)) ' set the range of column B, of the data
rng.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C" ' this is where you put the formula to
'set the data of above cell in every empty cell of
'column B
End Sub
Or you can use the Excel option:
Select the the data of column B, from the last row to the cell B1, then press F5, and this window appears.
Then press Special...
After that, select Blanks and OK
You will select only the blank cells, and then you can put the formula to link the above cell from the active cell.
This way using ([Ctrl]+[Enter] is needed to put the formula into all of the cells at once as Jeeped says):
You will get this:

Is it possible to select a range of data via a macro if the start and end row is variable?

I need to add a piece of code to my macro that copies and pastes a range of data.
The problem that I am having is that the data can start and finish on any row.
Is it possible to do this?
e.g. Currently the data starts in cell C10 and ends in cell I20. But next month it could start in C5 and end in I40.
The column range will always remain the same i.e C:I, but the row will change each time.
How to a add code to search for the first instance of data in Column C and copy all the data that follows?
Thanks
Here are some code snippets that might help
Get the first cell in C
If Range("C1").Value <> "" Then
Set firstcell = Range("C1")
Else
Set firstcell = Range("C1").End(xlDown)
End If
I test C1 because it will go all the way to the bottom if that is the only cell in C with values.
After that, to get the bottom cell after that
Set lastcell = Cells(Rows.Count, "I").End(xlUp)
Another way might be to use a named range in the spreadsheet to define the range you want to copy.
=OFFSET(C1,MATCH(TRUE,INDEX(NOT(ISBLANK(C:C)),0),0)-1,0,COUNTA(C:C),7)
That formula will select any number of data rows starting anywhere in column C. It doesn't account for header rows or any other content. If your data would always start in at least, say, row 5, then you could adjust the C1 value and use C5:C1000 instead of C:C. Then, in the VBA you can just use Range("namedRange"). Copy instead of having to work out the start and end points.
If you highlight the required cells before running the macro, then you can use
Selection
to refer to the selected range
ie, instead of
Range("C10:i20").Clear
you can write
Selection.Clear

Resources