Application 1004 error

H

headly

This is spooky; This code works on some machines, but on others it throws a
1004 Application Defined Error;

Here's the code, any idea what's broken?

Public Sub ParseProduct()

'Separate products to sheets

'Goto starting cell
Range("b2").Select
'Navigate to bottom, get end cell
Selection.End(xlDown).Select

'Store end row in a variable
Dim varlastrow As Variant
varlastrow = ActiveCell.Row

'Go back to starting cell
Range("b2").Select

'Get the data sheet name
Dim varsheetname As Variant
varsheetname = ActiveSheet.Name

'Loop from row 2 to end row
Dim i As Variant 'i is a counter
For i = 2 To varlastrow

'Select the product in the variable row
Range("b" & i).Select

'Store the value in a variable
Dim varproduct As Variant
varproduct = Range("B" & i).Value

'create a variable to test true
Dim vartrue As Variant
vartrue = False

'Loop through sheets, test if product sheet exists
Dim k As Variant 'k is a counter

For k = 1 To Worksheets.Count
Worksheets(k).Select

If ActiveSheet.Name = varproduct Then
vartrue = True
End If

Next k
'if no product sheet
'add sheet, name the sheet
'and copy the header to the new sheet
If vartrue = False Then
Worksheets.Add
ActiveSheet.Name = varproduct
Worksheets(varsheetname).Select
Rows("1:1").Select
Selection.Copy
Worksheets(varproduct).Select
Rows("1:1").Select
ActiveSheet.Paste
End If

'Go back to data sheet
Worksheets(varsheetname).Select
'Copy the variable product to the product sheet
Rows(i).Select
Selection.Copy
Worksheets(varproduct).Select
Range("a65000").Select
Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste

'Go back to data sheet
Worksheets(varsheetname).Select

Next i
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top