Run-time error '1004': Cannot run Visual Basic macro because of a syntax error

lemery

New Member
Joined
Jul 31, 2014
Messages
36
I have 280,000 rows with 36 columns. I'm looking to pair down the file. I would like to reduce the file so that there are no more than 10 rows per company (column H). Some companies have 3 rows, some have 5000. I would like the macro to return a sheet with up to, but no more than 10 rows per company along with all of columns associated with their row.

There are 5 sheets on my file. The sheet I'm trying to reduce is titled "All comp", the blank sheet where I want to the results to go titled "new data".
I am trying to use the below macro, but I'm getting the error: Run-time error '1004': Cannot run Visual Basic macro because of a syntax error

It highlights .calculate as the first step in what is wrong with what I've entered.
Code:
Sub TenDuplicates()


    With Application
        .ScreenUpdating = False
        .Calculate
        .Calculation = xlCalculationManual
        .DisplayAlerts = False
        .EnableEvents = False
    End With








    Dim raw As Worksheet
    Dim output As Worksheet
    Dim names() As String
    
    Dim nextRow As Long
    Dim counter As Integer
    
    nextRow = 1
    counter = 0
    
    Set raw = Sheets("zebraUS")
    Set output = Sheets("New data")
    


    With output
        .UsedRange.Clear
        .Cells(1, 1).EntireColumn.Value = raw.Cells(1, 8).EntireColumn.Value
        .Range("H:H").RemoveDuplicates Columns:=1, Header:=xlNo


        For x = 1 To .Cells(Rows.Count, "H").End(xlUp).Row
            If x = 1 Then
                ReDim Preserve names(1 To 1)
            Else
                ReDim Preserve names(1 To x)
            End If
            
            names(x) = .Cells(x, 1)
        Next x
        
        .UsedRange.Clear
        
    End With
    
    With raw
        For x = 1 To UBound(names)
            counter = 0
            For y = 1 To .Cells(Rows.Count, "8").End(xlUp).Row
                If .Cells(y, 8) = names(x) Then
                    output.Cells(nextRow, 1).EntireRow.Value = _
                            .Cells(y, 1).EntireRow.Value
                    counter = counter + 1
                    nextRow = nextRow + 1
                End If
                
                Select Case counter
                    Case 10
                        GoTo nextName
                    Case Else
                End Select
                
            Next y
nextName:
        Next x
    End With




    With Application
        .ScreenUpdating = True
        .Calculation = xlCalculationAutomatic
        .DisplayAlerts = True
        .EnableEvents = True
    End With

End Sub
 
Last edited by a moderator:

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
It worked!! Thank you so much for all of your help!! If I wanted to use this same macro on other files (the columns will always be the same and I can make sure the tab names are the same. Is there anything else I need to edit if or can I just cut and paste this into into all files?
 
Upvote 0
You can cut and paste it into all files. It doesn't have any workbook reference so just changing the sheets is what you need to do.
 
Upvote 0
i noticed that ever company has 10. If a comany has less than 10 contacts, will they also be returned in this file?
 
Upvote 0

Forum statistics

Threads
1,215,700
Messages
6,126,305
Members
449,308
Latest member
VerifiedBleachersAttendee

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