[VBA] Autofill selected cells

Alexlin

New Member
Joined
Sep 29, 2018
Messages
15
Hi guys, I’ve encountered a difficult task.
There are two sheets (“sheet1” and “sheet2”)
In sheet1, Column A1:A100 are filled with 5 names (some are blank cells)

In sheet2, there are some cells required to be filled in (I.e.,A13, B23, C18, D50, E5). The value to be filled are exactly the five names from sheet1.

What should the VBA be composed so that no matter how the five names located in different row, they would still automatically be filled into those five no-pattern cells in sheet2?

Thanks a lot!
 
Try this macro. It will also automatically adjust if the number of names in column A changes.
Code:
Sub FillCells()
    Application.ScreenUpdating = False
    Dim Rng As Range, lastRow As Long, sVal As String, Key As Variant, RngList As Object, x As Long, y As Long
    y = 1
    lastRow = Sheets("E&P Allocation").Range("A" & Sheets("Data Input").Rows.Count).End(xlUp).Row
    Set RngList = CreateObject("Scripting.Dictionary")
    For Each Rng In Sheets("Data Input").Range("A20:A" & lastRow)
        If Rng <> "" Then
            If Not RngList.Exists(Rng.Value) Then
                RngList.Add Rng.Value, Nothing
            End If
        End If
    Next Rng
    For Each Key In RngList.keys
        sVal = sVal & "!" & Key
    Next Key
    splitVal = Split(sVal, "!")
    With Sheets("E&P Allocation")
        For x = 14 To lastRow Step 44
            .Range("D" & x) = splitVal(y)
            y = y + 1
            If y > UBound(splitVal) Then Exit For
        Next x
    End With
    Sheets("Data Input").Range("A19").AutoFilter
    Application.ScreenUpdating = True
End Sub

Thanks! It works!!
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top