ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   converting a string to long (https://www.excelbanter.com/excel-programming/315020-converting-string-long.html)

Ciar?n

converting a string to long
 
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

Tom Ogilvy

converting a string to long
 
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).

--
Regards,
Tom Ogilvy

"Ciar?n" wrote in message
om...
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





All times are GMT +1. The time now is 04:42 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com