Application Defined or Object-defined error

Pauljj

Well-known Member
Joined
Mar 28, 2004
Messages
2,046
I get this error message when my macro reaches this part of the code

With WSO.Range("k1:k5000")

Set i = Range("k1:k5000").Value(366).Count

j = i.Row


End With


I need 'I' to equal the number of times 366 appears as a value between K1 and K5000

Any help would be much appreciated
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
I now get a different message that says Object variable or With Block variable not set ?
 
Upvote 0
Try:

Code:
Sub Foo()
Dim Rng As Range
Dim i As Integer
Set Rng = WSO.Range("k1:k15")
i = WorksheetFunction.CountIf(Rng, 366)
MsgBox "I is equal to " & i
End Sub
 
Upvote 0
This is the complete code

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 10/03/2011 by paulhai
'

'
Dim WBO As Workbook 'original workbook
Dim WBN As Workbook 'new workbook
Dim WSO As Worksheet 'original worksheet
Dim WSN As Worksheet 'new worksheet
Dim R As Range
Dim i As Integer
Dim H As Long
Dim j As Long


Application.DisplayAlerts = False

Columns("F:F").Select
Selection.Delete Shift:=xlToLeft
Columns("H:I").Select
Range("I1").Activate
Selection.Delete Shift:=xlToLeft
Columns("I:I").Select
Selection.Delete Shift:=xlToLeft
Columns("J:L").Select
Selection.Delete Shift:=xlToLeft
Columns("L:Q").Select
Selection.Delete Shift:=xlToLeft
Range("A2").Select
Range("A1:K5000").Sort Key1:=Range("K2"), Order1:=xlAscending, Key2:=Range _
("F2"), Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase _
:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, _
DataOption2:=xlSortNormal
Selection.Subtotal GroupBy:=11, Function:=xlCount, TotalList:=Array(11), _
Replace:=True, PageBreaks:=False, SummaryBelowData:=False

Columns("C:C").EntireColumn.AutoFit
Columns("J:J").EntireColumn.AutoFit



Set WBO = ActiveWorkbook

Set WSO = ActiveSheet

With WSO.Range("J1:J5000")

Set R = .Find("333 Count", LookIn:=xlValues)
H = R.Row


End With

With WSO.Range("k1:k5000")

Set i = .Find(333, LookIn:=xlValues)
j = i.Row


End With

Set WBN = Workbooks.Add(template:=xlWBATWorksheet)
Set WSN = WBN.Worksheets(1)

WSO.Range("a1:k1").Copy Destination:=WSN.Cells(1, 1)



WSO.Range(WSO.Cells(H, 1), WSO.Cells(j, 11)).Copy Destination:=WSN.Cells(2, 1)


ans = InputBox("What week number are you saving this to", "Hinterland")

fn = "Week " & ans & ".xls"
fp = "\\TFCLUSTER-VOL1\VOL1\DEPART\stats\HINTERLAND\Hinterland\2011-12\" & fn

WBN.SaveAs Filename:=fp
WBN.Close savechanges:=False


With WSO.Range("J1:J5000")

Set R = .Find("366 Count", LookIn:=xlValues)
H = R.Row


End With

'With WSO.Range("k1:k5000")

i = WorksheetFunction.CountIf(Range("K1:K5000"), 366) + H

j = i.Row


'End With

Set WBN = Workbooks.Add(template:=xlWBATWorksheet)
Set WSN = WBN.Worksheets(1)

WSO.Range("a1:k1").Copy Destination:=WSN.Cells(1, 1)



WSO.Range(WSO.Cells(H, 1), WSO.Cells(j, 11)).Copy Destination:=WSN.Cells(2, 1)



fn = "Week " & ans & ".xls"
fp = "\\TFCLUSTER-VOL1\VOL1\DEPART\stats\HINTERLAND\Bloomfield\2011-12\" & fn

WBN.SaveAs Filename:=fp
WBN.Close savechanges:=False


Application.DisplayAlerts = True


End Sub
 
Upvote 0
Sorry, I changed your range to K1:K15 -- Below shoud work for you

Rich (BB code):
Sub Foo()
Dim Rng As Range
Dim i As Integer
Set Rng = WSO.Range("k1:k5000")
i = WorksheetFunction.CountIf(Rng, 366)
MsgBox "I is equal to " & i
End Sub
 
Upvote 0
i is a number - it doesn't have a Row property. You don't seem to be doing anything with j anyway.
 
Upvote 0
Perfect, thank you very much to you both for replying and helping me out Much appreciated
 
Upvote 0

Forum statistics

Threads
1,213,535
Messages
6,114,194
Members
448,554
Latest member
Gleisner2

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