简体   繁体   English

使用 for 循环和范围的自定义 VBA 函数

[英]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).我在下面发布的代码允许我设置两个变量“CFirstCell”和“CLastCell”(这只是整个函数实际执行的一部分)。 This will return an address (an example of this being: CFirstCell:"$I$4" & CLastCell:"$AL$4").这将返回一个地址(例如: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".我现在想要获取这两个变量并遍历它们之间的单元格(在名为“客户端配置”的工作表中),然后获取那些非空白值并将它们全部存储为“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.一旦我将所有这些值存储在数组“AllCodes”中,我想遍历该数组并打印出包含每个值的消息。 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.一个示例是循环遍历范围 I4:AL4,然后返回一个消息框,该消息框将打印出单元格 I4:P4 中的值,因为它们是唯一不为空的值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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