Solved

Autofill Dynamic Range

Posted on 2013-12-23
6
571 Views
Last Modified: 2013-12-23
I'm creating a macro that fills a range that changes every time it is run. The only thing consistent about the range is that it begins on row 7 of the active cell. The column filled changes every time. The number of rows may change. Therefore code like the following will not work.

Range("A2").AutoFill Destination:=Range("A2:A" & LastRow)

Open in new window

I need something more dynamic like this.
ActiveCell.AutoFill Destination:=Range(ActiveCell.Address & ":" & ActivceCell.Column & LastRow)

Open in new window

That will not work because the column property of the active cell returns a number, not a column letter. I maybe could try to convert the number to a letter, but it gets complicated after the letter Z.
Is there any way to auto-fill a completely dynamic range? (Well, as close to dynamic that I can think of.)
I also have tried substituting the Cells function inside of the Range function. No go.
0
Comment
Question by:maderitetech
  • 3
  • 3
6 Comments
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 39736814
Hi,

pls try
ActiveCell.AutoFill Destination:=Range(ActiveCell, Cells(LastRow, ActiveCell.Column))

Open in new window


Regards
0
 

Author Comment

by:maderitetech
ID: 39736830
Rgonzo1971,

Returns the same error when I tried to use Cells() - Runtime 1004
Method 'Range' of object'_Global' failed.
0
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 39736838
Hi,

How do you define LastRow?

regards
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:maderitetech
ID: 39736858
LastRow is Long value, obtained by using the following code

LastRow = ActiveSheet.UsedRange.Rows.Count

Open in new window

0
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 39736892
Hi,
if you want to use the letter pls try

LastRow = ActiveSheet.UsedRange.Rows.Count
strColumn = Split(ActiveCell.Address, "$")(1)
ActiveCell.AutoFill Destination:=Range(ActiveCell, Range(strColumn & LastRow))

Open in new window

0
 

Author Comment

by:maderitetech
ID: 39736907
I was just heading that way myself, since I didn't see anything else that would work.
You're right. That works.
What a pity we still have to parse strings.
Thank you!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now