Hi everybody
I hope you can help me with 3 things.
The VBA code below works but I think it's a bit slow.
1. )
The macro is set to run auto when Excels open (Private Sub Workbook_Open), so if a person then saves the file, and then opens it again and macro is run, the numbers get corrupted.
I am thinking of adding some code like the following, so the macro should only run if the format is not hh:mm:ss., but I am not sure how to implement it into the code.
2.) If a cell is empty it also converts the format to "hh:mm:ss" and display the cell as 00:00:00 which shouldn't happen. If a cell is blank it should stay blank.
How can this be implemented?
3.) Can the code be written so it's faster?
Thanks in advance.
VBA code:
I hope you can help me with 3 things.
The VBA code below works but I think it's a bit slow.
1. )
The macro is set to run auto when Excels open (Private Sub Workbook_Open), so if a person then saves the file, and then opens it again and macro is run, the numbers get corrupted.
I am thinking of adding some code like the following, so the macro should only run if the format is not hh:mm:ss., but I am not sure how to implement it into the code.
Code:
[I]if Range("R2").NumberFormat <> "h:mm:ss" then do[/I]
How can this be implemented?
3.) Can the code be written so it's faster?
Thanks in advance.
VBA code:
Code:
Sub Sec_to_correct_format()
Application.ScreenUpdating = False
Application.EnableEvents = False
Dim wb As Workbook: Set wb = ThisWorkbook
Set sheet_data = wb.Sheets("Data")
Set sheet_survey = wb.Sheets("Puzzel_survey_calls")
Const convert_sec_to_hh_mm_ss As String = 86400 '# sec to hh:mm:ss by (24*60*60)=86400
With sheet_data
lRow = sheet_data.Range("R" & Rows.Count & ":S" & Rows.Count & ":T" & Rows.Count & ":U" & Rows.Count & ":V" & Rows.Count & ":W" & Rows.Count & ":X" & Rows.Count).End(xlUp).row
Set MR1 = sheet_data.Range("R2:R" & lRow & ":S2:S" & lRow & ":T2:T" & lRow & ":U2:U" & lRow & ":V2:V" & lRow & ":W2:W" & lRow & ":X2:X" & lRow)
For Each cell In MR1
cell.Value = Format((cell.Value / convert_sec_to_hh_mm_ss), "hh:nn:ss")
Next
End With
With sheet_survey
lRow = sheet_survey.Range("C" & Rows.Count).End(xlUp).row
Set MR2 = sheet_survey.Range("C2:C" & lRow)
For Each cell In MR2
cell.Value = Format((cell.Value / convert_sec_to_hh_mm_ss), "hh:nn:ss")
Next
End With
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub