Number stored as text to number with VBA - vba

I have some columns that have cells that are "Number stored as text", which is causing problems with other code that is trying to copy one range to another
rng1.Copy Destination:=rng2
The destination range (rng2) is blank. Not had this issue with any other data, just when these "Number stored as text" are there, so I need to, via VBA, be able to convert them to number.
Looking around there seems to be two methods for this, but neither are working for me...
TextToColumnns:
rng1.Select
Selection.TextToColumns Destination:=Range(rng1), DataType:=xlDelimited
Value = value:
Range(rng1).Select
With Selection
.NumberFormat = "General"
.Value = .Value
End With
Other ways I have discovered are 100+ lines of code long which surely cannot be right??
What is the best way to achieve this?

What is wrong with something like this:
Public Sub TestMe()
With Selection
.NumberFormat = "0.00"
.Value = .Value
End With
End Sub

You can convert them with this:
Function text_to_number(txt As String) As Double
text_to_number = txt * 1.0
End Function
So, first of all, you need to convert them, and after this, copy to another place.
Something like this:
Sub copy_and_paste()
Dim numbers(30) As Double
'Copping from range A1:A30
For i = 1 To 30
numbers(i) = text_to_number(Cells(i, 1).Value)
Next i
'Pasting tn B1:B30
For i = i To 30
Cells(i, 2).Value = numbers(i)
Next i
End Sub

Related

Trying to combine two VBA scripts into one

I am very new to VBA Scripts. I have been googling trying to figure out how to combine these together and assign a button to run this.
My first script I am adding a sequence number and row to a table. The table is two columns B:C. My numbering is looking at the row below the line I am inserting to keep the numbers in sequential order. (I found this on Youtube works great)
Private Sub CommandButton2_Click()
Sheets("Sheet1").Range("B4").Select
ActiveCell.EntireRow.Insert Shift:=xlDown
Sheets("Sheet1").Range("B4:C4").Select
Selection.Borders.Weight = xlThin
Sheets("Sheet1").Range("B4").Select
ActiveCell.Value = "=B5+1"
End Sub
Second one is applying a timestamp to C4 which is giving a timestamp to the sequential number.
Private Sub timeStamp()
Dim ts As Date
With Range("C4")
.Value = Now
.NumberFormat = "h:mm:ss AM/PM"
End With
End Sub
I cannot figure out how to make these two run together. Individually they work.
Thank you in advance for any help.
How about you just paste the code of the second in the first (and a few other adjustments):
Private Sub CommandButton2_Click()
Dim ts As Date
With ThisWorkbook.Sheets("Sheet1")
.Range("B4").EntireRow.Insert Shift:=xlDown
.Range("B4:C4").Borders.Weight = xlThin
.Range("B4").Value= "=B5+1"
With .Range("C4")
.Value = Now
.NumberFormat = "h:mm:ss AM/PM"
End With
End With
End Sub

VBA - Index SumProduct with mis

