There seems to be lots of similar questions but nothing that quite seems to answer this one.
I have created the below sub to convert a column of text to dates. It works in some scenarios, i.e. where the date has 8 characters such as 10022017, but not if the value has 7 characters such as 1022017.
Any ideas appreciated.
Sub convertDate()
'Find the last Row with data in a Column
Dim lastRow As Long
Dim i As Long
With ActiveSheet
lastRow = .Cells(.Rows.Count, ColumnSelect).End(xlUp).Row
End With
'convert column from string to date
For i = RowSelect To lastRow
Cells(i, ColumnSelect).Value = Format(Cells(i, ColumnSelect).Value, "00/00/0000")
Next i
End Sub
Hi again all, still having a few problems, the ideal formula seems to be =DATEVALUE(TEXT(A1,"00-00-0000")) if for example A1 = e.g. 02022017. Still haven't found the best way to do this with VBA as everything seems to run up against the truncated zeros problem. Thanks in advance
If your format is going to be consistent then try this.
This adds a 0 before Cells(i, ColumnSelect).Value when there are 7 characters.
For i = RowSelect To lastRow
Select Case Len(Trim(Cells(i, ColumnSelect).Value))
Case 8
Cells(i, ColumnSelect).Value = Format(Cells(i, ColumnSelect).Value, "00/00/0000")
Case 7
Cells(i, ColumnSelect).Value = Format("0" & _
Cells(i, ColumnSelect).Value, "00/00/0000")
End Select
Next i
But be careful with numbers like 1122017... this can be 11/2/2017 or 1/12/2017
Edit
Looking at 10022017, I realised that 1122017 will be 1/12/2017 and not 11/2/2017 as your mm zeros are not getting truncated.
OP wants a formula as well
=IF(LEN(A1)=8,TEXT(DATEVALUE(LEFT(A1,2)&"/"&MID(A1,3,2)&"/"&RIGHT(A1,4)),"dd/mm/yyyy"),TEXT(DATEVALUE(LEFT(A1,1)&"/"&MID(A1,2,2)&"/"&RIGHT(A1,4)),"dd/mm/yyyy"))
Related
The main problem started when I wanted to "convert to number" by the green triangle (I know I can do it by hand, but there are a lot of cells like that and in the future I only want to use code).
So I wanted to do it by code, and I came across with this code that helps, but I have a problem with the number format which removes the decimal numbers.
Sub Valor3()
Dim LastRow As Long, i As Long
LastRow = Sheets("Hoja3").Range("A" & Rows.Count).End(xlUp).Row
'Sheets("Hoja3").Range("A1:A" & LastRow).NumberFormat = "# ##0,00"
For i = 1 To LastRow
If Val(Sheets("Hoja3").Range("A" & i).Value) <> 0 Then _
Sheets("Hoja3").Range("A" & i).Formula = _
Val(Sheets("Hoja3").Range("A" & i).Value)
Next i
End Sub
I've been trying many formats but none of them seems to help.
It might be because here we use the comma as a decimal separator and there is no miles separator.
What number format would help me?
The issue is that you use Val function in combination with a non-us-english decimal separator, which is not a proper solution to your issue.
The Val function recognizes only the period ( .) as a valid decimal separator. When different decimal separators are used, as in international applications, use CDbl instead to convert a string to a number.
Source: Microsoft documentation Val function.
Since the Val function does not convert a text into a value but extracts
The Val function only works with a dot . as decimal separator.
Example:
Val("2.55") 'will return 2.55 as number
Val("2,55") 'will return 2 as number (because it cuts off all text and the comma is not considered as decimal separator)
To get rid of the green triangle and convert a number that is saved as text into a real number properly, use the following:
Option Explicit
Public Sub ConvertNumberAsTextIntoRealNumber()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Hoja3")
Dim LastRow As Long
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
With ws.Range("A1", "A" & LastRow)
.NumberFormat = "# ##0.00" 'set your desired number format
.Value = .Value 'this will in most cases already convert to real numbers.
End With
'But if your numbers are hard coded to text and begin with a `'` you need the following additionally:
Dim iRow As Long
For iRow = 1 To LastRow
With ws.Cells(iRow, "A")
If IsNumeric(.Value) Then 'can the value be interpreted as a number
If .Value <> 0 Then 'is the value not zero
.Value = CDbl(.Value) 'then convert it into a real number
End If
End If
End With
Next iRow
End Sub
I know you are looking for VBA solution, but here's a small Excel trick that you might find useful:
Enter 1 (numeric value) somewhere in the file and copy it:
Select your range (A1:A6) and go to Paste > Paste Special > select Multiply:
The final result is all your text values being converted to numbers:
The same trick will work with other combinations, e.g. Operation: Add while having 0 copied, etc.
Hi I am writing Excel VBA code. I just wanted to do subtraction for this kind of table
But I can't figure out how to do that using VBA. Is the code below correct?
Sub OperationO()
Dim OE As Integer
Dim i As Integer
For i = 3 To Range("F6")
Cells(6, i).Value = Cells(2, i).Value - Cells(4, i)
Next i
End Sub
You should really use formulas for something like this, but if you insists on VBA, here it goes.
Whenever you write code for VBA or any similar languages, read what you are writing, what it should/needs to do, as you read it out loud many times the errors pop up. Think of the comments below as "me reading as I write."
Sub OperationO()
'Initiate a Variable type Integer for number storage
Dim OE as Integer
'Initiate another variable same type to use in the loop
Dim i as Integer
'Start a loop from 3 to 6 (because these are the columns you are working with)
For i = 3 to 6
'Set the value in Column "i" on Row 6 to the value in Row 2 minus Row 4 in the same column
'Now here is the thing, when you subtract a negative number, you are adding it, crazy math rules i know, so if the number is negative, you need to Add instead.
Cells(6, i).Value = (Cells(2, i).Value + Cells(4, i).Value)
'If both cells don't contain a number, this will fail, additional checks may help, like IsNumber
Next i
'Its the end of the Sub and you never used the Variable OE, it was declared for nothing.
End Sub
I'm relatively new to VBA and I'm trying to write a macro that will compare two columns of data (first and last names). While traversing the column, any time first name = last name (ie. they're both blank or say UNKNOWN) I want the cell in the 9th column to be cleared and the cell in the 10th column to get the value UNKNOWN.
As of now, the code correctly recognizes any time when the first and last name are identical. My problem is that any time first name is a sub-string of any last name (ie. cell I2=David J2=Jones , I3=Joseph J3=Davidson) David gets compared with Davidson and is subsequently erased.
I've spent a while looking for similar problems and I haven't been able to adapt anything to my problem thus far. Thanks in advance for any help.
Sub compare_cols()
Dim Report As Worksheet
Dim i As Integer, j As Integer
Dim lastRow As Integer
Set Report = Excel.ActiveSheet
lastRow = Report.UsedRange.Rows.count
Application.ScreenUpdating = False
For i = 1 To lastRow ' This will find all identical pairs of cells in I,J (blank, blank) or (unknown, unknown). I stays blank, J gets UNKNOWN
For j = 1 To lastRow ' I think its currently erasing any matches (ex. if someones first name is James, it will get erased if there is a last name jameson)
If InStr(1, Report.Cells(j, 10).Value, Report.Cells(i, 9).Value, vbTextCompare) > 0 Then
Report.Cells(i, 9).Value = ""
Report.Cells(i, 10).Value = "UNKNOWN"
Exit For
Else
End If
Next j
Next i
Application.ScreenUpdating = True
End Sub
Unlike some other languages, you can compare strings in vba just using the "=" sign and that will find exact matches, which is what it appears you are looking for. Try
if Report.Cells(j, 10) = Report.Cells(i, 9) etc.
I am trying to apply an Excel formula to sort the excel columns but not able to come up with it.
The excel entries looks like this:
As you can see, In the first column the records are having 000 at the end.
1) I need to neglect the last 000 from column A, compare the content of Column A with Column B.
a) If it matches, then remove the 000 from the Column A.
b) If it does not matches, then delete the entire row OR make the content for both the column as N/A.
Please help me out with this, the Content of this excel is a huge one, hence doing it manually is not feasible:(
Thanks,
If you're looking to do this with VBA, you can add the following macro to your workbook:
EDIT
Your comment states that you're looking for both leading AND trailing zeros, not just trailing like the original question suggests. If that's the case, you can accomplish this by a simple conditional statement that checks where the 3 zeros are, if anywhere.
If possible, it would be much simpler to Format the cells Number type to remove leading zero's. If that's not possible, then here's the macro that achieves just that:
Public Sub CompareValues()
Dim rowCount As Integer, currentRow As Integer
Dim firstCellValue As String
Dim secondValValue As String
rowCount = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
Dim test As String
For currentRow = rowCount To 1 Step -1
'remove three leading or trailing 0's from value
test = Left(Cells(currentRow, 1).Value, 3)
If Left(Cells(currentRow, 1).Value, 3) = "000" Then
firstCellValue = Right(Cells(currentRow, 1).Value, Len(Cells(currentRow, 1).Value) - 3)
Else
firstCellValue = Left(Cells(currentRow, 1).Value, Len(Cells(currentRow, 1).Value) - 3)
End If
secondValValue = Cells(currentRow, 2).Value
If Not IsEmpty(firstCellValue) And Not firstCellValue = "" Then
If firstCellValue = secondValValue Then
Cells(currentRow, 1).Value = secondValValue
Else
Cells(currentRow, 1).EntireRow.Delete
End If
End If
Next
End Sub
Before Macro
After Macro
If the first column is always numeric and always has 3 zeros, then simply divide by 1000 and do the compare.
If not, then convert to a string and substr the first (length -3) characters and compare to the string result of the 2nd column
I'm new to VBA and I can't manage to do what I want although it's very simple.
I need to automatically modify cells of a big (333x333) empty (full of zeros) spreadsheet.
In a separate spreadsheet I have the row and column of all the cells to modify. (5000 of them)
A for loop seems to be suited for this purpose.
Here is the code of my macro. The problem appears on the line before the last one.
Dim val1 As String, val2 As String, i As Integer
For i = 1 To 333
Sheets("Feuil2").Activate
ActiveSheet.Cells(i, 1).Select
val1 = Cells(i, 1).Value
val2 = Cells(i, 2).Value
Sheets("Classeur2.csv").Select
Cells(val1, val2).Select
ActiveCell.FormulaR1C1 = "1"
Next i
The line that causes a problem is this one : Cells(val1, val2).Select
I believe my error is a syntax error. But I can't find out what I should add before, after or around my two variables "val1" and "val2"
What do you think ?
Thanks a lot for your help.
Nicolas.
Edit
My problem is now solved :
The first answer is exactly what I needed to male my macro work.
The second answer is the proper and faster way to do it.
No need to activate or selection sheets or cells if you're using VBA. You can access it all directly.
The code:
Dim rng As Range
For Each rng In Sheets("Feuil2").Range("A1:A333")
Sheets("Classeur2.csv").Cells(rng.Value, rng.Offset(, 1).Value) = "1"
Next rng
is producing the same result as Joe's code.
If you need to switch sheets for some reasons, use Application.ScreenUpdating = False at the beginning of your macro (and Application.ScreenUpdating=True at the end). This will remove the screenflickering - and speed up the execution.
VAL1 and VAL2 need to be dimmed as integer, not as string, to be used as an argument for Cells, which takes integers, not strings, as arguments.
Dim val1 As Integer, val2 As Integer, i As Integer
For i = 1 To 333
Sheets("Feuil2").Activate
ActiveSheet.Cells(i, 1).Select
val1 = Cells(i, 1).Value
val2 = Cells(i, 2).Value
Sheets("Classeur2.csv").Select
Cells(val1, val2).Select
ActiveCell.FormulaR1C1 = "1"
Next i