ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Using "Names" in VBA codes (https://www.excelbanter.com/excel-discussion-misc-queries/113522-using-names-vba-codes.html)

Ed

Using "Names" in VBA codes
 
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


Bob Phillips

Using "Names" in VBA codes
 

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)

"Ed" wrote in message
...
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




Ed

Using "Names" in VBA codes
 
Oh that was quite simple, thanks! and also thanks for the tip, yeah I will
just name what is really needed!

,Ed

Don Guillett

Using "Names" in VBA codes
 
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.

--
Don Guillett
SalesAid Software

"Bob Phillips" wrote in message
...

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)

"Ed" wrote in message
...
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







All times are GMT +1. The time now is 09:48 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com