I am new to VBA and am working on a userform at the moment that has 3 ComboBoxes that a user picks 1 item from each. The intent is for the code to look up the corresponding 4th value from the spreadsheet and return it to a textbox on the userform. Right now I'm getting a "Type Mismatch (Error 13):"
I have been fiddling with this for going on 3 days now. Some websites say the SumProduct function doesn't work in VBA, some say it does as long as you specify "WorkSheetFunction", and still some say you should instead use 'evaluate'.
Like in this post:
SUMPRODUCT Formula in VBA
(I didn't have much luck using 'evaluate' but my syntax might have been off)
Anyway, I created a quick example to show what I am trying to do. If anyone can help it would be tremendously appreciated.
worksheetdata
this is the code I have been trying:
Private Sub TestButton_Click()
textboxTesting.Text = Application.WorkSheetFucntion.Index(Range("Thickness"), _
Application.WorkSheetFucntion.SumProduct((Range("Wood") = "Oak") _
* (Range("Metal") = "Copper") * (Range("Box") = "Red")), 0)
End Sub
A better approach is to write your own function, I believe the below is much more understandable:
Sub Test()
Dim thickness
thickness = FindThickness("oak", "copper", "red")
MsgBox (thickness)
End Sub
Function FindThickness(wood As String, metal As String, box As String) As String
Dim rng As Range
Set rng = Range("Wood")
For Each cell In rng
If cell.Value = wood _
And cell.Offset(0, 1).Value = metal _
And cell.Offset(0, 3).Value = box Then
FindThickness = cell.Offset(0, 2).Value
Exit Function
End If
Next cell
End Function

VBA: Convert all worksheet text to number

I'm looking to to convert automatically all number stored as text in worksheet to numbers with VBA. Excel seems to detect automatically theses values, there is a way in VBA to convert all theses numbers ?
I found this following solution to convert a text to a number, but i want this apply to all the worksheet and not to a specify Range, because the worksheet is dynamic.
Range("F:F").Select
With Selection
Selection.NumberFormat = "General"
.Value = .Value
End With
Somebody have an idea ?
with activesheet.usedRange
.numberFormat = "General"
.value = .value
End with
Guessing that the problem is in the different decimal separator, which is quite a big problem in Germany and France. If this is the case, this works:
Option Explicit
Public Sub TestMe()
On Error GoTo TestMe_Error
Dim myCell As Range
Dim tryCell As String
For Each myCell In Cells.SpecialCells(xlCellTypeConstants)
tryCell = Replace(myCell, ",", ".")
If IsNumeric(tryCell) Then
myCell = Replace(myCell, ",", ".")
End If
Next myCell
On Error GoTo 0
Exit Sub
TestMe_Error:
MsgBox "No contant values!"
End Sub
The code checks all the cells without formula in the ActiveSheet. Then, if by changing the , to a . the cell would become numeric, it changes it.

How to change range to run only when there information

I have an excel sheet where I am doing a VlookUP using VBA. The problem is that I extract information, and the amount is always different. I want to find a way to add to the code that will add information until there is no more information to add.
Here is the code that works but only for the cells I put in:
Sub vLook()
With ThisWorkbook.Worksheets("EODComponents").Range("f5:F200")
.Formula = "=VLOOKUP(C5,($H$5:$i$34),2,FALSE)"
.Value = .Value
End With
End Sub
You can set a lastRow:
Sub vLook()
Dim lastRow as Long
With ThisWorkbook.Worksheets("EODComponents")
lastRow = .Cells(.Rows.Count,6).End(xlUp).Row
With .Range("f5:F" & lastRow)
.Formula = "=VLOOKUP(C5,($H$5:$i$34),2,FALSE)"
.Value = .Value
End With
End With
End Sub
Maybe you can try to use a Do while-loop?
If you put the while statement right, this will continue until the statement becomes false, in your case; there is no more information to add.
You can use the Len()-function to check the length of the text/value inside a cell, when this is zero you can assume the cell is empty.
More information about this item can be found here.
Example:
Public Sub Something()
Dim i As Integer
i = 1
Do While (Len(Cells(i, 1).Text) > 0)
i = i + 1
Loop
MsgBox "The next row of column 'A' is empty: A" & i
End Sub
Hope this helps.

VBA Right-Function returning wrong data type

I have written a very simple code which returns the last 6 characters of every active cell within a range.
The code works pretty good until it finds a particular cell in which the characters to be returned should be: "MARC01". Unfortunately it returns a date type character (01.Mrz).
By using the normal excel formula it works fine, that is why I would expect it to work with a Macro as well.
Here you can see my code which takes the strings from column "A" and enters it in column "B":
Range("B12").Activate
Do
ActiveCell.Value = Right((ActiveCell.Offset(0, -1).Value), 6)
ActiveCell.Offset(1, 0).Activate
Loop Until ActiveCell.Offset(0, -1).Value = 0
Excel likes to change anything that looks like a possible date to a date. To force this not to happen put a "'" in front of the formula.
ActiveCell.Value = "'" & Right((ActiveCell.Offset(0, -1).value), 6)
This will force it to stay text. The down side to this is, if it is a number it will be saved as text.
Excel likes to try to interpret certain data, rather than just leaving it as is. It especially does that with strings that look like dates, and with numeric entries.
Two ways to workaround are
Put the text prefix character in front of your string. This is usually a single quote. (see Scott's answer for code)
Format the cell as Text before you place the value there.
Sub foo()
Range("B12").Activate
Do
ActiveCell.NumberFormat = "#"
ActiveCell.Value = Right((ActiveCell.Offset(0, -1).Formula), 6)
ActiveCell.Offset(1, 0).Activate
Loop Until ActiveCell.Offset(0, -1).Value = 0
End Sub
With this simple goal, I don't know why you need VBA looping.
You can just mass set the formular1c1 to =RIGHT(RC[-1],6).
Option Explicit
Sub Right6()
Const R6LeftCol = "=RIGHT(RC[-1],6)"
Dim oRng As Range, lRow As Long
lRow = Cells(Rows.Count, "A").End(xlUp).Row
Set oRng = Range("B12")
Range(oRng, Cells(lRow, "B")).FormulaR1C1 = R6LeftCol
Set oRng = Nothing
End Sub