VBA Count Function

trough

Board Regular
Joined
Oct 26, 2010
Messages
55
Not new to programming but new to vba. Macro gets hung up on highlighted line SOMETIMES. It will work and then it won't. Really puzzled. Tried some things I found in forums but nothing has been a permanent fix. Solutions?

Sub RotLoad()

' macro to cycle through all loadcases
' and obtain rotated loads at all load
' points for all load cases

Dim num_lpts As Integer
Dim num_lc As Integer
Dim part_num As String
Dim calc_sheet As String
Dim load_sheet As String
Dim rot_sheet As String

' prompt for sheet prefix specific to fitting being analyzed
part_num = InputBox("Enter part prefix:")

' store sheet names used
calc_sheet = part_num & "_Analysis"
load_sheet = part_num & "_Global_Loads"
rot_sheet = part_num & "_Rotated_Loads"

' store number of load points and number of load cases
num_lpts = Sheets(calc_sheet).Cells(79, 4)
num_lc = Application.WorksheetFunction.Count(Sheets(load_sheet).Range(Cells(10, 1), Cells(9999, 1)))

' set load case number then store rotated loads for each load point for each load case
For i = 1 To num_lc
Sheets(calc_sheet).Cells(188, 3) = i
For j = 1 To num_lpts
Sheets(rot_sheet).Cells(10 - 1 + i, 3 * j + 1) = Sheets(calc_sheet).Cells(195 - 1 + j, 12)
Sheets(rot_sheet).Cells(10 - 1 + i, 3 * j + 1) = Sheets(calc_sheet).Cells(195 - 1 + j, 13)
Sheets(rot_sheet).Cells(10 - 1 + i, 3 * j + 1) = Sheets(calc_sheet).Cells(195 - 1 + j, 14)
Next j
Next i

End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Try

num_lc = Application.WorksheetFunction.Count(Sheets(load_sheet).Range("A10:A9999"))
 
Upvote 0
If user enters 888 for the input box, then this line:
load_sheet = part_num & "_Global_Loads"
will wind up being
load_sheet = "888_Global_Loads"

and that load_sheet may not exist, hence your error.
 
Upvote 0
You've already received a couple of possible solutions but you've not shared what the problem is. What does "hung up" mean? Do you get an error message? if so, what?
Not new to programming but new to vba. Macro gets hung up on highlighted line SOMETIMES. It will work and then it won't. Really puzzled. Tried some things I found in forums but nothing has been a permanent fix. Solutions?

Sub RotLoad()

' macro to cycle through all loadcases
' and obtain rotated loads at all load
' points for all load cases

Dim num_lpts As Integer
Dim num_lc As Integer
Dim part_num As String
Dim calc_sheet As String
Dim load_sheet As String
Dim rot_sheet As String

' prompt for sheet prefix specific to fitting being analyzed
part_num = InputBox("Enter part prefix:")

' store sheet names used
calc_sheet = part_num & "_Analysis"
load_sheet = part_num & "_Global_Loads"
rot_sheet = part_num & "_Rotated_Loads"

' store number of load points and number of load cases
num_lpts = Sheets(calc_sheet).Cells(79, 4)
num_lc = Application.WorksheetFunction.Count(Sheets(load_sheet).Range(Cells(10, 1), Cells(9999, 1)))

' set load case number then store rotated loads for each load point for each load case
For i = 1 To num_lc
Sheets(calc_sheet).Cells(188, 3) = i
For j = 1 To num_lpts
Sheets(rot_sheet).Cells(10 - 1 + i, 3 * j + 1) = Sheets(calc_sheet).Cells(195 - 1 + j, 12)
Sheets(rot_sheet).Cells(10 - 1 + i, 3 * j + 1) = Sheets(calc_sheet).Cells(195 - 1 + j, 13)
Sheets(rot_sheet).Cells(10 - 1 + i, 3 * j + 1) = Sheets(calc_sheet).Cells(195 - 1 + j, 14)
Next j
Next i

End Sub
 
Upvote 0
Sorry, I've been in a meeting.

The macro stops and goes into debug mode when it hits the highlighted line but not always. Don't know what triggers it but sometimes it runs perfectly and other times not. Once it stops working it doesn't seem to start working again until I do some editing of the line trying different things then I'll go back to my original code on that line and it will work for awhile.

The sheet does exist. I've checked that the other variables have the correct value.

The A10:A9999 method works but I would prefer to use the row,column reference style.

Error: Run -time error '1004'
Application-defined or object-defined error
num_lc=0 should be 789
 
Upvote 0
OK, the reason it fails with the Cells function is because...

When you use this syntax
Range(Cells(..,..), Cells(..,..))
And you specify the sheet on the RANGE, then you must also specify it on the Cells

Instead of
Sheets("sheetname").Range(Cells(..,..), Cells(..,..))
You need
Sheets("sheetname").Range(Sheets("sheetname").Cells(..,..), Sheets("sheetname").Cells(..,..))

or

With Sheets("Sheetname")
.Range(.Cells(..,..), .Cells(..,..))
End With


The reason it works sometimes, is if the Sheet specified just happens to be the currently active sheet, then the Cells is referring to the same sheet.
But if say Sheet10 is currently active, but range is referring to Sheet1, then it fails because Range is referring to Sheet1, but the Cells is referring to Sheet10.

Hope that helps.
 
Upvote 0
Most excellent. That works except for the "or" part. I had tried that earlier to no avail.

Thanks.
 
Upvote 0
Oh! Got it.

Again, new to vba. Need to get me a vba book.


Thanks for explaining all of it.
 
Upvote 0
I have the issue but inspite of following the suggestions in this thread the problem persits. It does not compile with the folowing error:

Compile Error: Object Required

Your help will be appreciated!
Sub Macro1()


Dim Row As Integer, Col As Integer


For Col = 10 To 280
With Sheets("XYZ")
Set Row = Application.WorksheetFunction.Count(.Range(.Cells(1, Col)), .Range(.Cells(1200, Col)))
End With
Sheets("Summary").Cells(((Col - 2) / 5) + 1, 5) = Sheets("XYZ").Cells(Row - 1, Col).Value

Col = Col + 5
Next Col

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,827
Members
452,946
Latest member
JoseDavid

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