V Nadeau
asked on
Trouble removing right side characters from Excel cell using vb.net
Hi. I am trying to remove characters from the right side of a cell in excel using vb.net so that I will be left with a date value. Here is the cell value:
REPORT DATE:07-Sep-18
Here is the code:
The error message is "Integer values cannot be converted to Char"
Thank you in advance for any help with this.
Valerie
REPORT DATE:07-Sep-18
Here is the code:
'set up initial column
strColumn = "J"
'set up initial row
intRowID = 2
'Set up range
strRange = strColumn & intRowID
strStringDate = ForcastWorkSheet.Cells.Range(strRange).Value
strDate = strStringDate.TrimStart(strStringDate.Length, -12)
The error message is "Integer values cannot be converted to Char"
Thank you in advance for any help with this.
Valerie
What's actually in that cell?
ASKER
The cell contains:
REPORT DATE:07-Sep-18
REPORT DATE:07-Sep-18
Try this, not sure if it will work in VB.net
strStringDate = Application.WorksheetFunction.Right(ForcastWorkSheet.Cells.Range(strRange).Value, 9)
Actually, this might be better
This is not a valid Range reference
'set up initial column
strColumn = "J"
'set up initial row
intRowID = 2
'Set up range
strRange = strColumn & intRowID
strStringDate = Application.WorksheetFunction.Right(ForcastWorkSheet.Range(strRange).Value, 9)
This is not a valid Range reference
strStringDate = ForcastWorkSheet.Cells.Range(strRange).Value
ASKER
Sorry this does not work. Got error on Application.WorksheetFunct ion. No Worksheet method of application.
Have you opened Excel, I don't use VB.net but I think something like this
Dim appXL As Excel.Application
Dim wbXl As Excel.Workbook
Dim shXL As Excel.Worksheet
Dim raXL As Excel.Range
' Start Excel and get Application object.
appXL = CreateObject("Excel.Application")
appXL.Visible = True
ASKER
Yes, excel is opened successfully:
I have no issues using the worksheet, it will add values to other cells with no problem.
Dim ForcastApp = New Microsoft.Office.Interop.Excel.Application
Dim ForcastWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim ForcastWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
.
.
.
ForcastWorkBook = ForcastApp.Workbooks.Open(strFileName)
'Name worksheet to use in workbook
ForcastWorkSheet = ForcastWorkBook.Worksheets("Michelin_PA___Project_Cost_Sum_")
ForcastWorkBook = ForcastApp.Workbooks.Open(strFileName, Editable:=True, ReadOnly:=False)
I have no issues using the worksheet, it will add values to other cells with no problem.
Hi,
I suspect the culprit is the following line:
Declare variables at initialize them right before using them for the 1st time (AKA narrow declaration)
Give up hungarian notation, it provide nothing usefull and you have a powerfull IDE giving you variable's type. And with narrow declaration, hungarian notation make little to no sens.
Are you using appropriate data type (unsigned for values that can'be negative) ?
Option Explicit On ?
Option Strict On ?
I suspect the culprit is the following line:
strRange = strColumn & intRowIDSo you need an explicit cast:
strRange = strColumn & str(intRowID)
And since you're in .Net environment:Declare variables at initialize them right before using them for the 1st time (AKA narrow declaration)
Give up hungarian notation, it provide nothing usefull and you have a powerfull IDE giving you variable's type. And with narrow declaration, hungarian notation make little to no sens.
Are you using appropriate data type (unsigned for values that can'be negative) ?
Option Explicit On ?
Option Strict On ?
ASKER
Sorry, but that did not work. Concatenating the strColumn and intRowID produces N44, changing it to str(intRowID) produces N14 which causes errors with all lines trying to change font.
Err,
Concatenating "J" and "2" produce "N44" or "N14" ??????
That does not make any sens, unless there is something you did not told us.
Toggle break points, and check your variable's values with the debugger.
Concatenating "J" and "2" produce "N44" or "N14" ??????
That does not make any sens, unless there is something you did not told us.
Toggle break points, and check your variable's values with the debugger.
ASKER
Sorry, yeah that made no sense at all!
Concatenating "J" (a string) with str(intRowID) of '2' gives me J 2 ... if I join the string column of "J" with the int row of '2' then I get the proper J2 with no space in between. Therefore, using str(intRowId) will not work and causes other errors.
Hope this makes more sense ...
Concatenating "J" (a string) with str(intRowID) of '2' gives me J 2 ... if I join the string column of "J" with the int row of '2' then I get the proper J2 with no space in between. Therefore, using str(intRowId) will not work and causes other errors.
Hope this makes more sense ...
So the issue you posted seems solved.
As for "other errors with all lines trying to change font", afraid you didn't post any code related to that.
As for "other errors with all lines trying to change font", afraid you didn't post any code related to that.
ASKER
I got confused between two different posts, I apologize for the confusion.
My original post and remaining problem was this:
Hi. I am trying to remove characters from the right side of a cell in excel using vb.net so that I will be left with a date value. Here is the cell value:
REPORT DATE:07-Sep-18
The code I have is this:
'set up initial column
strColumn = "J"
'set up initial row
intRowID = 2
'Set up range
strRange = strColumn & intRowID
strStringDate = ForcastWorkSheet.Cells.Ran ge(strRang e).Value
strDate = strStringDate.TrimStart(st rStringDat e.Length, -12)
The problem is with the line where I am trying to strip the first 12 characters from the string. The string contains a date that i would like to perform calculations on.
My original post and remaining problem was this:
Hi. I am trying to remove characters from the right side of a cell in excel using vb.net so that I will be left with a date value. Here is the cell value:
REPORT DATE:07-Sep-18
The code I have is this:
'set up initial column
strColumn = "J"
'set up initial row
intRowID = 2
'Set up range
strRange = strColumn & intRowID
strStringDate = ForcastWorkSheet.Cells.Ran
strDate = strStringDate.TrimStart(st
The problem is with the line where I am trying to strip the first 12 characters from the string. The string contains a date that i would like to perform calculations on.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!