Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Access VBA - Convert to Number

Posted on 2016-09-29
6
Medium Priority
?
67 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

Author Comment

by:Crxfrd
ID: 41823077
Hi Barry,

Made the change and still no result.
0
 
LVL 17

Accepted Solution

by:
Barry Cunney earned 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
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…

722 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