Question:
Excel Macro - How do I write - If cell (x,y) contains ":" then ...?
thomas m
2010-09-06 08:05:07 UTC
I want to take all cells containing time and move that time to another column.

MUST BE MACRO CODE
Seven answers:
garbo7441
2010-09-06 09:36:43 UTC
Edit: you know, it always helps to have ALL the information when one is attempting to develop a solution.



Your original question stated "all cells containing time".



It did not state "all cells in which a time appears at the end of the cell contents".



That would have made it incredibly simple to come up with a macro to do that, assuming that each cell contains no more than one space. (This assumption may also be a bit of a stretch...)



Sub Redo()

Dim i, LastRow

LastRow = Range("A" & Rows.Count).End(xlUp).Row

For i = 1 To LastRow

If InStr(Cells(i, "A"), ":") Then

Cells(i, "G").Value = Right(Cells(i, "A"), _

(Len(Cells(i, "A")) - Application.Find(" ", Cells(i, "A"))))

Cells(i, "A").Value = ""

End If

Next

End Sub





===========================





There are several points to consider here:



You want to 'move' the cells containing a time value, not copy to another column.



There is no built in 'ISTIME' function in Excel.



You cannot reference a ':' in a cell formatted as Time. The value in the cell is a number between 0 and 1, displayed in a 'time' format.



This can be demonstrated by entering a time in cell A1, then entering this formula in B1:



=Find(".",A1)



and this formula in C1:



=Find(":",A1)



B1 will return '2', as the actual value in A1 is a number such as 0.31, 0.50, etc.



C1 will return a #Value! error as no ':' was found in the string.



The underlying value of a 'time' entry is a number between 0 and 1, with 0 equating to 12:00:00 AM and 1 equating to 11:59:59 PM.

7:31AM, for example, formatted as General would return '0.313194444'. Formatted as Number, to two places, would return '0.31'.



So, if you have other cells in the column that contain values between 0 and 1 that are NOT time values, any macro must be able to differentiate between the cell formats.



Here is a sort of 'brute force' approach that will check all values in column A and if they are formatted as 'Time' will move them to column G, formatting each row in column G as the cell in column A is formatted. Change the "A" column references to the column you wish to validate. Change the "G" references to the column letter you wish to 'relocate' the values to.



Sub tTime()

Dim i, LastRow

LastRow = Range("A" & Rows.Count).End(xlUp).Row

For i = 1 To LastRow

If Cells(i, "A").NumberFormat = "[$-409]h:mm AM/PM;@" Or _

Cells(i, "A").NumberFormat = "[$-409]h:mm:ss AM/PM;@" Or _

Cells(i, "A").NumberFormat = "mm:ss.0;@" Or _

Cells(i, "A").NumberFormat = "[h]:mm:ss;@" Or _

Cells(i, "A").NumberFormat = "[$-F400]h:mm:ss AM/PM" Or _

Cells(i, "A").NumberFormat = "h:mm AM/PM" Or _

Cells(i, "A").NumberFormat = "h:mm:ss AM/PM" Or _

Cells(i, "A").NumberFormat = "h:mm" Or _

Cells(i, "A").NumberFormat = "h:mm:ss" Or _

Cells(i, "A").NumberFormat = "h:mm:ss;@" Then

Cells(i, "G").NumberFormat = Cells(i, "A").NumberFormat

Cells(i, "G").Value = Cells(i, "A").Value

Cells(i, "A").ClearContents

End If

Next

End Sub



Note: if you just enter a 'time' value in a cell, with no prior formatting, the default format assigned is 'Custom - "h:mm".



So, you could possibly eliminate all other 'numberformat' checks in the above macro, keeping only "h:mm".



Now, if you are not concerned about other numerical values in the column that fall between 0 and 1, you could use this macro:



Sub tTime2()

Dim i, LastRow

LastRow = Range("A" & Rows.Count).End(xlUp).Row

For i = 1 To LastRow

If Cells(i, "A").Value >= 0 And Cells(i, "A").Value <= 1 Then

Cells(i, "G").NumberFormat = Cells(i, "A").NumberFormat

Cells(i, "G").Value = Cells(i, "A").Value

Cells(i, "A").ClearContents

End If

Next

End Sub
?
2017-01-20 19:35:49 UTC
1
2016-03-18 08:51:09 UTC
Siti V's answer is pretty good. But David W did include one thing in his code that I would make sure to include, althought his code is faulty. David W's code is faulty for two reasons. First, you need to delete the rows from the last row and work your way to the first row like Siti did. You'll notice that she used a Step -1, which makes her macro work from the bottom to the top like a macro should when deleting rows. This is because the rows shift up when you delete a row and otherwise you will end up skipping a row. David W included a good feature in his macro, but he didn't finish it making it faulty in another way. It's normally a good practice to turn the screen updating temporarily off while you are having a macro change cell values. This increases the speed of the macro because each time a cell value is changed the screen has to be updated if the screen updating property is set to true. So you want to set the screen updating to false at the beginning of the macro. However, at the end of the macro you need to turn the screen updating back on so that you can see the results of the macro. If the screen updating isn't turned back on, then you don't see the results. I tried Siti Vi's macro also and I got it to work and I also got it to fail. Hers works if you have data on row 1. If you don't then the macro fails and deletes all but one row. I took her macro and modified it a little. I tested the macro and it works fine even if row 1 is empty. The macro should also run fast since the screen updating is turned off while the macro is running. Sub Delete_BlueRedRows() Dim MyRange As Range, r As Long Dim LastRow As Long, FirstRow As Long Set MyRange = ActiveSheet.UsedRange 'Finds the row number of the row the data starts on FirstRow = MyRange.Row 'Finds the row number of the last row of data LastRow = MyRange.Row + MyRange.Rows.Count - 1 'Turn the screen updating off to make the macro run faster Application.ScreenUpdating = False For r = LastRow To FirstRow Step -1 If UCase(Trim(Range("J" & r))) = "RED" Or _ UCase(Trim(Range("J" & r))) = "BLUE" Then Range("J" & r).EntireRow.Delete End If Next r 'Turn the screen updating back on so the changes are shown Application.ScreenUpdating = True End Sub
Chastity
2015-08-07 07:37:43 UTC
This Site Might Help You.



RE:

Excel Macro - How do I write - If cell (x,y) contains ":" then ...?

I want to take all cells containing time and move that time to another column.



MUST BE MACRO CODE
Pearl
2016-04-05 05:05:32 UTC
For the best answers, search on this site https://shorturl.im/avzuS



Press the key combination Alt-F11 to open the visual basic editor. I am not a coder, but the code I have provided will work. Someone else might be able to make it prettier. At any rate, insert a new module and paste the following code: Sub DeleteRows() On Error Resume Next Application.ScreenUpdating = False Range("J1").Select 'J1 just says start at the first cell in the field but you can start at whatever cell For X = 1 To WorksheetFunction.CountA(Range("J:J")) If WorksheetFunction.Or(ActiveCell.Text = "Red", ActiveCell.Text = "Blue") Then Selection.EntireRow.Delete Else ActiveCell.Offset(1, 0).Select Next X End Sub
?
2010-09-06 08:14:14 UTC
There is no distinction between macro code and VBA program code in Excel. If you know how to do it in VBA then you know how to do it in a macro.



If the cells that contain a time can be in different columns then you'll have to write a loop that uses relative addressing to search the cells, using substring functions to look for ":" and then move them to where you want them to go.
2014-11-13 01:42:32 UTC
tricky aspect. check out into the search engines. that might help!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...