How do I format copied cell text as number? - excel

I have a vba formula that copies a value (which consists in a number) of a cell to another cell. When the number is with decimals (ex: 25,50), the result is ok, but when the number is without decimals (ex: 50), I get an error checking flag suggesting me to convert that cell to number.
I wouldn't have a problem with this error flagging, but I cannot use the result in a formula. For example, if I use SUM(A:A), the formula adds all the numbers, except the flagged ones.
So far, I have tried pasting using the .xlPasteValuesAndNumberFormats property, without any success.
Sub Adauga()
Dim i As Range
Dim cellTaxa As Range
Set cellTaxa = ActiveSheet.Buttons(Application.Caller).TopLeftCell.Offset(1, -2) 'Here I set the range to the value of a cell that is related to the button I click.
n = 2
Set i = Sheets("TJT DETERMINABILA").Cells(n, 10) 'Here I insert the first result
Do While i <> "" 'Here I find the next empty cell in the same column to insert the value.
n = n + 1
Set i = Sheets("TJT DETERMINABILA").Cells(n, 10)
Loop
cellTaxa.Copy 'Here I copy the cell
i.PasteSpecial xlPasteValues 'Here I paste the cell.
End Sub
I would like to be able to use the values that are pasted in the column I mentioned in the code with the SUM formula.
Thank you!

As #urdearboy has stated, an easy trick to convert text to numbers is to add +0.
In my case, I have added cellTaxa = CellTaxa + 0 before the line cellTaxa.Copy and it has solved my issue.

Related

find blank cells in columns and replace them with 0, repeat for next row if next row has data

I am using excel userform to input the data. I want for Columns C2:F2, if the user has not inputted any number in it, excel should convert these blanks into 0. And I want it to repeat for the next row when the data for next row is inputted. I tried some simple coding but it didn't work.
Public Sub BlankCells()
Dim rng As Range
rng = Range("C2:C1000", "D2:D1000", "E2:E1000", "F2:F1000")
For Each cell In rng
If cell = "" Then cell.Value = "0"
Next cell
Find
End Sub
It works if use coding for single row only i.e. "C2:F2", but i want it repeat for the next rows as well when the next row gets the data.
When you create a variable for sheet or range, you need to use the Set keyword like this:
Set rng = Range("C2:F1000")
If you'd like to refer to multiple ranges, you can add all the ranges as one comma separated string

VBA - Enter a formula into all active rows of the current column (when you don't know the column letter, or if the column changes)

Hi guys this is my first post, I'm wondering if you can possibly assist me.
I'd like to write a macro / script that will allow me to put a formula into the column to the right of the currently selected one (for all active rows of the current column) based on what column I've selected. The issue I'm having is that I don't always know the current column letter (as my selection changes from worksheet to worksheet).
To give you an example:
One of my columns currently contains dates, that dates are entered in different formats though, some are separated with ".", some with "-", some with spaces and so on. I have a formula that will deal with this so I need to put this formula in the column to the right of the selected column (which has the dates).
I have been able to do this when I specify the column letter, but not if it changes.
Please can you help?
Give this a go,
Sub SomethingNeat()
Dim rng As Range, x
x = Selection.Column
On Error Resume Next
Set rng = Columns(x).SpecialCells(xlCellTypeConstants, 23)
If Not rng Is Nothing Then rng.Offset(, 1) = "'=MyFormula"
End Sub
You can use ActiveCell.Offset(0,1).Value = Variable
That means that whetever your current cell is you can move and "select" to put a value to the right cell of the one you have activated. You can move the selection using a loop.
Do
Workbooks("Yur workbook name").Worksheets(1).Range(Adress you want to start adding).Offset(0, 1).formula = "=FORMULA"
i = i + 1
ActiveCell.Offset(1, 0).Activate
Loop While i <= max_row
Edit: 2nd
Put the formula in a cell lets say C1
'Select a range
Set take = Worksheets(1).Range("C1")
take.Copy 'copy the formula
Worksheets(1).Paste Destination:=Worksheets(1).Range("B1:B10")
That will copy your function whenever you want it to

Excel Lookup return multiple values horizontally while removing duplicates

I would like to do a vertical lookup for a list of lookup values and then have multiple values returned into columns for each lookup value. I actually managed to do this after a long Google search, this is the code:
=INDEX(Data!$H$3:$H$70000, SMALL(IF($B3=Data!$J$3:$J$70000, ROW(Data!$J$3:$J$70000)-MIN(ROW(Data!$J$3:$J$70000))+1, ""), COLUMN(A$2)))
Now, my problem is, as you can see in the formula, my lookup range contains 70,000 rows, which means a lot of return values. But most of these return values are double. This means I have to drag above formula over many columns until all lookup values (roughly 200) return #NUM!.
Is there any possible way, I guess VBA is necessary, to return the values after duplicates have been removed? I'm new at VBA and I am not sure how to go about this. Also it takes forever to calculate having so many cells.
[Edited]
You can do what you want with a revised formula, not sure how efficient it will be with 70,000 rows, though.
Use this formula for the first match
=IFERROR(INDEX(Data!$H3:$H70000,MATCH($B3,Data!$J3:$J70000,0)),"")
Now assuming that formula in in F5 use this formula in G5 confirmed with CTRL+SHIFT+ENTER and copied across
=IFERROR(INDEX(Data!$H3:$H70000,MATCH(1,($B3=Data!$J3:$J70000)*ISNA(MATCH(Data!$H3:$H70000,$F5:F5,0)),0)),"")
changed the bolded part depending on location of formula 1
This will give you a list without repeats.....and when you run out of values you get blanks rather than an error
Not sure if you're still after a VBA answer but this should do the job - takes about 25 seconds to run on my machine - it could probably be accelerated by the guys on this forum:
Sub ReturnValues()
Dim rnSearch As Range, rnLookup As Range, rnTemp As Range Dim varArray
As Variant Dim lnIndex As Long Dim strTemp As String
Set rnSearch = Sheet1.Range("A1:A200") 'Set this to your 200 row value range
Set rnLookup = Sheet2.Range("A1:B70000") 'Set this to your lookup range (assume 2
columns)
varArray = rnLookup
For Each rnTemp In rnSearch
For lnIndex = LBound(varArray, 1) To UBound(varArray, 1)
strTemp = rnTemp.Value
If varArray(lnIndex, 1) = strTemp Then
If WorksheetFunction.CountIf(rnTemp.EntireRow, varArray(lnIndex, 2)) = 0 Then 'Check if value exists already
Sheet1.Cells(rnTemp.Row, rnTemp.EntireRow.Columns.Count).End(xlToLeft).Offset(0, 1).Value =
varArray(lnIndex, 2)
End If
End If
Next Next
End Sub

