logo elektroda
logo elektroda
X
logo elektroda

Excel VBA: Update Dynamic Data Range in PivotTable After Removing Duplicates & Columns

lili02103 5364 11
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 16220363
    lili02103
    Level 7  
    Hello,

    At work, I extract a report in Excel with 35 columns (A-AI) and a variable number of rows. I created an Excel file to analyze this data, so one tab contains the pasted data and several other tables and pivot charts based on them. As not all columns from the report are needed by me, I created a macro that:
    - removes duplicates
    - removes unnecessary columns
    - Updates all pivot tables

    Finally, it obtains a table with 17 columns (A to Q) as the basis for the pivot tables. And here I have a problem.
    When I create a PivotTable, I have a data range: 'Data to analyze'! $ A $ 1: $ Q $ 229.
    Unfortunately, after uploading new data to the tab and running the macro, the scope changes to "Data for analysis"! $ A $ 1: $ G $ 697.
    Why does the table data range change (omit HQ columns)?
    The macro looks like this:
    Code: VBScript
    Log in, to see the code
  • ADVERTISEMENT
  • #2 16220782
    JRV
    VBA, Excel specialist
    Better to make a separate worksheet for PivotTable data and re-copy the data you want instead of deleting the columns that the PivotTable is already cached
  • ADVERTISEMENT
  • #3 16235451
    lili02103
    Level 7  
    Thank you very much. I did it and it works. I have one more question. I would like to sort data with a macro, but unfortunately I get an error. I have a table always structured in the same way, but the sheet in which it is located has different names. I tried to remove Worksheets ("reporting 01/27"), changed ActiveWorkbook.Worksheets ("reporting 01/27") to ThisWorkbook did nothing. How do I fix a macro to sorot data in the sheet it was included in, even if the name changes (reporting 27.01, reporting 28.01)?


    ActiveWorkbook.Worksheets ("reporting 01/27"). Sort.SortFields.Clear
    ActiveWorkbook.Worksheets ("reporting 27.01"). Sort.SortFields.Add Key: = Range (_
    "C2: C469"), SortOn: = xlSortOnValues, Order: = xlAscending, DataOption: = _
    xlSortNormal
    With ActiveWorkbook.Worksheets ("reporting 01/27"). Sort
    .SetRange Range ("A1: AI469")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
  • Helpful post
    #4 16235512
    JRV
    VBA, Excel specialist
    instead of ActiveWorkbook.Worksheets ("reporting 27.01")
    simply ActiveSheet

    ps ThisWorkbook is the entire workbook in which the macro
  • ADVERTISEMENT
  • #5 16258058
    lili02103
    Level 7  
    Cool thanks. That's one more question. I would like to remove all spaces from column AE. But I have a bug with the command cell = Trim (cell) How can I fix it?

    Code: text
    Log in, to see the code
  • #6 16258101
    JRV
    VBA, Excel specialist
    cell.text=trim(cell.text)
  • ADVERTISEMENT
  • #7 16258169
    clubs
    Level 38  
    Or

    Code: VB.net
    Log in, to see the code
  • #8 16259240
    lili02103
    Level 7  
    @JRV
    Mam błąd dotyczący linii cell.Text = Trim(cell.Text)
    Run time error „1004” Unable to set the text property of range class

    @clubs
    Mam błąd dotyczący linii .Value = Application.Trim(.Value)
    Run timer error “13” Type mismatch
  • #9 16259305
    JRV
    VBA, Excel specialist
    zmien
    For Each cell In Columns("AE")
    na
    For Each cell In Columns("AE").cells
  • Helpful post
    #10 16259486
    clubs
    Level 38  
    Or

    Code: VB.net
    Log in, to see the code
  • #11 16260091
    lili02103
    Level 7  
    clubs wrote:
    Or

    Code: VB.net
    Log in, to see the code


    Thank you both, but only the macro above works.
  • #12 16261859
    clubs
    Level 38  
    Hello

    It will also work, but in this form
    Code: VB.net
    Log in, to see the code


    I just don't know why you refer to the entire AE column, where in Excel 2010 there are 1048576 rows and the macro has to "grind" the whole range, it is better to set a specific range or the range used, then the macro will work much faster

Topic summary

The discussion revolves around an Excel VBA macro designed to process a report with 35 columns by removing duplicates and unnecessary columns, ultimately creating a PivotTable with a reduced data range. The user encounters an issue where the data range for the PivotTable changes unexpectedly after running the macro. Suggestions include creating a separate worksheet for PivotTable data to avoid caching issues and using `ActiveSheet` instead of specific worksheet names to handle variable sheet names. Additional queries involve sorting data and removing spaces from a specific column, with various solutions provided for handling errors in the macro code.
Summary generated by the language model.
ADVERTISEMENT