0

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.

5
  • Relevant: stackoverflow.com/questions/10714251/… Commented Apr 7, 2017 at 15:39
  • Also relevant: stackoverflow.com/questions/19351832/… Commented Apr 7, 2017 at 15:43
  • 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. Commented Apr 7, 2017 at 15:52
  • 1
    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. Commented Apr 7, 2017 at 15:55
  • show your lastrow definition Commented Apr 7, 2017 at 16:30

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, I tried: with BookZ.sheets("Sheet2") .range(B1:E" & lastrow).copy .Range("B1").pastespecial xlpastevalues This generated an automation error - "the object invoked has disconnected from its clients", etc.
1

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).

4 Comments

This doesn't seem to work. This is causing Excel to crash. Do you have any idea why that might be?
@Statsanalyst what's the value of lastrow?
@@David Zemens 46.
That shouldn't cause any error @Statsanalyst can you provide the full code you're using? Are you sure it's crashing on this part of the code and not somewhere else? Are there any event-handlers in this worksheet which might be triggering an infinite loop or something?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.