Convert from string to int in Excel for empty or blank

As output I have for Range("H" & temp).Cells :
234
0
(Empty)
2
I want to convert it into long or int, because it's a text value. So I did
Range("H" & temp).Cells = CInt(Range("H" & temp).Cells)
It works perfectly for 234, 0 and 2 but when the cell is empty it shows me error. What should I do? I want the empty cell to be taken as 0, of course using VBA macro.
That cell isn't really empty. It has a space or some other non-printing character in it. Try using Trim before using CInt to get rid of the spaces.
If you need to do this in formula, you can use this:
IF(IFERROR(VALUE(A1)); 0; A1)
Enter the number 1 in an unused cell and copy that cell to the clipboard. Highlight the range of cells in which you want blanks replaced by zeros. Choose Edit | Paste Special from the menu. In the Operation section of the resulting dialog box, select the Multiply option and click on OK.You have just multiplied every cell in the range by 1. Numeric cells won't change. Cells containing text data will be ignored. But any cell that was blank will now contain a zero. You could, of course, accomplish the same thing by copying a cell containing 0 to the clipboard and using Add rather than Multiply in the Paste Special dialog.
Sub t()
Dim colNum As Long
colNum = Range("H1").Column 'please coustomize which column
For i = 1 To 4 'Please customize the row range
Cells(i, colNum).Value = CLng(Cells(i, colNum).Value) 'blank --> 0
Cells(i, colNum).NumberFormat = "0.00" 'also changes the numberFormat
Next i
End Sub
This is what worked for me, a custom function, you can change variant to int or double depending on your needs
Function checkEmpty(value As Variant) As Variant
If value = "" Then
checkEmpty = 0
Else
checkEmpty = value
End If
End Function
how to call it:
checkEmpty(cells(1,1).value)

VBA convert all cells in column to type text

I'm using VBA to do some further formatting to a generated CSV file that's always in the same format. I have a problem with my For Each Loop. the loop deletes an entire row if there is more than one blank row which can be determined from the first column alone.
Dim rowCount As Integer
For Each cell In Columns("A").Cells
rowCount = rowCount + 1
'
' Delete blank row
'
If cell = "" And cell.Offset(1, 0) = "" Then
Rows(rowCount + 1).EntireRow.Delete
spaceCount = 0
End If
Next
At some point the value in the loop one of the calls does not have a value of "", it's just empty and causes it to crash. To solve this I think that changing the type of the cell to text before that compare would work but I can't figure out how (no intellisense!!!)
So how do you convert a cell type in VBA or how else would I solve the problem?
Thanks.
Use cell.Value instead of the cell.Text as it will evaluate the value of the cell regardless of the formating. Press F1 over .Value and .Text to read more about both.
Be carefull with the statement For Each cell In Columns("A").Cells as you will test every row in the sheet (over a million in Excel 2010) and it could make Excel to crash.
Edit:
Consider also the funcion TRIM. It removes every empty space before and after a string. If in the cell there is a white space " "; it will look like empty for the human eye, but it has a space inside therefore is different than "". If you want to treat it like an empty cell, then try:
If Trim(cell.value) = "" then
As #andy (https://stackoverflow.com/users/1248931/andy-holaday) said in a comment, For Each is definitely the way to go. This even allows for there to be spaces in between lines.
Example code:
Sub ListFirstCol()
Worksheets("Sheet1").Activate
Range("A1").Activate
For Each cell In Application.Intersect(Range("A:A"), Worksheets("Sheet1").UsedRange)
MsgBox (cell)
Next
End Sub
Thanks Andy!

Resources