WRITING TO EXCEL FROM VBA

I have a function which writes to EXCEL every other time
It is unstable
There is a range in a Sheet, say Sheet1, called solution

there is a 2D matrix which I need to write to excel, let's call it matrix
The dimensions of "matrix" are n x 2

Worksheets("Sheet1").Activate

dim i as integer
dim n as integer

n =UBound(matrix)
for i = 1 to n
Range("solution").Cells(i,1).ClearContents
Range("solution").Cells(i,1) = matrix(i,1)
Range("solution").Cells(i,2).ClearContents
Range("solution").Cells(i,2) = matrix(i,2)
next

The strange thing is that the contents are cleared but it escapes when the value has to be written.

Please help.

It does work sometimes

Any suggestions?

Nick's picture

have a try with

have a try with this:
http://excelexperts.com/33-vba-tips-output-array-without-looping

also... if things are not behaving consistently, the chances are that either:

1. your sheet is corrupt
=>
http://excelexperts.com/Excel-Tips-Fix-Corrupt-Spreadsheet

2. something else (an event maybe) is happening when you input your data
=> put
application.enablevents = false
at the beginning of the code, and
application.enablevents = true
at the end