Using "Names" in VBA codes

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello I would like to know in which way I can have values as "Names" or
something like that to use them in a code... to replace for example in the
code below:

Worksheets("Budget").Range("G24")

With something like a Name, so if I happen to add or delete rows above row G
the reference is still valid, and it doesn't just take G24 regardless of what
it contains.

Sub Data()
Dim LastRow As Long
With ActiveSheet
LastRow = .Range("B1").Value
Range("b19:f" & LastRow).FillDown

Range("c" & LastRow + 2) = Worksheets("Budget").Range("G24")
Range("f" & LastRow + 2) = Worksheets("Budget").Range("H24")

Range("c" & LastRow + 3) = Worksheets("Budget").Range("G25")
Range("f" & LastRow + 3) = Worksheets("Budget").Range("H25")

Range("C" & LastRow + 5) = "TOTAL"
Range("C" & LastRow + 5).Select
Selection.Font.Bold = True

Range("f" & LastRow + 5) = Worksheets("Budget").Range("H27")
Range("f" & LastRow + 5).Select
Selection.Font.Bold = True

End With
End Sub
 
Range("c" & LastRow + 2) = Worksheets("Budget").Range("myG24Name")
Range("f" & LastRow + 2) = Worksheets("Budget").Range("myH24Name")

etc.

--

HTH

Bob Phillips

(replace xxxx in the email address with gmail if mailing direct)
 
Oh that was quite simple, thanks! and also thanks for the tip, yeah I will
just name what is really needed!

,Ed
 
Naming ranges is great but best not to use names for no good reason. I have
clients pay me to remove them and restore formulas.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Macro range 2
Macro copy from range 3
end it 2
Range error 1
external links 2
After Macro runs, no cell activated - what am I missing? 2
Collate data 1
VBA open file from a form 3

Back
Top