【问题标题】:VBA- Need to create a function, which takes the range as inputVBA-需要创建一个函数,将范围作为输入
【发布时间】:2012-07-10 06:47:44
【问题描述】:

我在 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 中找到该产品以更新库存水平。

【问题讨论】:

  • 第二张表中updated stock 的值应该是多少?
  • @SiddharthRout 就像在 table1 的 row1 中一样,限制代理是 P1。因此,P1 表 2 中的更新值为 10-(10/.6),P3 为 50 -(10/.6),P5 为 100 +(10/.6)。你如何进行?
  • 好的,知道了...谢谢,让我解决剩下的问题:)
  • okies...完成问题...如果您在理解问题时遇到任何困难,请告诉我。

标签: vba excel


【解决方案1】:

这是一个关于如何使用二维数组的示例。该函数将分解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 输入您的公式。

【讨论】:

  • @siddharath :非常感谢...感谢您的帮助.. 我看到您已将范围直接存储到数组MyArr1 中。那是因为范围 R1 仅包含 2 列吗?如果是这种情况,我如何将 R2(3 列)存储在 MyArr2 ..
  • Is that because the range R1 consists of 2 columns only?? 不,这不是原因。要存储 3 列的范围,您可以使用相同的方式 :) MyArr2 = R2 但我认为这里没有必要。就像我提到的,您可以直接使用.Find 在该范围内查找product index。请参阅此链接了解如何使用.Find siddharthrout.wordpress.com/2011/07/14/…
  • 你的博客对我帮助很大。我使用.Find在table2中找到产品索引的位置。问题是,我无法使用偏移方法更改表 2 中第 3 列的值。那是因为我正在使用一个函数来完成这个任务吗?如果是这样,你能建议我一个更好的方法来解决它吗???语法是(在函数中)blendprodcell(j).Offset(0, 2).Value = F 其中 blendprodcell(j) 是表 2 中第 j 个“混合产品”的位置——第 1 列,F 是第 3 列第 j 行中要更新的值。
【解决方案2】:

我不确定我是否理解了整个故事,但这就是返回的函数
多维数组可能如下所示:

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

第二个选项可能是声明函数返回一个布尔值,因为参数是由Ref发送的标准;您可以仅在主子中声明范围和数组,并在函数中同时转换它们。我不会选择此选项,因为之后您将无法重新使用该函数将其他范围转换为数组。

补充资料: 这种技术是双向的。之后您可以定义一个范围并执行以下操作:

set oRange = vArray

这是在 Range 与数组大小相同的条件下。

【讨论】:

  • 感谢您的帮助。我试过你的代码,但它返回一个错误。另外,我已经编辑了这个问题,以便您更好地理解它。请给我建议一个正确的方法。再次感谢
  • 我不会为您编写自定义代码,我不会这样做,因为我不完全理解您试图实现的目标。但是,如果您为我为您编写的函数提供一个范围,您将返回一个多维数组,其中包含该范围提供的值。然后,您可以使用此数组以您希望的任何方式操作值,并将其转换回一个范围,如果这是目的的话。
  • 对不起,它没有返回任何错误。复制粘贴错误。我已经编辑了这个问题。我希望现在已经很清楚了。
  • 那么您的问题解决了,还是您还有其他问题?
  • 谢谢 Kim,如果我需要进一步的帮助,会尽快回复您;)
【解决方案3】:
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

【讨论】:

  • 感谢您的努力。我想,我的问题之前并不清楚。我已经编辑了我的问题以便更好地理解。请通过它,让我知道一个合适的方式进行。再次感谢。 :)
猜你喜欢
  • 2019-12-01
  • 2021-06-04
  • 1970-01-01
  • 1970-01-01
  • 2022-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-12
相关资源
最近更新 更多