VBA Excel delete chunks of non-integer cells - excel

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

Related

How to select the last cell that contains data in a specific column, and then use that cell to move to target cell with offset

I'm trying to find the last row off column D, then move three up, and one to the right and insert data to that column.
The reason for this, is that the above cells are dynamic and can be deleted and added as you go along - the last cell and the cell I'm trying to target, doesn't contain the same reference (except from the last line in any column, which are always three down and one to the right from D (LastRow).
I haven't found a solution in similar posts.
The image shows the sheet.
The cells marked with yellow are the lastRow cell and the cell I'm trying to insert data into.
The cells marked with a blue ring, contains the dynamic cells, which gets longer/shorter depending on the situation.
You may convert that to one line rather than multiple lines.
Dim korrektion1 As Double
Private Sub CommandButton1_Click()
Range("E4").End(xlDown).Offset(-3, 0).Value = korrektion1
Unload UserForm2
End Sub
I ended up solving my problem with the following code:
Dim korrektion1 As Double
Private Sub CommandButton1_Click()
Range("E4").End(xlDown).Select
Selection.Offset(-3, 0).Select
Selection.Value = korrektion1
Unload UserForm2
End Sub

How to round off the specific range value via excel macro

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

Delete blank cells in range

I want to delete blank cells in a range (E1:E130).
This code skips cells.
For Each cell In ranger
If cell.Value = "" Then
cell.Delete
End If
next cell
To make my objective more clear: I have a list of cells with text and empty cells in the range E1:E130 and I want to make a list starting on E1 without any empty cells.
Sorting on alphabet for instance would also be a good solution, however that didn't work out for me either.
I'd go like follows
With Range("E1:E130")
If WorksheetFunction.CountA(.Cells) > 0 Then .SpecialCells(xlCellTypeBlanks).Delete Shift:=xlShiftUp
End With
You could use the Range.SpecialCells Method to delete all blank cells in a specific range at once:
Range("E1:E130").SpecialCells(xlCellTypeBlanks).Delete
You can try with this code to remove blank cell on define range :
Sub RemoveBlankCells()
Dim rng As Range
'Store blank cells inside a variable
On Error GoTo NoBlanksFound
Set rng = Range("E1:E130").SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
'Delete blank cells and shift upward
rng.Rows.Delete Shift:=xlShiftUp
Exit Sub
'ERROR HANLDER
NoBlanksFound:
MsgBox "No Blank cells were found"
End Sub
Necromancing an old question here, but:
OP, I'm not sure your issue (of the 'for each cell in range' not deleting all the wanted cells) stems from the following, but before I knew about Range(...).RemoveDuplicates, i wrote 'for loops' for that very task.
At first i ran the loop from top to bottom and removed the unwanted cells. But when you remove a cell, the whole column shifts up, while your counter stays on the same value, so if there were 2 blank, the second one was shifted up when removing the first, and then the loop jumped over the blank.
So I, then, ran the loop bottom to top (step -1), and that took care of this issue.
This should work:
Columns("E:E").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.Delete Shift:=xlUp
This one uses special methods and is more useful if you wanna erase all the data associated with the blank cells.
Range("A:B").SpecialCells(xlCellTypeBlanks).Select
Selection.EntireRow.Delete

Select range from A1 to end of file using VBA in Excel

I want to select all values in Excel 2007 worksheet between A1 and end of file (effect of ctrl End). There are always 4 columns but the rows will range from 2 to possibly hundreds. There will possibly be lots of blank cells throughout the selection, including the last cell.
The following just goes to the last cell to be selected, not the entire range. How can I modify this to accomplish what I want?
ActiveSheet.Range("A1", SpecialCells(xlLastCell)).Select
Many thanks.
You almost have it. The SpecialCells method needs a qualifier:
ActiveSheet.Range("A1", ActiveCell.SpecialCells(xlLastCell)).Select
If you always want the first four columns, then perhaps:
Sub dural()
Intersect(ActiveSheet.UsedRange, Range("A:D")).Select
End Sub
Record a macro doing it then review the code:
Something like this may work.
Range("A1").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Running this macro selected the below in my example:

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).

Resources