Vba range object single column autofill

0 votes

I'm attempting to automatically populate one range object's column. Eventually, I want to autofill using a formula. I tested this out using the below toy example. This results in a type mismatch error that I am unable to resolve.

Sub Macro1()
    
    Dim wk As Worksheet
    
    Set wk = ActiveWorkbook.Sheets("Temp")
    
    wk.Cells.Clear
    
    Dim rng As Range
    
    Set rng = wk.Range("A1:B5")
    
    rng.rows(1).Columns(1) = 1
    
    rng.rows(1).Columns(1).Select
    
    Selection.AutoFill Destination:=rng(1 & ":" & rng.rows.Count, "A")
    
End Sub

A vba newbie here. 

Apr 2, 2023 in Others by narikkadan
• 63,720 points
376 views

1 answer to this question.

0 votes

Just refer to the first column of rng. Change

rng.rows(1).Columns(1) = 1

rng.rows(1).Columns(1).Select
    
Selection.AutoFill Destination:=rng(1 & ":" & rng.rows.Count, "A")

to

With rng
    .Cells(1).Value = 1
    .Cells(1).AutoFill Destination:=.Columns(1)
End With

If you want to autofill from row 3 onwards:

With rng.Columns(1).Cells(3)
    .Value = 1
    .AutoFill Destination:=.Resize(rng.Rows.Count - 2)
End With
answered Apr 2, 2023 by Kithuzzz
• 38,020 points

Related Questions In Others

0 votes
1 answer

VBA Loop to select then copy a range of cells based on value in column B

Try this: Sub Macro2() Dim ...READ MORE

answered Mar 23, 2023 in Others by narikkadan
• 63,720 points
2,195 views
0 votes
1 answer
0 votes
1 answer

Excel-VBA - How to identify Target range (more than 1 cell) is deleted in a Worksheet_Change function?

You misunderstand the purpose of the function ...READ MORE

answered Sep 23, 2022 in Others by narikkadan
• 63,720 points
3,573 views
0 votes
1 answer

Is there any way in python to auto-correct spelling mistake in multiple rows of an excel files of a single column?

Use Spellchecker for doing your stuff: import pandas ...READ MORE

answered Oct 14, 2022 in Others by narikkadan
• 63,720 points
1,942 views
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,740 points
1,099 views
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
3,524 views
0 votes
1 answer

Using VBA Excel to create a gramatically correct list

The Excel AND function is a logical ...READ MORE

answered Feb 9, 2022 in Others by gaurav
• 23,260 points
824 views
0 votes
2 answers

How to copy a formula horizontally within a table using Excel VBA?

Hi so basically, create an adjacent column ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,690 points
983 views
0 votes
1 answer
0 votes
1 answer

What VBA code would I use to concatenate cell A2 & B2 in cell C2 and then have it Autofill down the column?

Solution Find the last row. Write a formula to ...READ MORE

answered Feb 14, 2023 in Others by Kithuzzz
• 38,020 points
897 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP