简体   繁体   中英

Custom VBA function using for loop & ranges

I'm currently trying to right my own function and need some help with looping through a set of data. The code I have posted below allows me to set two variables "CFirstCell" and "CLastCell" (this is only a part of what the overall function will actually do). This will return an address (an example of this being: CFirstCell:"$I$4" & CLastCell:"$AL$4").

I now want to take these two variables and loop through the cells between them (within the sheet called "Client Configuration") and then take those values that are not blank and store them all "AllCodes". Once I have all those values stored in the array "AllCodes", I want to loop through that array and print out a message with each value. How can I do this?

An example of this would be to loop through the range I4:AL4 and then return a message box that would print out the values in cells I4:P4 because they are the only ones that aren't blank.

Public Function GETHOLDINGS(ClientId, Category, CategoryValue, DisplayValueAs) As String

    Dim ClientName As String
    Dim ReportingType As String    

    Dim CFirstCell As String
    Dim CLastCell As String

    Dim AllCodes As String

    ClientName = WorksheetFunction.Index(Sheets("Client Configuration").Range("Client_Config_Table[[#All],[Client Name]]"), _
    WorksheetFunction.Match(1, Sheets("Client Configuration").Range("Client_Config_Table[[#All],[ID]]")))

    ReportingType = WorksheetFunction.Index(Sheets("Client Configuration").Range("Client_Config_Table[[#All],[Portfolio Reporting Type]]"), _
    WorksheetFunction.Match(1, Sheets("Client Configuration").Range("Client_Config_Table[[#All],[ID]]")))

    CFirstCell = WorksheetFunction.Index(Sheets("Client Configuration").Range("Client_Config_Table[[#All],[C1]]"), _
    WorksheetFunction.Match(1, Sheets("Client Configuration").Range("Client_Config_Table[[#All],[ID]]"))).Address
    CLastCell = WorksheetFunction.Index(Sheets("Client Configuration").Range("Client_Config_Table[[#All],[C30]]"), _
    WorksheetFunction.Match(1, Sheets("Client Configuration").Range("Client_Config_Table[[#All],[ID]]"))).Address

End Function

Try this function, please. It should do what (I understood from your words part) you need. In order to understand how it works, I created a sub able to test it:

Sub testGETHOLDINGS()
    Dim sh As Worksheet, rng As Range, CFirstCell As String
    Dim AllCodes As Variant, El As Variant, CLastCell As String
    CFirstCell = "$I$4" 'determine it as you whish or give more
                        'details to find a different way
    CLastCell = "$AL$4" 'determine it as you whish
    Set sh = ActiveSheet 'use here your sheet
    Set rng = sh.Range(CFirstCell & ":" & CLastCell) 'build the range
    AllCodes = GETHOLDINGS(sh, rng) 'use the function to build the
                                    'array of non empty cells value
    If AllCodes = Empty Then Exit Sub 'if rng has more then one row
    For Each El In AllCodes
        Debug.Print El 'it returns in Immediate Window all elements
    Next
End Sub

Private Function GETHOLDINGS(sh As Worksheet, rng As Range) As Variant
  Dim arrC() As String, arrRng As Variant, i As Long, lngEmpty As Long
  Dim nonEmpty As Long, k As Long, rngRow As Long

  rngRow = rng.Cells(1, 1).Row 'determine the range row
  If rng.Rows.count > 1 Then 'stops if rang has more the 1 row
    MsgBox "This function works for one single row range!"
    GETHOLDINGS = Empty: Exit Function
  End If
  'determine how many empty cells are in rng
  lngEmpty = rng.SpecialCells(xlCellTypeBlanks).Cells.count
  nonEmpty = rng.Cells.count - lngEmpty 'non empty cells number
  ReDim arrC(nonEmpty + 1) 'redim the array to the appropriate value
  arrRng = rng.Value 'pass the range values in arrRng array
  For i = 1 To rng.Cells.count 'iterate between the array elements
    If sh.Cells(rngRow, i).Value <> Empty Then
        'load in the array the non empty cells
        arrC(k) = sh.Cells(rngRow, i).Value: k = k + 1
    End If
  Next i
  If arrC(0) <> Empty Then GETHOLDINGS = arrC ' return the array
End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM