Consulting

Results 1 to 4 of 4

Thread: Macro to Autofill

  1. #1

    Macro to Autofill

    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

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    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
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    .
    .
    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
    Attached Files Attached Files

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Of course, you could simply double-click the fill button on the cell.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •