View Full Version : Get Cell Value in Excel VBA


Keith
02-08-2009, 02:49 PM
I wish to check the value of a cell when a form is opened. I have spent a long time trying to find a solution.
The procedure that I have is,

Private Sub UserForm_Activate()
Dim x As Integer
x = Worksheets("Data").Range("P1:P1").Value
Debug.Print x

End Sub


I get the error ' Line is not an executable statement'. any suggestions please

boblarson
02-08-2009, 03:10 PM
Try changing this:

x = Worksheets("Data").Range("P1:P1").Value


to this:

x = Worksheets("Data").Range("P1").Value

Keith
02-09-2009, 08:04 AM
Thanks for the reply Bob, I've tried your suggestion but get the same error.
What I am trying to do is allow the user to add to a list of names but the list cannot contain more than 20 names. When the User Form is opened I want to check that list contains less than 20 names, if so allow the user to add a name. The cell "P1" contains a count of the names in the list.

boblarson
02-09-2009, 08:06 AM
Can you post your spreadsheet so we can check it out?

Keith
02-09-2009, 09:32 AM
Can you post your spreadsheet so we can check it out?

Thanks Bob I've Solved it.

Private Sub UserForm_Activate()
Dim NewRow As Integer

NewRow = Worksheets("Data").Range("$P$1").Value

If NewRow = 20 Then
Call MsgBox("The maximum number has been reached", vbExclamation, Application.Name)
frmNewName.Hide
End If

End Sub