Reorganizing data dumb through VBA code (autofilter then copy & paste loop)

Super Emad

New Member
Joined
Jul 12, 2016
Messages
11
Hello everyone,

I have an excel extracted file showing piping material spec code, item code, description, pipe size, unique number and other information that are not required in my report.
the below snapshot is showing the required format for my report, so I wrote it a code to create sheet for each piping spec and discerption (A-11132303 and Carbon Steel... in this case) and move the data to match the below table. However, I keep getting a 1004 Application-defined or object-defined error and I dont understand why. Hope someone can help. here is the code I wrote so far (Its still a work in progress but if it works once I can do it for the other components)
VBA Code:
Sub copyspec()
Dim ws1 As Worksheet
Dim DestWs As Worksheet
Dim WSName As String
Dim NewWs As Worksheet
Dim headers() As Variant
Dim NextRow As Integer
Dim LastRow As Integer
Dim LastRow1 As Integer
On Error GoTo errHandler

Set ws1 = Worksheets("Sheet1")
LastRow = ws1.Cells(Rows.Count, 3).End(xlUp).Row
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1

For i = 10 To LastRow
WSName = ws1.Cells(i, 1)
Set DestWs = Worksheets(WSName)
Set DestWs = ActiveSheet

Cells(1, 1).Value = "Specification"
Cells(1, 2).Value = ws1.Cells(i, 1)
Cells(1, 3).Value = ws1.Cells(i, 3)

Range("A2:H2").Merge
Range("A2:H2").HorizontalAlignment = xlCenter
Cells(2, 1).Value = "Pipes"

headers() = Array("Short Code", "Opt", "From", "To", "UOM", "Commodity Description", _
    "Commodity Code", "Notes")
For x = LBound(headers()) To UBound(headers())
Cells(3, 1 + x).Value = headers(x)
Next x

wsl.Range("A10:X10").AutoFilter Field:=1, Criteria1:=Range("A2")
wsl.Range("A10:X10").AutoFilter Field:=24, Criteria1:=Range("PIPE")
wsl.Range("D10:G1461").SpecialCells(xlCellTypeVisible).Select.Copy

ActiveSheet.Range("next row, 1").Paste


Next
Exit Sub

errHandler:

Worksheets.Add(, Worksheets(Worksheets.Count)).Name = WSName
Set NewWs = ActiveSheet

Resume
End Sub

1643025653233.png
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
have you stepped through the code to see which line is giving you trouble?
 
Upvote 0
have you stepped through the code to see which line is giving you trouble?
Hi, I was running the code as I was writing step by step and the error started showing when I added the following part:

VBA Code:
wsl.Range("A10:X10").AutoFilter Field:=1, Criteria1:=Range("A2")
wsl.Range("A10:X10").AutoFilter Field:=24, Criteria1:=Range("PIPE")
wsl.Range("D10:G1461").SpecialCells(xlCellTypeVisible).Select.Copy

ActiveSheet.Range("next row, 1").Paste
 
Upvote 0
Perhaps try using the .value for your criteria1. For example:
Criteria1:=Range("A2").value
 
Upvote 0
This line might be a problem
ActiveSheet.Range("next row, 1").Paste
Next row should likely be NextRow to match the variable you have created earlier. And I’m thinking it shouldn’t be in quotations as well.
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

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