How to round off the specific range value via excel macro - excel

I need to round off the range("M2:M13") value to two decimal points.I wrote the below code using "NumberFormat" option but when I copy paste this value to another workbook it displays me the whole number in formula bar and the rounded off number in the selected cell.
How do I completely round off the numbers to two decimal points?
Sub RoundOff()
Worksheets("Sheets2").Activate
Range("M2").Select
Selection.NumberFormat="0.00"
ActiveCell.FormulaR1C1="RC[-3]/RC[-4]*100"
Range("M2:M13").Select
Selection.FillDown
End Sub

#Nilusha M. It s missing one "=" before "RC[-3]/" from your formula.
You can try:
Sub Test()
With ThisWorkbook.Worksheets("Sheet2")
.Range("M2").FormulaR1C1 = "=RC[-3]/RC[-4]*100"
With .Range("M2:M13")
.FillDown
.NumberFormat = "0.00"
End With
End With
End Sub

You can do by simply doing
range("A1").EntireColumn.NumberFormat = "#.00"
It will format the entire A column

Related

Macro to work on selected columns, not set columns (text to number)

i'm sure this is simple, but i'd like to know how to have a macro work only on a column that is selected (prior to running the macro)
the ultimate goal is to convert text to numbers, but the column these values are in are not always in the same column, without converting the entire workbook, it would only need to convert the selected column.
If I understand correct, you can try this.
a = ActiveCell.Column
Columns(a).Select
Selection.NumberFormat = "#,##0.00"
I found an answer that works using:
Sub change_txt_2_number()
With Selection
.NumberFormat = "0"
.Value = .Value
End With
End Sub

Setting a dynamic print area

I am trying to define the print area for the active sheet that would include all rows in columns A thru X.
The number of rows changes with each active sheet.
This gives me a runtime error and thinks I'm trying to write a formula.
Sub setprintarea()
' Sets Range from cell A1 for all rows thru column 24 (X)
ActiveSheet.PageSetup.PrintArea = "$A$1:$X$"
End Sub
You have a minor syntax error. To specify full columns, you will want to specify "$A:$X". This may give the impression that you will print the entirety of columns A through X. However, that is not the case. Excel will print only enough sheets as needed for cells containing content.
As an example, create a new worksheet. On the worksheet, enter values in random cells in columns A through C. Next, set the print area to $A:$B. Now, do a print preview. You will see that Excel is smart enough to only create pages where data exists.
Corrected code:
ActiveSheet.PageSetup.PrintArea = "$A:$X"
It is true that the OP probably doesn't want the range to extend to the last row on the sheet. It is also possible that X is not the longest column on the sheet, in which case you would want to test each column to determine the sheet length. Hope this doesn't overcomplicate the matter, but sometimes the last column isn't the sheet length.
MaxLastRow = 1
For x = 1 To 24
If ActiveSheet.Cells(Rows.Count, x).End(xlUp).Row > MaxLastRow Then
MaxLastRow = ActiveSheet.Cells(Rows.Count, x).End(xlUp).Row
End If
Next x
ActiveSheet.PageSetup.PrintArea = "$A$1:$X$" & MaxLastRow
Dynamic Print Area
Sub setPrintArea()
With ActiveSheet
.PageSetup.PrintArea = Intersect(.UsedRange, .Columns("A:X")).Address
' Write the address to the Immediate window (CTRL+G).
Debug.Print .PageSetup.PrintArea
End With
End Sub

VBA Excel delete chunks of non-integer cells

I have an excel file with a lot of data and I would like to reduce it by cutting parts of it. For instance, I would like to delete the real numbers (non-integers) and keep only the integers in the first column. There are over a thousand lines and a macro is crucial. A sample can be seen below (the red contours needs to be deleted).
I have tried using IsNumeric and something like this:
Sub Macro1()
For Each Cell In Selection
If Cell.Value = IsNumeric(Cell.Row) Then
Rows(Cell.Row).ClearContents
End If
Next
End Sub
But it doesn't work.
Rather than IsNumeric, try comparing them against rounded values to eliminate non-integers.
Sub Macro1()
For Each Cell In Selection
If Cell.Value <> round(cell.value,0) Then
Rows(Cell.Row).ClearContents
End If
Next
End Sub

Excel VBA - Convert string entered in user form to number

I have an excel user form into which the user enters numbers, when those numbers are entered into the spreadsheet they appear with the notification that this is a number stored as text. =SUM(H6:H13) shows a zero result.
I have tried NumCrtn = cLng(NumCrtn) - doesn't change the cell to a number, formula still shows zero.
I have tried NumCrtn = Val(NumCrtn) - doesn't change the cell to a number, formula still shows zero.
I have tried copy and paste.special to a value and that doesn't change it to a number either.
Don't know what to do.
Help!
Try this one:
With Range("H6:H13")
.NumberFormat = "0"
.Value = .Value
End With
Edit:
Another solution. Building on Pradeep Kumar's suggestion which deals with preparing your range before you enter the data, Change your code to something like this
Private Sub UserForm_Initialize()
Dim aCell As Range
Range("H6:H13").NumberFormat = "0"
'This is to cater for any previous values if filled in
For Each aCell In Range("H6:H13")
aCell.Formula = aCell.Value
Next
End Sub
Private Sub CommandButton1_Click()
'Entering value for H6
Range("H6").Value = TextBox1.Value
End Sub
Range("H6:H13").NumberFormat = "#,##0"
It is not a VBA solution, but an ordinary Excel solution.
Do like this
Select the column
Select Data - Text to columns
Quite often the default settings will do and you can click Finish. Otherwise you
will have to make sure that the result is just a "General" column
A macro doing just this would look something like this:
Columns("A:A").TextToColumns
There's a lot of parameters to the TextToColumns method, but it should work fine with default values only (i.e. no parameters).

Format cell for comma-separated values

Written a VBA script which outputs numbers and I thought I had the correct format string for thousand-separating (4,656,565 5,343 232,434 etc) but its not working for certain magnitudes of numbers.
So far I am using Cells(x,y).NumberFormat = "#,###"
can someone provide me with the correct format string to thousand-comma-separate any number, no matter the magnitude?
This works for me. Notice, formatting the cell first before assigning the number to it
Option Explicit
Sub Sample()
With Cells(1, 1)
.NumberFormat = "#,##0"
.Value = 4.65656553432324E+16 '46565655343232400
End With
End Sub
RESULT
Cell A1 has 46,565,655,343,232,400

Resources