Thanks Dave, it worked.
I would like to gain some insight about what was wrong and how your code
helped.
First, both procedures--InsertSpectrum and the one containing this code--are
in separate modules in the same project, listed in the project explorer under
"Modules," not under "Microsoft Excel Objects." Would this not make them
"general modules," not "worksheet modules." If so, then the unqualified
range references [i.e., Cells(...)] should have referred to the active sheet;
why didn't they work? i.e., Why was it necessary to explicity specify
ActiveSheet for Cells(...)?
Second, I applied what I thought was the same as your approach in another
line later in the code:
With ActiveSheet
strExpt = .Range(.Cells(i + 8, 1)).Value
End With
but this gave me a "Run-time error '1004': Application-defined or
object-defined error". What's wrong? (strExpt is a string variable. In
this case, i=1; cell A9 indeed contained a string.)
Dave Peterson said:
Is this code in a worksheet module or in a General module?
If it's behind a worksheet, then the activesheet may not be the same as the
worksheet owning the code.
And those unqualified ranges (Cells(12+J,22) and cells(12+J,35) refer to the
worksheet owning that code--not the activesheet. In a general module, those
unqualified range references will refer to the activesheet.
This should work in either case:
with activesheet
.Range(.Cells(12 + j, 22), .Cells(12 + j, 35)).Value _
= .Range("V10:AI10").Value
end with
The dots in front of the .Range and .Cells mean that they belong to the object
in the previous With statement--in this case the activesheet.
Another way so you don't have to do the arithmetic:
with activesheet.range("v10:ai10")
.parent.cells(12+j,22).resize(.rows.count,.columns.count).value = .value
end with
The parent of the range("v10:ai10") is the activesheet. And then you just use
one cell and resize it to match the size of the range from which you're taking
the values.
hmm wrote:
Here is a segment of my code:
InsertSpectrum
ActiveSheet.Range(Cells(12 + j, 22), Cells(12 + j, 35)).Value = _
ActiveSheet.Range("V10:AI10").Value
where InsertSpectrum is another procedure in the same project.
The 2nd line does not execute; instead, I get an error "Run-time error
'1004': Method 'Cells' of object '_Global' failed". If I remove the call to
InsertSpectrum, the next line executes properly.
What can I check to correct the problem, so that the second line will run
after the procedure call?