2012-07-10 106 views
1

我在Excel中的一個二維表的範圍內。例如。VBA-需要創建一個函數,該函數作爲輸入

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變化(它可以變化)。我要創建一個函數,它的第一個任務是讀這個範圍(這將是一個用戶定義的輸入),然後存儲該數據轉換成2維陣列,使得我可以在第一列和所述使用數據(整數)字符串在第二列,適當。

第一列是所得到的產物索引,而第二列是混合產品在給定的比例,結合在一起以給出在第一列中的產品。

然後是另一個表:

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維數組。然後從該數組中查看輸出產品的索引,並在表2中找到該產品以更新庫存水平。

+0

根據你的情況,應該是第二張表中'updated stock'的值? – 2012-07-10 08:59:23

+0

@SiddharthRout喜歡在table1中,row1,限制代理是P1。因此P1的表2中的更新值是10-(10/.6),P3是50-(10/.6),P5是100 +(10/.6)。你如何繼續? – Rachit 2012-07-10 09:10:51

+0

好吧明白了......謝謝讓我通過其餘的問題:) – 2012-07-10 09:22:09

回答

3

下面是關於如何使用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 

快照

enter image description here

一旦你有了這些值,你可以使用.Find範圍R2來搜索product index,然後用.Offset輸入公式。

+0

@siddharath:非常感謝...感謝你的幫助..我看到你已經將範圍直接存儲到數組MyArr1中。那是因爲範圍R1僅包含2列?如果是這種情況,我如何將R2(3列)存儲在'MyArr2'中.. – Rachit 2012-07-11 05:22:30

+0

'是因爲範圍R1僅包含2列嗎?'不。這不是原因。要存儲3列的範圍,您可以使用相同的方式:)'MyArr2 = R2'但我沒有看到這裏的需要。就像我之前提到的那樣,您可以直接使用'.Find'來查找該範圍內的'product index'。看到這個鏈接如何使用'.Find' http://siddharthrout.wordpress.com/2011/07/14/find-and-findnext-in-excel-vba/ – 2012-07-11 08:36:03

+0

你的博客幫了我很多。我使用'.Find'來查找table2中產品索引的位置。問題是,我無法使用偏移方法更改表2中列3的值。那是因爲我正在使用一個函數來完成任務嗎?如果是這樣,你能否建議我一個更好的方法來解決它? (j).Offset(0,2).Value = F'其中blendprodcell(j)是表2中的第j個「混合產品」的位置 - 第1列,並且F是列3行j中要更新的值。 – Rachit 2012-07-13 06:51:48

0
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 
+0

謝謝你的努力。我想,我的問題之前並不清楚。爲了更好的理解,我編輯了我的問題。請通過它,讓我知道一個合適的方式繼續。再次感謝。 :) – Rachit 2012-07-10 08:46:51

+1

你正在要求的源代碼。我只能給點意見。 – sreehari 2012-07-10 09:32:34

1

我不知道如果我的理解整個故事,但是這是一個多麼函數返回
多維數組可能看起來像:

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 

的第二個選擇是申報該函數返回一個布爾值,因爲參數是由標準發送的;你可以在主子部分聲明範圍和數組,並在函數中同時將它們轉換。我不會選擇這個選項,因爲之後你無法再使用該函數將其他範圍轉換爲數組。

補充信息: 此技術是雙向的。在條件

set oRange = vArray 

這是該範圍的大小與數組一樣:你可以事後定義的範圍做。

+0

感謝您的幫助。我試過你的代碼,但它返回一個錯誤。另外,我編輯了這個問題,以便你能更好地理解它。建議我採取適當的方式繼續。再次感謝 – Rachit 2012-07-10 08:45:05

+0

您收到的錯誤是什麼?我已經測試了我自己的代碼,它的工作... – Trace 2012-07-10 08:47:14

+0

我不會做的是爲您編寫自定義代碼,我不會因爲我不完全理解您嘗試實現的目標。但是,如果您爲我爲您編寫的函數提供了一個範圍,那麼您將返回一個多維數組,其中包含該範圍提供的值。然後,您可以使用此數組以任何您希望的方式操縱值,並將其轉換回範圍(如果這是目的)。 – Trace 2012-07-10 08:51:09

相關問題