98.7% Of all customers recommend us, we're so confident about our results we publish all reviews and stats
View Live Stats View ReviewsForum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Adjust Name Range with VBA (Row is variable) | Excel forum
Adjust Name Range with VBA (Row is variable) | Excel forum
Resolved · Medium Priority · Version 2010
Jose Carlos has attended:
Excel Forecasting and Data Analysis course
Excel VBA Intermediate course
Adjust Name Range with VBA (Row is variable)
I have tried the following first (defining a RowNumber variable that refers to a cell with CountA that will tell me the exact number of cells the range needs to have once we paste new info). It will always start with A1 (but will be good to know how to include a variable for a possible column change). The Name range is called "Data" and it is for a Pivot table.
However, I need to know how to "rename/adjust" an existing NameRange.
This is the code I have tried#
------------------------------------------------
Sub Adjust_Range()
Dim RowNumber As Integer
RowNumber = Range("L1").Value 'no of rows that this speficic set of data will have.
Set Data = Worksheets("Sheet1").Range("A1", "D" & RowNumber)
ActiveWorkbook.Names.Add Name:="Data", RefersToR1C1:="=Sheet1!R1C5:R3C9"
End Sub
------------------------------------------------
RE: Adjust Name Range with VBA (Row is variable)
Hi Jose Carlos,
Thank you for the forum question.
You will not need to use Counta. The below code will always put the whole range reference in you worksheet in the range Data. You can add as many columns or rows you like the code will always know.
Set Data = Worksheets("Sheet1").Range("A1").CurrentRegion
I hope this can help you.
Kind regards
Jens Bonde
Microsoft Office Specialist Trainer
Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us
London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector
RE: Adjust Name Range with VBA (Row is variable)
Thanks Jens,
but before
Set Data = Worksheets("Sheet1").Range("A1").CurrentRegion
Which code do I need to put to make sure the range I want is selected?
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
I have tried with the one above, but DATA range is not created.
Thanks,
RE: Adjust Name Range with VBA (Row is variable)
Hi Carlos,
We have different methods to create/define ranges. The method below will create a range name and each time you change the range and execute the code the range name will update.
Worksheets("Sheet1").Range("A1").CurrentRegion.Name = "Data"
Please let me know if this is not doing what you want.
Kind regards
Jens Bonde
Microsoft Office Specialist Trainer
Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us
London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector
Thu 22 Sep 2016: Automatically marked as resolved.
Training information:
Welcome. Please choose your application (eg. Excel) and then post your question. Our Microsoft Qualified trainers will then respond within 24 hours (working days). Frequently Asked Questions
Any suggestions, questions or comments? Please post in the Improve the forum thread. |
Excel tip:Switching Between SpreadsheetsAs the Alt+Tab key switches between loaded applications or files, Ctrl+Tab switches between loaded or open Excel files. Hold down the Ctrl key until you have tabbed to the correct spreadsheet. |