0
votes

I am struggling with this code and I don't understand why it's causing an issue.

ActiveSheet.Range("B1:E" & lastrow).Copy
        ActiveSheet.Range("B1").Select
        Selection.PasteSpecial xlPasteValues
        ActiveSheet.Name = startday & "_" & startmonth & "_" & startyear

This keeps generating an error on line three.

Originally I simply had "Range("B1").pastespecial" etc, but this was pasting into another open workbook, even though it was not the active sheet.

I have tried about 70 or 80 different possible alternative approaches, and I cannot get it to do something which I regard as fairly simple - namely copy this range and paste them as values in the same location, in the same sheet.

3
I'm aware of the importance of avoiding select, yes. I am doing things which are not proper because I had already tried all the obviously "correct" things.Statsanalyst
But the key is you should also be avoiding reliance on ActiveSheet :) It's always better to explicitly declare and assign worksheet variables, rather than rely on the user to keep the appropriate sheet active throughout runtime.David Zemens
show your lastrow definitionuser3598756

3 Answers

1
votes

ActiveSheet can cause problems, have a read about how to avoid using Select and Active... elements like ActiveSheet.

Essentially, just make sure you fully qualify each reference to a Worksheet by also specifying which Workbook the worksheet is in.

ThisWorkbook.Worksheets("SomeWorksheet").Range("B1:E" & lastrow).Copy
ThisWorkbook.Worksheets("AnotherWorksheet").Range("B1").PasteSpecial xlPasteValues
ThisWorkbook.Worksheets("YetAnotherWorksheet").Name = startday & "_" & startmonth & "_" & startyear

If all your actions are related to the same worksheet, you can simplify things by using the With token:

With ThisWorkbook.Worksheets("SomeWorksheet")    
    .Range("B1:E" & lastrow).Copy
    .Range("B1").PasteSpecial xlPasteValues
    .Name = startday & "_" & startmonth & "_" & startyear
End With
1
votes

If you're trying to paste values in to the same sheet, then you need only do:

With ActiveSheet.Range("B1:E" & lastrow)
    .Value = .Value
End With
ActiveSheet.Name = startday & "_" & startmonth & "_" & startyear

Preferably, I would define which sheet, just so there's no ambiguity:

With Workbooks("name of workbook").Worksheets("name of sheet").Range("B1:E" & lastRow)
    .Value = .Value
End With

This takes the .Value property of the range/array and writes it back to the sheet. There is no need to copy or paste anything, and the result will be that only the values remain (i.e., all formula references will be wiped out).

0
votes

Thanks all for the excellent replies.

I identified the cause, although I don't fully understand the underlying reason for the problem.

I was taking the approach of copying a worksheet from one workbook to a new workbook, and then copying and pasting the target range (including formulae) on this new worksheet as values.

When this didn't work, I tried the method suggested by @David Zemens, but this didn't work either.

It seems to me as though the issue was that the formulae in the new sheet were referencing cells in the original worksheet, and that for some reason (said reason being the bit I don't currently understand), this meant that VBA did not like it when I tried to convert them to values and/or copy and paste as values.

Once I adopted the alternative approach of getting VBA to create a new workbook, and then pasting the content of the original sheet in that new book as values, the problem was solved.