1

I'm creating a report with multiple columns. What I need is that the columns that show only whole numbers, no decimals, should be rounded to the whole number (so that it not only shows a rounded number, it actually equals a round number). The columns that show the numbers with two numbers after the decimal should not be rounded.

What I can do is:

If c.NumberFormat = "#,##0_);(#,##0)" Then
    c.Formula = "=round(" & Right(strFormula, Len(strFormula) - 1) & ",0)"
End If

However, I have the entire report in an array, and I would like to just paste the whole array into the sheet rather than pasting one cell at a time. I would also rather not process and round each cell based on the cell formatting, rather I would like to copy the formatting of the range where the report will go into an array, and work from the array. I believe this will cut a few seconds off the process.

Is there a way to copy the formatting of a range into an array?

6
  • That sounds like mixing up presentation with representation. Commented Jul 1, 2013 at 3:33
  • Sorry. What do you mean? Commented Jul 1, 2013 at 3:34
  • I agree with Mitch Wheat. He means that the data that you have represented in the array should be separate from the presentation (to the user) of that same data. Commented Jul 1, 2013 at 3:55
  • I still don't get what you're saying. I need the data rounded, because the data gets summed, and I need the sum to actually match what the numbers being summed show. Commented Jul 1, 2013 at 3:59
  • It's a bit confusing all around. Are you writing values from an array to the worksheet and are using the pre-existing formatting in the worksheet to determine which values should be rounded? Or are you modifying existing formulas in certain columns so that they round and are using the formatting to determine which formulas should be modified? Or some of both? Commented Jul 1, 2013 at 4:49

1 Answer 1

4

Is there a way to copy the formatting of a range into an array?

Focusing on the question as posed, yes, that can be done. Unfortunately, it can't done in a one-liner, in the way that a range's values can be assigned to an array with myArray = Range("A2:F25"), for example. (See, also, brettdj's comment.) Instead, you'd need something like:

Dim rng as Range
Dim formatArray() As String
Dim i as Long, j as Long
Set rng = Range(A2:F20)    'or whatever the range is
Redim formatArray(1 to rng.Rows.Count, 1 to rng.Columns.Count)
For i = 1 to rng.Rows.Count
    For j = 1 to rng.Columns.Count
        formatArray(i, j) = rng.Cells(i, j).NumberFormat
    Next
Next
...

A couple of observations, though:

You actually only need to know the formatting for a single row of the range since, presumably, the number formatting in a column will not change mid-column.

That would simplify the code to:

...
Redim formatArray(1 to rng.Columns.Count)
For i = 1 to rng.Columns.Count
    formatArray(i) = rng.Cells(1, i).NumberFormat
Next
...

assuming, for sake of example, that row 1 of the range has the necessary number formats.

I am curious why you would want to modify a formula on the worksheet so that it will round, since you could presumably do the calculation in your code, with rounding, and then write the resulting value back to the sheet for your report.

If you need to apply number formats to the values you are writing to the worksheet (not just modify formulas so that they will produce whole numbers), that can be done to whole columns within the range at once, i.e.,

...
For i = 1 to rng.Columns.Count
    rng.Columns(i).NumberFormat = formatArray(i)
Next
...

If you need to convert the results of a cell's formula to a rounded value, you can do that with something like

rng.Cells(2, 5).Value = WorksheetFunction.Round(rng.Cells(2, 5).Value, 0)

to give an example for a single cell. This assumes, of course, that the data feeding into the formula are already in the sheet and that the formula has been recalculated.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.