How to wait until ActiveWorkbook.RefreshAll finishes before executing more code

I had the same issue with an OLEDBConnection connection type, however DoEvents (as suggested in a prior answer) didn’t help me as my data connections had background-refresh enabled. Instead, using Wayne G. Dunn’s answer as a jumping-off point, I created the following solution, which worked: Sub Refresh_All_Data_Connections() For Each objConnection In ThisWorkbook.Connections ‘Get current background-refresh … Read more

Reverse order of For Each loop

It’s not possible to loop backwards using the for each loop syntax. As an alternative you can use a For i = a To 1 Step -1 loop: Sub reverseForEach() Dim i As Long, rng As Range Set rng = ActiveSheet.Range(“A1:B2”) For i = rng.Cells.Count To 1 Step -1 Debug.Print rng.item(i).Address ‘ Or shorthand rng(i) … Read more

Get Name of Current VBA Function

There’s nothing to get the current function name, but you can build a fairly lightweight tracing system using the fact that VBA object lifetimes are deterministic. For example, you can have a class called ‘Tracer’ with this code: Private proc_ As String Public Sub init(proc As String) proc_ = proc End Sub Private Sub Class_Terminate() … Read more

Excel Date Conversion from yyyymmdd to mm/dd/yyyy

You can convert the value to a date using a formula like this, next to the cell: =DATE(LEFT(A1,4),MID(A1,5,2),RIGHT(A1,2)) Where A1 is the field you need to convert. Alternatively, you could use this code in VBA: Sub ConvertYYYYMMDDToDate() Dim c As Range For Each c In Selection.Cells c.Value = DateSerial(Left(c.Value, 4), Mid(c.Value, 5, 2), Right(c.Value, 2)) … Read more

Display milliseconds in Excel

Right click on Cell B1 and choose Format Cells. In Custom, put the following in the text box labeled Type: [h]:mm:ss.000 To set this in code, you can do something like: Range(“A1”).NumberFormat = “[h]:mm:ss.000” That should give you what you’re looking for. NOTE: Specially formatted fields often require that the column width be wide enough … Read more