Solved

Excel 2013 VBA 'Range on another worksheet' question

Posted on 2014-12-05
4
256 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
  • 2
4 Comments
 

Author Comment

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

Accepted Solution

by:
SimonAdept earned 400 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 100 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
I recently resolved a client's Office 2013 installation problem and wanted to offer an observation that may help you with troubleshooting similar issues. The client ordered three Dell Optiplex system units with the Windows 7 downgrade option inst…
An overview on how to enroll an hourly employee into the employee database and how to give them access into the clock in terminal.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now