matlab com interface: add vba code to specific sheet

Andi 1 Reputation point
2022-10-04T08:26:47.55+00:00

Hello, for quite some time I am trying to add VBA code to a certain sheet/ table instead of to Module.
Below you can find the Matlab code using the COM interface.
My actual aim is to have a callback/ listener which updates the number of filtered items.
I cannot use "Subtotal/ Teilerergebnis" as I have merged cells and so the Excel internal function fails.
Hopefully someone can help me

regards
Andi

dummy Matlab code

function minimalExampleVBACode  
  
    Excel = actxserver('Excel.Application');  
    Excel.Visible = 1;  
    Workbook = invoke(Excel.Workbooks, 'Add');  
  
    % prepare sheet  
    Sheet = Workbook.Sheets.Item(1);  
    Sheet.Name = 'sheet for vba code';  
  
    % create macro file for import  
    MacroString = [ '''\n' ...  
                    ''' Author: Andreas Rechtsteiner\n' ...  
                    ''' Mail:   ******@zf.com\n' ...  
                    ''' Date:   ' date '\n' ...  
                    ''' Both the excel file and macro were generated with <' mfilename '.m>.\n' ...  
                    '''\n' ...  
                    ''' Output functions \n' ...  
                    '''\n' ...  
                    'Function outputRows() As Integer\n' ...  
                    '    nRows = Trim(Str(ActiveWorkbook.ActiveSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row))\n' ...  
                    '    outputRows = Application.WorksheetFunction.Subtotal(3, Range("B2:B" & nRows))\n' ...  
                    'End Function'];  
  
    FH = fopen('tmp_VBAMacro.txt', 'w+'); % open or create file for reading and writing; discard existing contents  
    fprintf(FH, MacroString);  
    fclose(FH);  
  
    % import macro file  
    Excel.VBE.ActiveVBProject.VBComponents.Import(fullfile(pwd, 'tmp_VBAMacro.txt'))  
    % or  
    Workbook.VBProject.VBComponents.Import(fullfile(pwd, 'tmp_VBAMacro.txt'))  
  
    Excel.DisplayAlerts = 0;  
    % Save Excel Sheet  
    invoke(Workbook, 'SaveAs', fullfile(pwd, [mfilename '.xlsm']), 52); % 52 for xlsm extension  
  
    % Release handles  
  
    release(Workbook)   
    delete(Excel);  
  
end  

desired VBA code of actual file for expressing my need

Private Sub Worksheet_Calculate()  
    Cells(1, 13).Value = Str(countUniqueVisible()) & " of 404 testcases"  
End Sub  
  
Function myVisible(Zelle As Range) As Boolean  
    myVisible = Not (Zelle.EntireRow.Hidden Or Zelle.EntireColumn.Hidden)  
End Function  
  
Function countUniqueVisible() As Integer  
  
    idxCol = 12  
      
    nRows = ActiveWorkbook.ActiveSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row  
      
    Dim Dict As Object  
    Dim j As Long  
    'put unique data elements in a dictionay  
    Set Dict = CreateObject("Scripting.Dictionary")  
      
      
    For idxRow = 2 To nRows  
        If myVisible(Cells(idxRow, idxCol)) Then  
            Dict(Cells(idxRow, idxCol).Value) = 1  
        End If  
    Next idxRow  
    countUniqueVisible = UBound(WorksheetFunction.Transpose(Dict.keys))  
    Application.EnableEvents = True  
      
End Function  
Developer technologies Visual Basic for Applications
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.