Autofill across and above a dynamic range - excel

I'd like to autofill cells across and above a dynamic range.
I have a line of numbers in row 3 and would like to put the word "Customer No." in the cell above each one.
I do this by copying A2 and pasting into C2 then dragging across
Via VBA macro recorder the code I get looks like this
Selection.Copy
Range("C2").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("C2:E2"), Type:=xlFillDefault
Range("C2:E2").Select
I was wondering if there's a way to create an autofill across a dynamic range as the number of cells in row 3 will change from time to time?
Thanks

You could do it like this:
With ActiveSheet
.Range("C3", .Cells(3, Columns.Count).End(xlToLeft)).Offset(-1).Value = .Range("A2").Value
End With

Related

Excel VBA to Select Rows Down That Include Blanks

I'm trying to create a macro to copy cells down an entire column of a table in hopes of acting as a 'refresh' in case those formulas were altered or replaced.
I have multiple tables in the same sheet so I can't select the table name because they constantly change.
What I'm thinking of is having a bottom row with a keyword that VBA can select down until they hit the keyword and select those cells to copy the formulas down.
The thing is that I have multiple tables and they would all have the bottom row of keywords.
When I recorded a macro, I have to Control+Shift+Down multiple times to account for missing rows which I imagine wouldn't always be the case. This is what scares me for this macro since sometimes a table would have no missing data so the xlDown function would select more data than it should.
Here is what I recorded:
Sub Macro9()
'
' Macro9 Macro
'
'
ActiveCell.Offset(3, 2).Range("A1").Select
Range(Selection, Selection.End(xlToLeft)).Select
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
End Sub
Here is an example of the column I am trying to restore formulas on:
I want the formula below "Total Price" to fill down until it hits the word "Total". Note that formulas are hidden if there is no data elsewhere in the sheet.
There are multiple tables on this sheet so this would have to work in different sections of te same sheet.
If you have actual Tables/Listobjects then you could do something like this:
Sub FillDownFormulas()
Dim lo As ListObject, col As Range
For Each lo In ActiveSheet.ListObjects
For Each col In lo.DataBodyRange.Columns
If col.Cells(1).HasFormula Then 'first cell in column has a formula?
col.Formula = col.Cells(1).Formula 'fill formula to rest of column
End If
Next col 'next column
Next lo 'next table
End Sub
One of the most simple ways (for people that usually don't use VBA) is to get the number of the last row in your table. You can do that by counting values in table or with your own code by using a column that is always filled, like:
last_row = Range("B2").end(xldown).row
With last_row value you can fill your formula in ranges, like:
Range("C2").value = 'Your Formula here
Range("C2").AutoFill Destination:=Range("C2:C" & last_row)
You can do that to every column that you want.

Excel VBA: After Filtering column, to replace filtered cells value dynamically

Like to input into Excel VBA:
Have a Column (V), after filtered for "#N/A"
To replace with formula (=ColC -Col V)
And FillDown rest of Filtered cells in ColV
Have a Column (D), after filtered for "TBA"
To paste into Columns beside (ColE,F,G,H)
And FillDown rest of Filtered cells
Have a Column (J), after filtered for "Blank"
To replace with "TBA"
And FillDown rest of filtered cells in ColJ
For MS Windows Excel 2016
'Record Macro'
'First Case
ActiveSheet.Range("$AA$1:$AA$65303").AutoFilter Field:=1, Criteria1:="="
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RC[-5]+RC[-1]"
Range("AA365").Select
Range("AA365").Select
Selection.FillDown
'Second Case
Columns("L:L").Select
Selection.AutoFilter
ActiveSheet.Range("$L$1:$L$65303").AutoFilter Field:=1, Criteria1:="TBA"
Range("L488").Select
ActiveCell.FormulaR1C1 = "TBA"
Range("L479:L488").Select
Selection.FillRight
Range("O65307").Select
Thank You
Feel free if need clarification
You can use Replace or SpecialCells(xlCellTypeVisible), don't really understand everything You want, but i think these hints are everything You need to do this code :)
lLastR = .cells(rows.count,1).end(xlup).row
.Range("J2:J" & lLastR).Replace "", "TBA"
.Range("F2:F" & lLastR).SpecialCells(xlCellTypeVisible) = 1

Change formatting of a column to text

Every time I run this code the entire sheet becomes Text.
I want to keep the rest of the sheet General, but change column A to Text.
For the
Columns("A").Select _
ActiveCell.NumberFormat = "#"
I tried
Range("A:A").Select
Selection.NumberFormat = "#"
I tried writing it with the specific workbook instead of letting VBA assume the active worksheet and cells.
I tried to NumberFormat the other columns to be General after running the formatting on column A.
Columns("A").Select
Range("A1").Activate
Selection.Delete Shift:=xlToLeft
Columns("A").Select
ActiveCell.NumberFormat = "#"
Range("A1").Select
ActiveSheet.Paste
Application.CommandBars("Office Clipboard").Visible = False
I expected that if I put another line after my numberformat that I would be able to get column B:ZZ to all be General.
I've gotten no error messages.
Just tested these, both work. Formulas referencing the affected cells do not compute, formulas referencing other numeric cells are calculating as normal.
Range("A:A").NumberFormat="#"
Range("A1").EntireColumn.NumberFormat="#"

Microsoft Excel VBA code range based on cell contents

I have some VBA code, and I have a problem with a range.
In my excel sheet, I want the range to be based on the contents of cell C2.
Here is the VBA code,
Sub Repeat()
'
' Repeat Macro
'
'
Range("A1:A3").Select
Selection.AutoFill Destination:=Range("A1:A19"), Type:=xlFillDefault
Range("A1:A19").Select
Range("B1").Select
End Sub
However, instead of A19 I would ideally like to use the AX with X being the value in cell C2, if cell C2 contains 24 I would like it to say A24 instead of A19 any ideas?
Try
Range("A1:A" & Range("C2").value)
But you should qualify with the sheet name as well.
For example:
With Worksheets("Sheet1")
.Range("A1:A3").AutoFill Destination:= .Range("A1:A" & .Range("C2").value) , Type:=xlFillDefault
End With

Macro to Auto Fill Down to last adjacent cell

I am trying to use macro recorder in Excel to record a macro to fill down a column of cells, however because the fill down each time is a different number of cells it either fills down to short or too long and this seems to be because the macro identifies the cell range and its fixed.
Is there anyway that I can get it to fill down to the last populated neighbouring cell. E.g. AutoFill down column E until it reaches the last populated row in column D. I have looked at some examples on here but the code all looks very different so not sure if it can be done with macro recorder or I have to get someone to write some code or is it something that has to be done manually?
This is the code that I have in the macro.
ActiveCell.FormulaR1C1 = _
"=IF(MONTH(RC[-1])>3,"" ""&YEAR(RC[-1])&""-""&RIGHT(YEAR(RC[-1])+1,2),"" ""&YEAR(RC[-1])-1&""-""&RIGHT(YEAR(RC[-1]),2))"
Selection.AutoFill Destination:=Range("E2:E1344")
'Selection.AutoFill Destination:=Range("E2:E1344")
Range("E2:E1344").Select
If anyone can help i'd be extremely grateful
Untested....but should work.
Dim lastrow as long
lastrow = range("D65000").end(xlup).Row
ActiveCell.FormulaR1C1 = _
"=IF(MONTH(RC[-1])>3,"" ""&YEAR(RC[-1])&""-""&RIGHT(YEAR(RC[-1])+1,2),"" ""&YEAR(RC[-1])-1&""-""&RIGHT(YEAR(RC[-1]),2))"
Selection.AutoFill Destination:=Range("E2:E" & lastrow)
'Selection.AutoFill Destination:=Range("E2:E"& lastrow)
Range("E2:E1344").Select
Only exception being are you sure your Autofill code is perfect...
This example shows you how to fill column B based on the the volume of data in Column A. Adjust "A1" accordingly to your needs. It will fill in column B based on the formula in B1.
Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, 1).Select
Range(Selection, Selection.End(xlUp)).Select
Selection.FillDown
ActiveCell.Offset(0, -1).Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, 1).Select
Range(Selection, Selection.End(xlUp)).Select
Selection.FillDown

Resources