Copy Top Ten Values to another sheet that contain formulas

Pumper

Board Regular
Joined
Sep 12, 2013
Messages
114
Office Version
  1. 365
Hi All,

I have a query that is almost identical to another post here that was solved but I still can't get it to work.


I am trying to copy the Top Ten values in column E Tab "Distribution" (this column contains formulas and is in % format) and corresponding value in column A (Text) to tab "Clients"

My code seems to have two problems
1) Column E having formulas in it seems to be giving "Object Variable or With Block Variable not set" error.
2) If I check and remove formulas and run looks like the number has to be greater than 1 to return it (I am after the top 10 numbers regardless of size).

Any help would be greatly appreciated!

Code:
Sub Top10()

Dim rngValues As Range
Dim rngNames As Range
Dim i As Integer
Dim r As Integer
Dim j As Long
Dim lrow As Long
Dim l As String
Dim MyData As String

MyData = "States" & ".xlsm"

Set rngValues = Workbooks(MyData).Sheets("Distribution").Range("E3:E500")

l = 0
j = 0
r = 3

For i = 1 To 10

j = Application.WorksheetFunction.Large(rngValues, i)
Set rngNames = rngValues.Find(j, , xlFormulas, xlWhole)
Worksheets("Clients").Cells(r, "C") = j
Worksheets("Clients").Cells(r, "B") = rngNames.Offset(, -4)
r = r + 1
Next i

End Sub
 
Try this:
Do you have zero or negative number Range("E3:E500")?
VBA Code:
Sub Top10y()

Dim rngValues As Range
Dim rngNames As Range
Dim i As Integer
Dim r As Integer
Dim j As Double
Dim MyData As String
Dim ary
Dim fm As Long

MyData = "States" & ".xlsm"

Set rngValues = Workbooks(MyData).Sheets("Distribution").Range("E3:E500")
ary = Application.Transpose(rngValues)
r = 3

For i = 1 To 10

    j = Application.WorksheetFunction.Large(ary, 1)
    Worksheets("Clients").Cells(r, "C") = j
        fm = Application.Match(j, ary, 0)
            If IsNumeric(fm) Then
                Worksheets("Clients").Cells(r, "B") = rngValues.Cells(fm, -4)
                r = r + 1
                ary(fm) = Empty
            End If

Next i

End Sub
Yes could could contain positive and negatives (the plan was once i get the "large" function to work I would then use that to also get "Small" to land next to these)
 
Upvote 0

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Yes could could contain positive and negatives (the plan was once i get the "large" function to work I would then use that to also get "Small" to land next to these)
In that case, replace this:
ary(fm) = Empty
with:
ary(fm) = "x"
 
Upvote 0
In that case, replace this:
ary(fm) = Empty
with:
ary(fm) = "x"
yeah i tried (including the above amendment)

Getting Application-defined or object-defined error on this line

Worksheets("Clients").Cells(r, "B") = rngValues.Cells(fm, -4)

Brings back 1 number and no codes
 
Upvote 0
Worksheets("Clients").Cells(r, "B") = rngValues.Cells(fm, -4)
Sorry, it should be:
Worksheets("Clients").Cells(r, "B") = rngValues.Cells(fm, 1).Offset(, -4)
 
Upvote 0
Solution
Sorry, it should be:
Worksheets("Clients").Cells(r, "B") = rngValues.Cells(fm, 1).Offset(, -4)

YES!

Works perfectly!

Really appreciate your time and willingness to go over this multiple times.

Thanks so much (y)
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,224,830
Messages
6,181,228
Members
453,025
Latest member
Hannah_Pham93

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