1

I have become stuck trying to solve a piece of code I thought would be very simple. I have defined a range (2 rows, 150 columns) and transferred it to an array. I then want to use the defined array in multiple ranges (same size of 2 rows and 150 columns). I have written the following code:

Dim LocalArray As Variant
LocalArray = .Range("FD6781:KW6782").Value2
.Range("FD6839:KW6840,FD6955:KW6956,FD7013:KW7014,FD7071:KW7072").Value2 = LocalArray

The issue is that every second range defined in .range("FD6839:KW6840,FD6955:KW6956...") shows up as N/A. Hence range FD6839:KW6840 is correct while range FD6955:KW6956 is wrong. What have I done wrong in the above code?

Thank you!

4
  • Have you tried assigning the ranges separately, rather than attempting all four at once? This is the first step I would take. Commented Nov 1, 2018 at 13:01
  • Yes, it works when doing them separately. I was just trying to limit the amount of code, and then I don't understand why above wouldn't work. Commented Nov 1, 2018 at 13:08
  • @Philip Good question. Solution may be looping.But real question is why it is behaving like this? Commented Nov 1, 2018 at 13:20
  • I tried it with 2 Col X 5 Row data. it is behaving weird only in 2nd, 4th ranges. if 2nd and 4th ranges are kept 5 col X 2 row, data is being weirdly restructured in 2nd and 4th ranges. .Range("A11:B15,A21:E22,A31:B35,A41:E42").Value = .Range("A1:B5").Value. However if square shaped ranges (i.e. rows=col) used the syntax is working correctly (I tired up to 8th range with 5X5 range) Commented Nov 1, 2018 at 14:12

1 Answer 1

1

You could loop. Otherwise, it does seem to be related to the number of columns causing the issue. Seems odd.

Option Explicit
Public Sub test()
    Dim localArray(), rng As Range, ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Sheet3")
    localArray = ws.Range("FD6781:KW6782").Value2

     For Each rng In ws.Range("FD6839, FD6955, FD7013, FD7071")
         rng.Resize(UBound(localArray, 1), UBound(localArray, 2)) = localArray
     Next

End Sub
Sign up to request clarification or add additional context in comments.

4 Comments

Completely agree with dropping the intermediary array. Also, at least with sample data, there was no need for Union: ws.Range("D1:E2,G1:H2,J1:K2") = arr.
Hi both, when using that method I get the same problem as previously (i.e. every second range is wrong).
Just tried with the updated code QHarr, that works perfectly! :) Thank you!
I recommend using rng.Resize(UBound(localArray, 1), UBound(localArray, 2)).Value = localArray instead of the implicit (with no .Value) in order to make it clear you are transfering values and not assigning references.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.