Copying a number and pasting a truncated one?

Koontz

Board Regular
Joined
May 30, 2014
Messages
74
Does anyone know the VBA code for how to copy a number that goes out to 18 decimals from one spreadsheet and paste that number with

For instance, I have a number 52.945849949543435656 that I would my macro to paste into a new sheet as simply, 53.

My current code looks like this:

Cells.Select
Selection.Copy
Workbooks.Add
ActiveSheet.Paste

Any help would be much appreciated!

Thanks,
Ryan
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
First, changing the format simply changes the appearance of a numeric value. It does not change the actual value. It is unclear whether that would meet Ryan's needs. (Ryan, what say you?)
JoeU2004, the *appearance* is currently fine, I need to make sure that when you click on the cell, there are no places after the decimal.

I don't think we are using the terminology in the same way. I'll try to be less subtle. Please answer all of the following questions, highlighted in red.

Suppose A1 initially displays the 20-digit "number" 52.945849949543435656. After we copy A1 to a new worksheet, if we enter the formula =A1*100 into B1 in the new worksheet, which result do you want to see in B1: 5300 or 5295, assuming B1 is formatted as Number with 0 decimal places?

If you want to see 5300 in B1. you want the actual value in A1 to be 53. Of course, that is also how it will appear.

On the other hand, if you want to see 5295 in B1, you want the actual value in A1 to be the original 20-digit number (to the extent possible), only changing its appearance to be 53. That is, formatting it as Number with 0 decimal places.

The difference is: "also appearing" v. "only appearing".

-----

In any case, it is not necessary to explicitly do Workbooks.Add and ActiveSheet.Paste.

And it is not sufficient to change the numeric format of the copied cells.

Instead of:
Rich (BB code):
Cells.Select
Selection.Copy
Workbooks.Add
ActiveSheet.Paste
it is sufficient and probably a lot more efficient to write:
Rich (BB code):
ActiveSheet.Copy
That statement copies ActiveSheet, creates a new workbook, and effectively pastes into Sheet1 of the new workbook.

-----

But if A1 displays a 20-digit "number" like 52.945849949543435656, the "number" must be text, not truly numeric.

Please confirm: does =ISTEXT(A1) return TRUE?

Any numeric format -- for example, Number with 0 decimal places, which is the same range.NumberFormat="0" in VBA -- has no effect on text in the cell, even if it looks numeric.

So after doing ActiveSheet.Copy, we must physically change some cells.

But I confess: I no longer trust my understanding of exactly what cells you want to change.

Do all the cells in the copied worksheet contain "numeric" text like your 20-digit example?

Do any cells contain other data that you do not want to change; perhaps other text, or real numeric values, or formulas?

If they contain formulas that return real numeric values or "numeric" text, do you want to replace those formulas with their current value?


It would be ideal if you would upload an example Excel file to a file-sharing website and post the "shared" URL in a response to this thread.

The worksheet in the example Excel file should be representative of the variety of data that you expect in the real worksheet to be copied.

-----

Does one of the following code fragments (which?) do what you want? If not, what does it do differently, and how should it behave?

If you want to change the actual value as well as appearance:
Rich (BB code):
    Dim v As Variant, nr As Long, nc As Long
    Dim rng As Range, i As Long, j As Long

    ActiveSheet.Copy    ' into Sheet1 of new workbook
    Set rng = ActiveSheet.UsedRange
    If rng.Count = 1 Then
        ReDim v(1 To 1, 1 To 1) As Variant
        v(1, 1) = rng
    Else
        v = rng
    End If
    nr = UBound(v, 1)    ' number of used rows
    nc = UBound(v, 2)    ' number of used columns
    For i = 1 To nr: For j = 1 To nc
        If v(i, j) <> "" Then
            If IsNumeric(v(i, j)) Then
                With rng(i, j)
                    .NumberFormat = "0"
                    .Value = WorksheetFunction.Round(v(i, j), 0)
                End With
            End If
        End If
    Next j, i
If you want to change only the appearance, which requires that you also change "numeric" text to real numeric values:
Rich (BB code):
    Dim v As Variant, nr As Long, nc As Long
    Dim rng As Range, i As Long, j As Long

    ActiveSheet.Copy    ' into Sheet1 of new workbook
    Set rng = ActiveSheet.UsedRange
    If rng.Count = 1 Then
        ReDim v(1 To 1, 1 To 1) As Variant
        v(1, 1) = rng
    Else
        v = rng
    End If
    nr = UBound(v, 1)    ' number of used rows
    nc = UBound(v, 2)    ' number of used columns
    For i = 1 To nr: For j = 1 To nc
        If v(i, j) <> "" Then
            If IsNumeric(v(i, j)) Then
                With rng(i, j)
                    .NumberFormat = "0"
                    .Value = CDbl(v(i, j))
                End With
            End If
        End If
    Next j, i

Is any this helpful?
 
Last edited:
Upvote 0
I use WorksheetFunction.Round because it rounds like Excel. VBA Round behaves differently if the value is exactly xxxx.5 and xxxx is even (divisible by 2).
The rounding method you have described for VB is called Banker's Rounding and VB uses for all rounding that it does in all function, statements, operators, etc. EXCEPT for the Format function... that one function uses what we all know as "normal rounding" (which is what Excel's ROUND function uses). So, you can use this native VB code to do the rounding as well...

NormallyRoundedValue = Format(YourNumber, "0")

