Hello, I have some issues. Beyond that, Excel VBA is misbehaving.
What I am trying to accomplish: I would like to select a dynamic range, based of last column, last column + 16, etc., then to autofill that range to the last row.
Here is what I have:
This doesn't work. I wouldn't be opposed to using AutoFill with Selection, if you could make that work when Selection is a range. I could then just select the range above, and autofill down to LastRow.Dim LC As Integer LC = Cells(1, Columns.Count).End(xlToLeft).Column Dim LR As Long LR = Cells(Rows.Count, 1).End(xlUp).Row Range(Cells(2, LC + 1), Cells(2, LC + 16)).AutoFill Destination:=Range(Cells(2, LC + 1), Cells(2, LC + 16) & LR)
Something like:
That doesn't work, either (Type mismatch error). I know it's because I do not know the proper syntax for AutoFill, and the problem is that I am having a hard time finding a solution that uses dynamic columns and rows with autofill.Range(Cells(2, LC + 1), Cells(2, LC + 16)).Select Selection.AutoFill Destination:=Range(Selection & LR)
Any help is greatly appreciated. Thank you in advance.
Last edited by goldenr2; 12-05-2011 at 10:29 PM.
Hi Goldenr2
It's difficult to see what you're trying to achieve. The first LC+1 will end up outside the populated range. You need to be within the populated range when selecting your first cell.
Also, your syntax for LR in the Range statement is incorrect. LR is a row number, and should appear within the Cells statement.
If I understand correctly, you will have to fill right, to LC+16, then fill down, to LR. You cannot fill in two directions at the same time.
Let me know if I've misunderstood, and ideally post a dummy sheet so we can see the problem.Dim LC As Integer LC = Cells(1, Columns.Count).End(xlToLeft).Column Dim LR As Long LR = Cells(Rows.Count, 1).End(xlUp).Row Cells(2, LC).AutoFill Destination:=Range(Cells(2, LC), Cells(2, LC + 16)), Type:=xlFillDefault Range(Cells(2, LC + 1), Cells(2, LC + 16)).AutoFill Destination:=Range(Cells(2, LC + 1), Cells(LR, LC + 16)), Type:=xlFillDefault
Regards, Rob.
Sorry for not being as clear as I could have. I will give a bit more of an explanation.
I generate a spreadsheet that has a number of columns and rows. That number changes every time I generate the spreadsheet. In the first empty column Cell(1, LR + 1) I enter a title via macro. In the row below, Cell(2, LR + 1) I enter a formula via macro. It looks something like this:
I do this for a range, one column at a time, for the first 16 Columns after the Last Column of data in Row 1. What this leaves me with is 16 columns with row 1 being titles and row 2 being formulas (and some empty cells). The location of these columns will change every time I generate these spreadsheets.Cells(1, LC + 1).FormulaR1C1 = "Shipments" Cells(2, LC + 1).FormulaR1C1 = _ "=VLOOKUP(RC[-71],'Lookup Table'!C[-71]:C[-70],2,FALSE)"
What I want to do next is copy row 2 of those 16 columns down to the last row of data in column 1, or "A". I have attached a dummy sheet for clarification.
Edit: After using your code:
It worked beautifully!!! I have been wracking my brain over this, and it was such a simple fix. Thank you!Range(Cells(2, LC + 1), Cells(2, LC + 16)).AutoFill Destination:=Range(Cells(2, LC + 1), Cells(LR, LC + 16))
Last edited by goldenr2; 12-05-2011 at 10:29 PM.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks