VBA-需要创建一个函数,该函数将范围作为输入
我在Excel中有一个二维表格。例如VBA-需要创建一个函数,该函数将范围作为输入,vba,excel,Vba,Excel,我在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之间变化(可以变化)。我必须创建一个函数,它的第一个任务是读取这个范围(这将是一个用
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中找到该产品以更新库存水平。我不确定是否理解了整个故事,但这是一个函数返回的内容
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
多维数组可能如下所示:
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子函数中声明范围和数组,并在函数中同时转换它们。我不会选择此选项,因为以后您将无法重新使用该函数将其他范围转换为数组
补充资料:
这种技术是双向的。之后,您可以定义一个范围并执行以下操作:
set oRange = vArray
这是在范围与数组大小相同的条件下进行的 我不确定我是否理解了整个故事,但这是一个返回的函数
多维数组可能如下所示:
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子函数中声明范围和数组,并在函数中同时转换它们。我不会选择此选项,因为以后您将无法重新使用该函数将其他范围转换为数组
补充资料:
这种技术是双向的。之后,您可以定义一个范围并执行以下操作:
set oRange = vArray
这是在范围与数组大小相同的条件下进行的 下面是一个关于如何使用2D数组的示例。该函数将分解
混合组合
,并提取所需的值,以便使用这些值
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
子样本()
变暗Rng1为范围,Rng2为范围
出错时继续下一步
设置Rng1=Application.InputBox(“请选择Table1范围”,类型:=8)
错误转到0
如果Rng1.Columns.Count为2,则
MsgBox“请选择一个2列宽的范围”
出口接头
如果结束
出错时继续下一步
设置Rng2=Application.InputBox(“请选择Table2范围”,类型:=8)
错误转到0
如果Rng2.Columns.Count为3,则
MsgBox“请选择一个3列宽的范围”
出口接头
如果结束
混合函数Rng1、Rng2
端接头
公共函数混合_函数(R1为范围,R2为范围)
Dim MyAr1作为变异体,MyAr2作为变异体
我想我会坚持多久
Dim blndCom作为字符串,OutputPrd作为字符串
Dim ArP1()作为字符串,ArP2()作为字符串,tmpAr()作为字符串
MyAr1=R1
对于i=2到UBound(MyAr1,1)
OutputPrd=MyAr1(i,1)
blndCom=MyAr1(i,2)
tmpAr=拆分(blndCom,“/”)
ArP1=拆分(tmpAr(0),“:”)
ArP2=拆分(tmpAr(1),“:”)
调试.打印输出PRD
调试。打印修剪(ArP1(0))
调试。打印ArP1(1)
调试。打印ArP2(0)
调试。打印ArP2(1)
调试。打印“----”
下一个
端函数
快照
获得这些值后,可以使用
.Find
在R2
范围内搜索产品索引
,然后使用.Offset
输入公式。下面是一个关于如何使用2D数组的示例。该函数将分解混合组合
,并提取所需的值,以便使用这些值
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
子样本()
变暗Rng1为范围,Rng2为范围
出错时继续下一步
设置Rng1=Application.InputBox(“请选择Table1范围”,类型:=8)
错误转到0
如果Rng1.Columns.Count为2,则
MsgBox“请选择一个2列宽的范围”
出口接头
如果结束
出错时继续下一步
设置Rng2=Application.InputBox(“请选择Table2范围”,类型:=8)
错误转到0
如果Rng2.Columns.Count为3,则
MsgBox“请选择一个3列宽的范围”
出口接头
如果结束
混合函数Rng1、Rng2
端接头
公共函数混合_函数(R1为范围,R2为范围)
Dim MyAr1作为变异体,MyAr2作为变异体
我想我会坚持多久
Dim blndCom作为字符串,OutputPrd作为字符串
Dim ArP1()作为字符串,ArP2()作为字符串,tmpAr()作为字符串
MyAr1=R1
对于i=2到UBound(MyAr1,1)
OutputPrd=MyAr1(i,1)
blndCom=MyAr1(i,2)
tmpAr=拆分(blndCom,“/”)
ArP1=拆分(tmpAr(0),“:”)
ArP2=拆分(tmpAr(1),“:”)
调试.打印输出PRD
调试。打印修剪(ArP1(0))
调试。打印ArP1(1)
调试。打印ArP2(0)
调试。打印ArP2(1)
调试。打印“----”
下一个
端函数
快照
获得这些值后,您可以使用.Find
在R2
范围内搜索产品索引
,然后使用.Offset
输入公式。感谢您的帮助