Solved

Access VBA - Convert to Number

Posted on 2016-09-29
6
58 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

738 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