ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBA Range Property (https://www.excelbanter.com/excel-programming/368043-vba-range-property.html)

AndrewCrisp[_2_]

VBA Range Property
 

I am new to vba in Excel and have written a macro that works well but
keeps falling over.

The code is as follows:

Dim costcentre As Range
Dim LastSht As Long
Sheets("Header").Select
LastSht = Sheets.Count
Set costcentre = Range("e4")
Sheets("Template").Select
Sheets("Template").Copy after:=Sheets(LastSht)
Sheets("Template (2)").Select
Range("av7").Value = costcentre
Range("Q17:ab17").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone,
SkipBlanks:= _
False, Transpose:=False
Range("q21:ab43").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone,
SkipBlanks:= _
False, Transpose:=False
Sheets("Template (2)").Name = costcentre
Columns("A:O").Hidden = True
Columns("Ai:dd").Hidden = True
'Sheets("Header").Select
'Range("costcentre").Select

It is this last line that falls over. It is ok if I use a specifc
reference eg "e4" but I don't want to do this as I want to offset one
cell down each time through when I add a loop. Can I not use the
declared name to go to a cell?

Any halp much appreciated.

Thanks

Andrew


--
AndrewCrisp
------------------------------------------------------------------------
AndrewCrisp's Profile: http://www.excelforum.com/member.php...o&userid=36653
View this thread: http://www.excelforum.com/showthread...hreadid=564035


Ken Johnson

VBA Range Property
 

Hi Andrew,

Excel is looking for a named range named "costcentre" and probably
can't find such a named range.

Your code has set the variable "costcentre" to represent the range E4,
so I think your last line should be...

costcentre.select

Ken Johnson


AndrewCrisp[_3_]

VBA Range Property
 

Ken

Simple but did the trick.

Many thanks

Andrew


--
AndrewCrisp
------------------------------------------------------------------------
AndrewCrisp's Profile: http://www.excelforum.com/member.php...o&userid=36653
View this thread: http://www.excelforum.com/showthread...hreadid=564035


Ken Johnson

VBA Range Property
 

You're welcome Andrew.
Thanks for the feedback.

Ken Johnson



All times are GMT +1. The time now is 06:22 AM.

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