VBA Question

Utterly Bamboozled

New Member
Joined
Jul 7, 2010
Messages
39
Could someone please tell me why this code keeps crashing Excel??

'
Sheets("Timesheet").Select
Sheets("Timesheet").Copy Before:=Sheets(5)
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Timesheet (2)").Select
Sheets("Timesheet (2)").Name = Range("C6").Value
Range("C6:E6").Select
Application.CutCopyMode = False
End Sub

Thanks!
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
What do you have in cell C6?
Your code is trying to rename the worksheet to that value.
Do you have characters that may not be legal for a sheetname?
 
Upvote 0
Try something like this...

Code:
Sub test()

    Sheets("Timesheet").Copy Before:=Sheets(5)
    ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value
    
    On Error Resume Next
        ActiveSheet.Name = Range("C6").Text
    On Error GoTo 0
    
    If ActiveSheet.Name <> Range("C6").Text Then MsgBox "Couldn't rename the sheet as " & Range("C6").Text
    
    ActiveSheet.Range("C6:E6").Select

End Sub
 
Upvote 0
Thanks guys,

Cell C6 is actually a merged cell (C6:E6), but the code was working previously and all of a sudden decided to crash. Strange.

AlphaFrog, I've attempted to use your suggestion as follows:

Sub Export()
'
' Sheets("Timesheet").Copy Before:=Sheets(5)
ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value

On Error Resume Next
ActiveSheet.Name = Range("C6").Text
On Error GoTo 0

If ActiveSheet.Name <> Range("C6").Text Then MsgBox "Couldn't rename the sheet as " & Range("C6").Text

ActiveSheet.Range("C6:E6").Select
End Sub

I get a runtime error 1004 - Application defined or Object defined error. When I debug it highlights ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value

Thanks for your help.
 
Upvote 0
I don't know why you are getting an error. I can't get it to error for me no matter what I try.

What version of Excel do you have?
 
Upvote 0
If your worksheet is Protected, you could get that error. You must Unprotect the worksheet first...

Code:
Sub Export()
'
    Sheets("Timesheet").Copy Before:=Sheets(5)
    
    [COLOR="Red"]ActiveSheet.Unprotect Password:="Secret"[/COLOR]
    ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value
    
    On Error Resume Next
    ActiveSheet.Name = Range("C6").Text
    On Error GoTo 0
    
    If ActiveSheet.Name <> Range("C6").Text Then MsgBox "Couldn't rename the sheet as " & Range("C6").Text
    
    ActiveSheet.Range("C6:E6").Select

End Sub
 
Last edited:
Upvote 0
Is it possible to add a line of code to remove all named ranges in the copied worksheet? The reason is when I go to export this sheet to a different workbook (and several versions of the same copied sheet will be exported) Excel says the name already exists on the destination worksheet. Then I end up having a large number of named ranges in my names manager that are linked to an external workbook.

Thanks for your help.
 
Upvote 0
Code:
Sub DeleteNamedRanges()
Dim i As Integer
    On Error Resume Next
    For i = 1 To Names.Count
        Names(i).Delete
    Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,605
Messages
6,179,860
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top