Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7848055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:04:26+00:00 2026-06-02T18:04:26+00:00

Copying the values of a filtered range to an array seems to work without

  • 0

Copying the values of a filtered range to an array seems to work without a problem: the array then contains values from both filtered and unfiltered cells. However, when I copy the array’s contents back to the filtered range, the results are incomprehensible to me.

Here’s my code:

Sub test()
    Dim rangecopy() As Variant

    rangecopy() = Range(Cells(2, 1), Cells(14, 3)).Value
    For c = LBound(rangecopy, 1) To UBound(rangecopy, 1)
        rangecopy(c, 1) = c
        rangecopy(c, 2) = c * 10
        rangecopy(c, 3) = c * 100
    Next
    Range(Cells(2, 1), Cells(14, 3)).Value = rangecopy()
End Sub

It is supposed to give the following result. Here, the range was unfiltered when the macro copied the array to it.

Imgur

If the range is filtered by column D (“NO” is filtered out), the result looks like this:

Imgur

First, the filtered cells aren’t updated. Then, most cells from column B get values from the array’s first column (4, 5, 6), while a few others get values from the array’s second column correctly (10). The last two rows are filled with #N/A error. Is this supposed to work that way? I’m using Office 2010.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T18:04:26+00:00Added an answer on June 2, 2026 at 6:04 pm

    I really hope someone with knowledge of the internal workings of VBA can provide more insight on your question. I can share the following:

    First, it is working as intended. However, I don’t know why this is the design, nor what exactly is happening in the assignment process.

    There are many cases that create a similar issue. For instance, if you have the filter on (some rows are hidden) and try to fill (drag) a formula down, you will see similar results, in that hidden rows aren’t populated, but they do affect the (relative) references in the formula. On the other hand, if you manually copy and paste into a filtered range, the data is pasted into the hidden rows (as you intend).

    It seems that any range referenced that is part of the Autofilter range is actually non-contiguous*. Using Range.Address does not always reveal this, nor does looping through Range.Areas. If we modify your example, we can see where the “real” error is:

    Dim r1 as range
    Dim r2 as range
    
    Set r1 = Sheet1.Range("A1:B5") 'some numbers in a range
    Set r2 = Sheet2.Range("A2:B6") 'same-size range underneath a filtered header
    
    r1.Copy Destination:=r2
    

    It works when all the rows are visible. When the filter on Sheet2 creates hidden rows, the result is “Run-time error ‘1004’: …the Copy area and the paste area are not the same size and shape.” On the other hand, using the “manual” / clipboard method works for hidden rows:

    r1.Copy    
    r2.PasteSpecial (xlPasteValues)
    

    Since assigning an array to a range bypasses the clipboard (as in the 1st block), we ought to receive an error (instead you just end up with erroneous results).

    The only solutions I’m aware of are to either loop through the range and assign a value to each cell:

    For i = 1 to LastRow
      For j = 1 to LastCol
        Sheet1.Cells(i,j).Value = myArr(i,j)
      Next
    Next
    

    OR (better) remove the Autofilter, assign the array to the range, then reapply the filter.

    *technically it’s contiguous, so it may be better to say that the range is composed of several ranges/areas, although using .Address doesn’t indicate this and there is only one area when you try to loop through Range.Areas

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ive written the following method which sorts an array by copying the biggest values
Is there any way to to it without copying values?
I have two arrays. The first array contains some values while the second array
I am copying several fields' values from one list item to another. If I
Database : SQL Server 2005 Problem : Copy values from one column to another
Hello i am having problem in copying data from one NSDictionary to another i
I'm copying phone number from iPhone address book to a text field. In text
I am copying a stored procedure from one database to another. I am currently
I am copying code from website matplotlib and pasting into the Vim editor in
I am copying a file to a directory, and then setting an environment variable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.