PDA

View Full Version : [SOLVED] Macro to Autofill



mrozelle5900
04-25-2017, 04:59 PM
Hi all

I am looking for a macro to autofill contents where the cell referenced is remaining the same

The name of the sheet will always be in Cell D2

I would like to fill that name down the length of column D given the length of data in column c

For examples

Column C Column d
1 NAME
2 NAME
3 NAME
4 NAME

Column C could have 400 rows of data or it could have 1,000 rows of data

Thank you for any help you can provide

mdmackillop
04-26-2017, 03:06 PM
Sub FillIt()
[D2].AutoFill Range([D2], [D2].Offset(, -1).End(xlDown).Offset(, 1))
End Sub


or a generic solution

Sub FillIt()
Dim c As Range, col As Long
Set c = ActiveCell
col = InputBox("Enter column offset to be matched +/- x")
c.AutoFill Range(c, c.Offset(, col).End(xlDown).Offset(, -col))
End Sub

Logit
04-26-2017, 07:13 PM
.
.
Paste in a Routine Module, call from CommandButton on sheet:



Option Explicit


Sub copyDownHeaders()
Dim sht As Worksheet
Dim lastRow As Long
Dim curRow As Long
Dim headerRow As Long

Set sht = ThisWorkbook.Worksheets("Sheet1")
'edit to your sheetname ------

lastRow = sht.Cells(sht.Rows.Count, 3).End(xlUp).Row
headerRow = 1

For curRow = 2 To lastRow
If sht.Cells(curRow, 3) = "" Then
sht.Cells(curRow, 4) = sht.Range("D2").Value
End If
Next curRow
End Sub

mdmackillop
04-27-2017, 02:06 AM
Of course, you could simply double-click the fill button on the cell.