Create a Global Counter Variable and Global Limit Variable - VBA

Johnny Thunder

Well-known Member
Joined
Apr 9, 2010
Messages
693
Office Version
  1. 2016
Platform
  1. MacOS
Hi guys,

Working on a niche project today that I don't seem to have found a straight answer for. I have a Bex Analyzer (SAP) query in an Excel file to run a query but I want to trigger a macro at the end of the query refresh/run. I found some code online to figure out when the query is finished (posted below). My issue is that the "Callback" code runs for every query you have in a workbook and most of my workbooks have at least 2 queries.

In my research for a solution is saw the below comment posted to a forum but no code or anything to show how to do this, anyone have any idea how? I would need my code below to be modified to do this step. I would be setting the Global "Limit" to 2 on like 98% of my reports. Thanks for the help


"create a global counter variable in VBA and a global 'limit' variable. Set the global limit variable with the number of bex objects in the workbook, then track when each Bex object is touched in VBA. Then execute the final step when the global counter and the global limit are equal. Then reset the global counter."

My thrown together code
Code:
Private Function getName(i_name As String) As Name
   Dim l_name As Name
     For Each l_name In ThisWorkbook.Names
        If l_name.Name = i_name Then
            Set getName = l_name
        End If
     Next l_name
End Function
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sub CallBack(ParamArray varname())


    On Error Resume Next


Dim lColumn         As Integer
Dim lRange2         As Range
Dim lGridTitle      As String
Dim lName           As Name




lGridTitle = "DF_" & varname(2)


' Get all columns count and clear if necessary
Set lName = getName(lGridTitle)
If Not lName Is Nothing Then
   Set lRange2 = lName.RefersToRange
   Set lRange2 = lRange2.Offset(-1, 0).Rows(1)
   If lRange2.Cells(1, 1).Value = "Table" Then
      lRange2.Cells(1, 1).ClearContents
      lRange2.ClearFormats
   End If
   lName.Delete
End If


' Save new position as Name
ThisWorkbook.Names.Add lGridTitle, "=" & varname(1).Worksheet.Name & "!" & varname(1).Address


 
' Add Accessibility Features
  If Accessibility.paccessibility Then
      Accessibility.reset_menu
      Accessibility.show_menu
  End If
  
 MsgBox "Bex Ran..... Woohoo" 'this is just to prove out that the code ran and how many times
  
  
  End Sub
 
Last edited:

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Forum statistics

Threads
1,223,886
Messages
6,175,194
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top