converting a string to long

C

Ciar?n

In the bit of code below
The msgbox near the end returns DividingCell as 2.134
but when I try to convert to a long as DividingNum its value is only 0?
How do I make DividingNum's value equal to 2.134

Regards,
Ciarán

Private Sub CommandButton1_Click()
Dim EndCell As String
Dim EndRow As Long
Dim CurrentCell As String
Dim CurrentRow As Long
Dim DividingCell As String
Dim DividingNum As Long

'Find the co-ords of last Col and row and put them into an integer
ActiveCell.SpecialCells(xlLastCell).Select
EndCell = ActiveCell.Address(, , xlR1C1)
MsgBox (EndCell)

If Mid(EndCell, 3, 1) = "C" Then
EndRow = CLng(Mid(EndCell, 2, 1))

ElseIf Mid(EndCell, 4, 1) = "C" Then
EndRow = CLng(Mid(EndCell, 2, 2))

ElseIf Mid(EndCell, 5, 1) = "C" Then
EndRow = CLng(Mid(EndCell, 2, 3))

ElseIf Mid(EndCell, 6, 1) = "C" Then
EndRow = CLng(Mid(EndCell, 2, 4))

ElseIf Mid(EndCell, 7, 1) = "C" Then
EndRow = CLng(Mid(EndCell, 2, 5))



End If
MsgBox (EndRow)



Range("F4").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
CurrentCell = ActiveCell.Address(, , xlR1C1)
MsgBox (CurrentCell)

If Mid(CurrentCell, 3, 1) = "C" Then
CurrentRow = CLng(Mid(CurrentCell, 2, 1))

ElseIf Mid(CurrentCell, 4, 1) = "C" Then
CurrentRow = CLng(Mid(CurrentCell, 2, 2))

ElseIf Mid(CurrentCell, 5, 1) = "C" Then
CurrentRow = CLng(Mid(CurrentCell, 2, 3))

ElseIf Mid(CurrentCell, 6, 1) = "C" Then
CurrentRow = CLng(Mid(CurrentCell, 2, 4))

ElseIf Mid(CurrentCell, 7, 1) = "C" Then
CurrentRow = CLng(Mid(CurrentCell, 2, 5))



End If
MsgBox (CurrentRow)

Range("c" & CurrentRow).Select

Selection.End(xlUp).Select

DividingCell = ActiveCell



MsgBox (DividingCell)
DividingNum = CLng(DividingCell)
MsgBox (DividingNum)

'MsgBox ("about to do calc")
'
'Range("g" & CurrentRow).Select
'
'
'MsgBox ("calc shud be done now")









End Sub
 
T

Tom Ogilvy

I took appropriate portions of you code:

Sub Tester1()
Dim DividingCell As String
Dim DividingNum As Long
ActiveCell.Value = "'2.134"
DividingCell = ActiveCell
Debug.Print DividingCell, TypeName(DividingCell)
DividingNum = CLng(DividingCell)
Debug.Print DividingNum, TypeName(DividingNum)
End Sub

and ran it and it worked fine:
2.134 String
2 Long

Perhaps you had DividingNum misspelled in one of your statements when you
were testing (is seems OK in your post).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top