Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 213
Default Why does this macro fail (1004)

When I open the workbook the macro runs fine, the next time I run the macro
it fails with "'Runtime error 1004. Paste method of worksheet class failed."
I have to manually delete the data from sheet "Print Data", save the excel
file and then I can run the macro.
I should be able to run this repeatedly. Open Userform and run, open
userform and run, etc...

'======
Private Sub CommandButton1_Click()
If UserPart.Value = "" Then
MsgBox "You must enter a Value in " & """Part Number""" & " text box!"
End If
If IsDate(UserDate.Value) = False Then
MsgBox "You must enter a valid date in " & """Job Due By""" & " text box in
a Date format (mm/dd/yy)!"
End If
With Sheets("Part Number")
Sh1LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
Set Sh1Range = .Range("A2:A" & Sh1LastRow)
End With
'MsgBox Sh1LastRow 'for testing
For Each Sh1Cell In Sh1Range
If Sh1Cell.Value = sUserPart Then
x1 = Replace(Sh1Cell.Address, "$", "")
x2 = Replace(x1, "A", "")
sRowData = x1 & ":H" & x2
'MsgBox sRowData 'for testing
Range(sRowData).Select
Selection.Copy
Sheets("Print Data").Select
ActiveSheet.Unprotect "2000"
Range("A2").Select
ActiveSheet.Paste 'Paste method of worksheet class failed (1004) on
second pass?
Columns("A:H").Select
Selection.Columns.AutoFit
Range("A2").Select
ActiveSheet.Protect "2000"
End If
Next Sh1Cell
End Sub
'======
--
Regards

VBA.Noob.Confused
XP Pro
Office 2007

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 213
Default Why does this macro fail (1004)

I think I know why, but I do not know why it happens.
When I rerun th emacro I think the sheet "unprotect" is failing.
--
Regards

VBA.Noob.Confused
XP Pro
Office 2007



"Rick S." wrote:

When I open the workbook the macro runs fine, the next time I run the macro
it fails with "'Runtime error 1004. Paste method of worksheet class failed."
I have to manually delete the data from sheet "Print Data", save the excel
file and then I can run the macro.
I should be able to run this repeatedly. Open Userform and run, open
userform and run, etc...

'======
Private Sub CommandButton1_Click()
If UserPart.Value = "" Then
MsgBox "You must enter a Value in " & """Part Number""" & " text box!"
End If
If IsDate(UserDate.Value) = False Then
MsgBox "You must enter a valid date in " & """Job Due By""" & " text box in
a Date format (mm/dd/yy)!"
End If
With Sheets("Part Number")
Sh1LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
Set Sh1Range = .Range("A2:A" & Sh1LastRow)
End With
'MsgBox Sh1LastRow 'for testing
For Each Sh1Cell In Sh1Range
If Sh1Cell.Value = sUserPart Then
x1 = Replace(Sh1Cell.Address, "$", "")
x2 = Replace(x1, "A", "")
sRowData = x1 & ":H" & x2
'MsgBox sRowData 'for testing
Range(sRowData).Select
Selection.Copy
Sheets("Print Data").Select
ActiveSheet.Unprotect "2000"
Range("A2").Select
ActiveSheet.Paste 'Paste method of worksheet class failed (1004) on
second pass?
Columns("A:H").Select
Selection.Columns.AutoFit
Range("A2").Select
ActiveSheet.Protect "2000"
End If
Next Sh1Cell
End Sub
'======
--
Regards

VBA.Noob.Confused
XP Pro
Office 2007

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Why does this macro fail (1004)

Try this... Keeping in mind that I can not test it since you are using global
variables and are not using option explict to require variable declarations...

'MsgBox sRowData 'for testing
with Sheets("Print Data")
..Unprotect "2000"
Range(sRowData).Copy Destination:= .Range("A2")
..Columns("A:H").Entirecolumn.AutoFit
..Unprotect "2000"
end with
End If
Next Sh1Cell
End Sub

--
HTH...

Jim Thomlinson


"Rick S." wrote:

I think I know why, but I do not know why it happens.
When I rerun th emacro I think the sheet "unprotect" is failing.
--
Regards

VBA.Noob.Confused
XP Pro
Office 2007



"Rick S." wrote:

When I open the workbook the macro runs fine, the next time I run the macro
it fails with "'Runtime error 1004. Paste method of worksheet class failed."
I have to manually delete the data from sheet "Print Data", save the excel
file and then I can run the macro.
I should be able to run this repeatedly. Open Userform and run, open
userform and run, etc...

'======
Private Sub CommandButton1_Click()
If UserPart.Value = "" Then
MsgBox "You must enter a Value in " & """Part Number""" & " text box!"
End If
If IsDate(UserDate.Value) = False Then
MsgBox "You must enter a valid date in " & """Job Due By""" & " text box in
a Date format (mm/dd/yy)!"
End If
With Sheets("Part Number")
Sh1LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
Set Sh1Range = .Range("A2:A" & Sh1LastRow)
End With
'MsgBox Sh1LastRow 'for testing
For Each Sh1Cell In Sh1Range
If Sh1Cell.Value = sUserPart Then
x1 = Replace(Sh1Cell.Address, "$", "")
x2 = Replace(x1, "A", "")
sRowData = x1 & ":H" & x2
'MsgBox sRowData 'for testing
Range(sRowData).Select
Selection.Copy
Sheets("Print Data").Select
ActiveSheet.Unprotect "2000"
Range("A2").Select
ActiveSheet.Paste 'Paste method of worksheet class failed (1004) on
second pass?
Columns("A:H").Select
Selection.Columns.AutoFit
Range("A2").Select
ActiveSheet.Protect "2000"
End If
Next Sh1Cell
End Sub
'======
--
Regards

VBA.Noob.Confused
XP Pro
Office 2007

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro Error 1004 JB63 Excel Discussion (Misc queries) 3 May 29th 09 03:55 PM
Formula in macro causes macro to fail KCK Excel Programming 2 February 8th 07 08:47 PM
Excel 2000 = upgraded to 2003 and macro to change font color fail Donna Excel Programming 4 October 4th 05 10:25 PM
A macro that copies and pastes autofiltered cells will fail. Mafunzalo Excel Programming 2 July 13th 05 05:43 PM
add-in macro strange errors--method sheets of object workbook fail DavidH[_2_] Excel Programming 3 November 4th 04 05:49 AM


All times are GMT +1. The time now is 01:17 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"