Link to home
Start Free TrialLog in
Avatar of V Nadeau
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:

        '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)

Open in new window


The error message is "Integer values cannot be converted to Char"

Thank you in advance for any help with this.

Valerie
Avatar of Roy Cox
Roy Cox
Flag of United Kingdom of Great Britain and Northern Ireland image

What's actually in that cell?
Avatar of V Nadeau
V Nadeau

ASKER

The cell contains:
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)

Open in new window

Actually, this might be better

'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)

Open in new window


This is not a valid Range reference

strStringDate = ForcastWorkSheet.Cells.Range(strRange).Value

Open in new window

Sorry this does not work. Got error on Application.WorksheetFunction. 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

Open in new window

Yes, excel is opened successfully:

        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)

Open in new window


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:
strRange = strColumn & intRowID
So you need an explicit cast:
strRange = strColumn & str(intRowID)

Open in new window

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 ?
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.
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 ...
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.
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.Range(strRange).Value
        strDate = strStringDate.TrimStart(strStringDate.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.
ASKER CERTIFIED SOLUTION
Avatar of Ark
Ark
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks!