You are here:

Excel/How to select ranges in a macro when the range keeps changing?


I have a report where I am extracting data and then refreshing pivot tables.  I do not know how to read visual basic very well.  In my macro I need to sort data and then delete duplicates before refreshing the pivot tables.  When I refresh the macro to sort or delete the data range only goes to a finite cell address instead of capturing any new data from the data new extraction.  How do I change my macro or keystrokes to accommodate this?

It's difficult to answer without seeing the code, but Alt F11 will get you into the macro - it PROBABLY has something that sets the pivot table data to a range - perhaps something like

activeSheet.PivotTableWizard SourceType:=xlDatabase, SourceData:= _

in this instance it says row 1 column 1 to row 3 column 3 - so it would be easy to amend this - for example

activeSheet.PivotTableWizard SourceType:=xlDatabase, SourceData:= _
       "Sheet1!R1C1:R" & SomethingThatcountCells & "C3"

where SomethingThatCountsCells is a variable containing the number you want - happy to help further so do ask any follow up questions you need - if it helps, my direct email is - question found in question pool indicating the original expert you asked was unable or unwilling to answer the question.
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Aidan Heritage


I have provided first hand support since `95 for Microsoft Office majoring in Word and Excel - support for all versions from 2 onwards. I'm based in the UK, so please allow for time differences when asking me questions from other parts of the world!


My background is in the insurance industry and call centre areas, but have been called upon to provide many varied solutions.

I'm educated to UK A level standard, but as I left school some 30 years ago that is rather irrelevent - university of life has provided more of a background!

©2017 All rights reserved.