Solved

Access VBA - Convert to Number

Posted on 2016-09-29
6
63 Views
Last Modified: 2016-10-05
Thank you for looking at my question,

I have a button on an Access form that fires a macro to export a query to an Excel worksheet. I added the code below to go through the worksheet and convert the numbers stored as text to numbers. The first time I ran the thing it worked as I expected but, for no reason I can see, doesn't work now.

Can anybody suggest a foolproof, repeatable way of achieving the result.
    Dim objXLPurchase As Object
    Dim objBKPurchase As Object
    Dim objSHPurchase As Object
    
    ''Purchase Detail
    Set objXLPurchase = CreateObject("Excel.Application")
    Set objBKPurchase = objXLPurchase.Workbooks.Open("\\Production\Costings\Purchase_Detail.xlsx")
    Set objSHPurchase = objBKPurchase.Worksheets.Item("qryPurchase_Detail_Export")
    
    lngRow = 1
    lngNumRows = 0
    With objSHPurchase
        Do While .Cells(lngRow, 1) <> ""
            lngNumRows = lngNumRows + 1
            lngRow = lngRow + 1
        Loop
    End With
    
    strRange = "A1:P" & lngNumRows
    
    With objSHPurchase.Range(strRange)
        If IsNumeric(.Value) = True Then
            .Value = .Value * 1
        End If
    End With
    
    objXLPurchase.DisplayAlerts = False
    objBKPurchase.Save
    objXLPurchase.Quit
        
    Set objSHPurchase = Nothing
    Set objBKPurchase = Nothing
    Set objXLPurchase = Nothing

Open in new window


I have also tried using
    With objSHPurchase.Range(strRange)
        If IsNumeric(.Value) = True Then
            .NumberFormat="General"
            .Value = .Value * 1
        End If
    End With

Open in new window

0
Comment
Question by:Crxfrd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 17

Expert Comment

by:Barry Cunney
ID: 41821319
Hi Crxfrd,
Please try the following:

 With objSHPurchase.Range(strRange)
        If IsNumeric(.Value) = True Then
            .NumberFormat="General"
            .Value = Val(.Value * 1)
        End If
    End With

Open in new window

0
 

Author Comment

by:Crxfrd
ID: 41821492
>Barry Cunney,

Hi Barry, sorry to say that didn't change anything - still got the little green flag in the top left corner of the cells concerned and the error message when you click into one of those cells.
0
 
LVL 17

Expert Comment

by:Barry Cunney
ID: 41821511
Hi Crxfrd,
Please try the following:

With objSHPurchase.Range(strRange)
        If IsNumeric(.Value) = True Then
            .NumberFormat="General"
            .Value = CInt(Val(.Value * 1))
        End If
    End With

Open in new window

0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:Crxfrd
ID: 41823077
Hi Barry,

Made the change and still no result.
0
 
LVL 17

Accepted Solution

by:
Barry Cunney earned 500 total points
ID: 41823206
Hi Crxfrd
Please try the following:

With objSHPurchase.Range(strRange)
        If IsNumeric(.Value) = True Then
            .NumberFormat="Number"
            .Value = CInt(Val(.Value * 1))
        End If
    End With

Open in new window

0
 

Author Closing Comment

by:Crxfrd
ID: 41829402
Barry, Thank you for your help
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question