Excel VBA function returning an array

前端 未结 2 1555
礼貌的吻别
礼貌的吻别 2021-01-02 03:06

Can you create an Excel VBA function that returns an array in the same manner as LINEST does, for example? I would to create one that, given a supplier code, returns a list

2条回答
  •  执念已碎
    2021-01-02 03:33

    ok, here I have a function datamapping that returns an array of multiple 'columns', so you can shrink this down just to one. Doesn't really matter how the array gets populated, particularly

    Function dataMapping(inMapSheet As String) As String()
    
       Dim mapping() As String
    
       Dim lastMapRowNum As Integer
    
       lastMapRowNum = ActiveWorkbook.Worksheets(inMapSheet).Cells.SpecialCells(xlCellTypeLastCell).Row
    
       ReDim mapping(lastMapRowNum, 3) As String
       For i = 1 To lastMapRowNum
          If ActiveWorkbook.Worksheets(inMapSheet).Cells(i, 1).Value <> "" Then
             mapping(i, 1) = ActiveWorkbook.Worksheets(inMapSheet).Cells(i, 1).Value
             mapping(i, 2) = ActiveWorkbook.Worksheets(inMapSheet).Cells(i, 2).Value
             mapping(i, 3) = ActiveWorkbook.Worksheets(inMapSheet).Cells(i, 3).Value
          End If
       Next i
    
       dataMapping = mapping
    
    End Function
    
    
    
    
    Sub mysub()
    
       Dim myMapping() As String
       Dim m As Integer
    
       myMapping = dataMapping(inDataMap)
    
       For m = 1 To UBound(myMapping)
    
         ' do some stuff
    
       Next m   
    
    end sub   
    

提交回复
热议问题