A side benefit of using the Format function is its first argument can numeric or text... no conversions are necessary.
 
Last edited:
Upvote 0
A side benefit of using the Format function is its first argument can numeric or text... no conversions are necessary.

FYI, that is true for WorksheetFunction.Round, as well as Excel ROUND.

Aside.... A benefit of using either WorksheetFunction.Round or Format is that VBA converts up to 17 significant digits. In contrast to Excel, which truncates after 15 significant digits.

For example, WorksheetFunction.Round("123456789012345.56",0) results in 123456789012346, whereas Excel ROUND("123456789012345.56",0) results in 123456789012345.

[EDIT] Clarification.... I mean VBA processes the first parameter up to 17 significant digits to convert into an internal 64-bit binary floating-point representation. VBA still limits WorksheetFunction.Round and Format results to up to 15 significant digits. :(
 
Last edited:
Upvote 0
Aside.... A benefit of using either WorksheetFunction.Round or Format is that VBA converts up to 17 significant digits.
I am pretty sure the Format function uses the Decimal data subtype (of the Variant data type) for its argument and, as such, it will properly round a number containing up to 28 digits (excluding the decimal point, if there is one)...

MsgBox Format("123456789012345678901234567.5", "0")
 
Upvote 0
Hi Joe, thank you for the responses, and I'm sorry for the delay, I was out of town. To answer your question:

Please confirm: does =ISTEXT(A1) return TRUE?
No, it returns FALSE.

Do all the cells in the copied worksheet contain "numeric" text like your 20-digit example? Yes.

Do any cells contain other data that you do not want to change; perhaps other text, or real numeric values, or formulas? Yes. I only need to change data in columns D, F, I, K, N, P, S, U. I need to leave all other columns as they are.

-Ryan
 
Upvote 0
Rick, is that a "cosmetic" rounding? When I click on the cell, I need the number to be actually rounded, not simply in appearance.

-Ryan
 
Upvote 0
Rick, is that a "cosmetic" rounding? When I click on the cell, I need the number to be actually rounded, not simply in appearance.
No, the value produced by the VB Format function is the rounded value... the original value does not get included in anyway. VB's Format function is sort of the equivalent of Excel's TEXT function... it does not do anything like what cell formatting does (if that is where your confusion is)... that is just VB's name for a function.
 
Upvote 0
So if I wanted to utilize the 'NormallyRoundedValue = Format(YourNumber, "0")' formula, I'm curious what it would look like? If I am understanding correctly, it would look like the below:



Dim GCell As Range
Dim ActCell As Range
Dim Page$, Txt$, MyPath$, MyWB$, MySheet$
Dim MyPath1
Dim MyWB1
MyPath1 = "T:\0186\2a7\WAL reports\2039067\"
MyWB1 = 2039067 & "_" & Format(IIf(Weekday(Date - 1) = 1, Date - 3, Date - 1), "MMDDYY") & ".xlsx"
Txt = "N.A.V."
MyPath = "T:\0186\2a7\R228 Reports\2039067\"
MyWB = Format(IIf(Weekday(Date - 1) = 1, Date - 3, Date - 1), "MMDDYY") & ".xlsx"
MySheet = ActiveSheet.Name
Application.ScreenUpdating = False
Set ActCell = ActiveCell
Workbooks.Open Filename:=MyPath & MyWB
Set GCell = ActiveSheet.Cells.Find(Txt).Offset(0, 2)
NormallyRoundedValue = Format(GCell, "0")
ActCell.Value = GCell.Value
ActiveWorkbook.Close savechanges:=False
Application.ScreenUpdating = True
Selection.Offset(0, 4).Select
 
Upvote 0
So if I wanted to utilize the 'NormallyRoundedValue = Format(YourNumber, "0")' formula, I'm curious what it would look like? If I am understanding correctly, it would look like the below:



Dim GCell As Range
Dim ActCell As Range
Dim Page$, Txt$, MyPath$, MyWB$, MySheet$
Dim MyPath1
Dim MyWB1
MyPath1 = "T:\0186\2a7\WAL reports\2039067\"
MyWB1 = 2039067 & "_" & Format(IIf(Weekday(Date - 1) = 1, Date - 3, Date - 1), "MMDDYY") & ".xlsx"
Txt = "N.A.V."
MyPath = "T:\0186\2a7\R228 Reports\2039067\"
MyWB = Format(IIf(Weekday(Date - 1) = 1, Date - 3, Date - 1), "MMDDYY") & ".xlsx"
MySheet = ActiveSheet.Name
Application.ScreenUpdating = False
Set ActCell = ActiveCell
Workbooks.Open Filename:=MyPath & MyWB
Set GCell = ActiveSheet.Cells.Find(Txt).Offset(0, 2)
NormallyRoundedValue = Format(GCell, "0")
ActCell.Value = GCell.Value

ActiveWorkbook.Close savechanges:=False
Application.ScreenUpdating = True
Selection.Offset(0, 4).Select
Is the idea of the part of your code I highlighted in red to take the value in GCell and round it within GCell? If so, then that part of your code should look like this...

Code:
Set GCell = ActiveSheet.Cells.Find(Txt).Offset(0, 2)
NormallyRoundedValue = Format(GCell, "0")
GCell.Value = NormallyRoundedValue
 
Upvote 0

Forum statistics

Threads
1,215,133
Messages
6,123,232
Members
449,092
Latest member
SCleaveland

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top