2017-10-16 111 views
1

我想寫一個if語句,但我需要從「矩陣」矩陣/數組中獲取一個值。我無法讓VBA將h等於「矩陣」中的時隙1,1的值。我已經打印了矩陣,以確保它正確地保存了值並且是。如何獲得循環外矩陣的值是在?從它創建的循環外創建一個矩陣的值

Option Explicit 
Option Base 1 
Private Sub CommandButton1_Click() 
Dim numberofcases As Single, r As Integer, i As Single, j As Integer, c As 
Single 
Dim matrix() As String, max As Single 
Dim loadmatrix() As String, loadtypes As Single, numbertypes As Single 
Dim STAADloadmatrix() As String, countrow As Single, countcolumn As Single 
Dim h as single 

max = ((Cells(Rows.count, "B").End(xlUp).Row)) 
loadtypes = ((Cells(Rows.count, "L").End(xlUp).Row)) 


j = 1 
c = 5 
i = 1 
ReDim matrix(i, j) 
For r = 2 To max 
    ReDim matrix(i, j) 
    j = 1 
    If Cells(r, c) = "" Then 
    ElseIf Cells(r, c) > 0 Then 
    matrix(i, j) = Cells(r, c) 
    j = 2 
    ReDim matrix(i, j) 
    matrix(i, j) = Cells(r, (c - 2)) 
    i = i + 1 
    End If 
Next r 

h = matrix(1,1) 

End sub 
+0

'ReDim'沒有「保存」會刪除所有數組內容。 http://www.excel-easy.com/vba/examples/dynamic-array.html –

回答

0

我認爲,如果你刪除一切將工作得很好不必要

REDIM矩陣(I,J)

內循環:

For r = 2 To max 
    j = 1 If Cells(r, c) = "" Then 
    ElseIf Cells(r, c) > 0 Then 
    matrix(i, j) = Cells(r, c) 
    j = 2 
    matrix(i, j) = Cells(r, (c - 2)) 
    i = i + 1 
    End If 
    Next r