Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Excel 2013 VBA 'Range on another worksheet' question

Posted on 2014-12-05
4
Medium Priority
?
298 Views
Last Modified: 2015-01-28
I'm trying to dump the data in an array in a range of cells on the sheet that's not the ActiveSheet.

 
  Sheets("Sheet2").Range(Cells(5, 4), Cells(36, 13)).Value = arrMIDIEvents

Open in new window


I get an 'Application-defined or Object-defined error'.

However, it works if I Activate the other sheet and dump it. But I'd like to avoid leaving the ActiveSheet. Isn't that possible?

Thanks
0
Comment
Question by:hindersaliva
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Author Comment

by:hindersaliva
ID: 40484031
Nope. Sorry Simon. It made no difference.
0
 
LVL 18

Accepted Solution

by:
Simon earned 1600 total points
ID: 40484037
Sorry, I deleted my suggestion before you responded after testing it.

This does work though:
Sub writeToNonActiveSheet()
arrMIDIEvents = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) 'Sample array to test with
Range(Sheets(2).Cells(5, 4), Sheets(2).Cells(36, 13)).Value = arrMIDIEvents
End Sub

Open in new window


I'd probably use an object variable to refer to the destination worksheet
Sub writeToNonActiveSheet2()
Dim destSheet As Worksheet
Set destSheet = Sheets(2)
arrMIDIEvents = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) 'Array to test with
destSheet.Range(destSheet.Cells(5, 4), destSheet.Cells(36, 13)).Value = arrMIDIEvents
End Sub

Open in new window

0
 
LVL 5

Assisted Solution

by:Hakan Yılmaz
Hakan Yılmaz earned 400 total points
ID: 40484270
You may use With keyword to shorten your references.
With ThisWorkbook.Worksheets("yoursheetname")
    .Range(.Cells(5, 4), .Cells(36, 13)).Value = arrMIDIEvents
End With

Open in new window


And keep in mind, ThisWorkbook.Sheets is not the same as ThisWorkbook.Worksheets. Because some sheets doesn't have Range and Cells properties.
0
 

Author Comment

by:hindersaliva
ID: 40484448
Perfect! Thank you all.
What I missed was, the .Cells(r, c) needs to be fully addressed with the Sheet name. I put it in the wrong place.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The advancement in technology has been a great source of betterment and empowerment for the human race, Nevertheless, this is not to say that technology doesn’t have any problems. We are bombarded with constant distractions, whether as an overload o…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

596 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question