簡體   English   中英

VBA-需要創建一個函數,它將范圍作為輸入

[英]VBA- Need to create a function, which takes the range as input

我在Excel中有一個二維表。 例如。

outputproduct      blending combination
**5                P1:0.6/P3:0.5**
  2                P1:0.3/P2:0.7
  4                P5:0.4/P2:0.7
  7                P11:0.7/P7:0.4

假設表的范圍從B2:C6變化(它可以變化)。 我必須創建一個函數,其第一個工作是讀取此范圍(這將是用戶定義的輸入),然后將數據存儲到二維數組中,以便我可以使用第一列中的數據(整數)和適當地在第二列中的字符串。

第一列是所得產物指數,而第二列是給定比例的混合產物,它們組合在一起以得到第一欄中的產物。

然后是另一張桌子:

product index      current stock    updated stock
      **1**             **10**
        2                 20 
      **3**             **50**
        4                 15
      **5**             **100**
        .                 .
        .                 .
        .                 .

我必須在數據處理后更新此表中的庫存量。 例如,在產物1與產物3以6:5(單位)的比例組合時,產生1單位產物5。 因此,我必須更新表2中每種產品的庫存量。

任何建議,如何將范圍轉換為二維數組?

Public Function Blending_function( R1 as Range, R2 as Range)
 ' R2 is the range of table 2, where the updating is to be done
 ' R1 is first stored in to a 2 dimensional array, such that the data in the
 ' column 1 could be read, and the data in the column 2 could be read (of table 1).
 ' the integer in the column 1 of table 1 refers to the product index in table 2.
 ' P(i) stands for the ith product. In first row of table-1, P1 and P3 combine in the 
 ' ratio of 6:5 to give P5. The current stock of each product is provide in table-2,
 ' whose range is R2(entire table 2).

 ' R1 is the range of table 1, from where the processing is to be done


End Function 

對我來說,主要障礙是將范圍R1(表-1)轉換為二維數組。 然后從該數組中查看輸出產品的索引,並在表2中找到該產品以更新庫存水平。

以下是如何使用2D數組的示例。 該函數將分解blending combination並提取所需的值,以便您可以使用它們。

Sub Sample()
    Dim Rng1 As Range, Rng2 As Range

    On Error Resume Next
    Set Rng1 = Application.InputBox("Please select the Table1 Range", Type:=8)
    On Error GoTo 0

    If Rng1.Columns.Count <> 2 Then
        MsgBox "Please select a range which is 2 columns wide"
        Exit Sub
    End If

    On Error Resume Next
    Set Rng2 = Application.InputBox("Please select the Table2 Range", Type:=8)
    On Error GoTo 0

    If Rng2.Columns.Count <> 3 Then
        MsgBox "Please select a range which is 3 columns wide"
        Exit Sub
    End If

    Blending_function Rng1, Rng2

End Sub

Public Function Blending_function(R1 As Range, R2 As Range)
    Dim MyAr1 As Variant, MyAr2 As Variant
    Dim i As Long
    Dim blndCom As String, OutputPrd As String
    Dim ArP1() As String, ArP2() As String, tmpAr() As String

    MyAr1 = R1

    For i = 2 To UBound(MyAr1, 1)
        OutputPrd = MyAr1(i, 1)
        blndCom = MyAr1(i, 2)
        tmpAr = Split(blndCom, "/")

        ArP1 = Split(tmpAr(0), ":")
        ArP2 = Split(tmpAr(1), ":")

        Debug.Print OutputPrd
        Debug.Print Trim(ArP1(0))
        Debug.Print ArP1(1)
        Debug.Print ArP2(0)
        Debug.Print ArP2(1)
        Debug.Print "-------"
    Next
End Function

快照

在此輸入圖像描述

獲得這些值后,您可以使用.FindR2范圍內搜索product index ,然后使用.Offset輸入公式。

我不確定我是否理解了整個故事,但這是一個回歸的功能
多維數組可能如下所示:

Public Sub Main_Sub()

Dim vArray_R1()                     As Variant
Dim oRange                          As Range


Set oRange = ThisWorkbook.Sheets(1).Range("A1:B5")
vArray_R1 = Blending_function(oRange)
'You do the same for The second array.     

set oRange = nothing

End Sub

Public Function Blending_function(R1 As Range)

 Dim iRange_Cols As Integer
 Dim iRange_Rows As Integer


iRange_Cols = R1.Columns.Count
iRange_Rows = R1.Rows.Count

'Set size of the array (an existing array would be cleared)
ReDim vArray(1 To iRange_Rows, 1 To iRange_Cols)

vArray = R1
Blending_function = vArray

End Function

第二個選項可能是聲明函數返回一個布爾值,因為參數是標准發送的byRef; 你可以只在main sub中聲明范圍和數組,並在函數中同時轉換它們。 我不會選擇此選項,因為您之后無法重新使用該函數將其他范圍轉換為數組。

補充信息:這種技術有兩種方式。 您可以在之后定義范圍並執行:

set oRange = vArray

這是在Range與數組大小相同的條件下。

row = 2
column = "B"
Do While Len(Range(column & row).Formula) > 0
    ' repeat until first empty cell in column 'column'(user input)
    ' read (column, row) and (column+1, row) value
     Cells(row, column).Value
     Cells(row, column+1).value
    ' store in Array
Loop

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM