0

I have been trying to use a named range and variable to define a range that I want to autofill and I can only get one of the named ranges to work and not the variable in my code. The ColLetter variable is what is giving me trouble. If the I omit that variable and just put "AR" in the range the code works it is just not dynamic.

Sub test()

    Dim lastRow As Long
    Dim ColLetter As Range
         lastRow = Range("D" & Rows.Count).End(xlUp).row 'Counts rows in specific column which controls how far to autofill.
         ColLetter = Mid(ActiveCell.Address, 2, 2)
         'Range("Notes_Start").AutoFill Destination:=Range("Notes_Start:AR" & lastRow) 'This works just is not dynamic.  The AR for column is static.
         Range("Notes_Start").AutoFill Destination:=Range("Notes_Start:ColLetter" & lastRow)
End Sub

2 Answers 2

1

A couple of problems, Dim ColLetter As Range should really be Dim ColLetter As String, your string concatenation is wrong, change Range("Notes_Start:ColLetter" & lastRow) to Range("Notes_Start:" & ColLetter & lastRow)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this fixed my code Jeanno. Syntax got me again. @FreeMan is right I should probably stop using letters for column references.
0

Be very careful using letters to define columns in your code.

  • If your active cell is A1, Mid(ActiveCell.Address, 2, 2)
    • yields A$, which is what you want.
  • If your active cell is AA1, Mid(ActiveCell.Address, 2, 2)
    • yields AA. You lose the $ and your reference is broken.

You would probably be better off using numerical references to columns:

Dim Col as integer
Col = ActiveCell.column

I'm not familiar with the Range("Notes_Start:..." syntax, but I believe that you could use Range("Notes_Start").Offset(lastrow,Col) to get what you're after. The Offset() may need a tweak of +1 or -1 to adjust to exactly where you need to be. Some quick trial & error will show exactly what you need to do.

You could use:

Dim adr() as string
Dim ColLetter as string
adr = split(ActiveCell.Address, "$")
ColLetter = adr(0)

But I'd still recommend using column numbers...

4 Comments

Thanks for the quick reply. The "Notes_Start" is my named range which is a single cell. I am still getting a runtime error '1004': AutoFill method of Range class failed. Is this the way you intended the code below? Sub test1() Dim lastRow As Long Dim Col As Integer Col = ActiveCell.Column lastRow = Range("D" & Rows.Count).End(xlUp).row 'Counts rows in specific column which controls how far to autofill. Range("Notes_Start").AutoFill Destination:=Range("Notes_Start").Offset(lastRow, Col) 'This works just is not dynamic. The AR for column is static. End Sub
If Notes_Start is a named range, you would refer to it as Range("Notes_Start"). I though that maybe it was a large range of cells and that the Notes_Start:<row>, <col> was a syntax I was unfamiliar with for referring to a cell within that named range.
What range are you trying to refer to with Destination:=Range("Notes_Start:ColLetter" & lastRow)
In this instance "Notes_Start" = $AR$8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.