2016-06-09 205 views
0

希望你做得很好。我將在前言中說我不是程序員,我確信我已經開始的代碼充滿了更多的錯誤,然後我認爲。希望你能幫助:D。Excel VBA偏移複製粘貼

我有被從出來這樣的其他程序生成的Excel表:

excel sheet

然而,這種紙的大小可隨每一代新此表從其他程序更改。 (例如,下一次A可以有7個,D可以有9個)並且由於我只需要特定時間的特定信息組,因此不能很容易地使用該表來完成所需的數學運算,在這個例子中,組B和只有D。

什麼,我希望創造的東西,將採取該紙張,作爲其生成,並把它變成的東西,看起來像這樣:

result sheet

這是我寫這樣的代碼很遠,但因爲我不知道自己在做什麼,所以我一直在遇到很多問題。任何幫助,將不勝感激。

Option Explicit 
Sub Numbers() 
Dim matchesFound As Integer 
Dim row As Integer 
Dim c As Integer 
Dim copyRow As Integer 
Dim copyLocationColumn As Integer 

Dim arr(2) As String 
arr(0) = "1" 
arr(1) = "2" 
arr(2) = "3" 

Function arrayContainsValue(array, varValue) 
    found = false 
    for each = 0 to array 
    if array(i) = varValue then  
     found = true 
     exit for  
    arrayContainsValue = found 
End Function 

row = 1 
c = 1 
copyLocationColumn = 1 
copyRow = 1 

matchesFound = 0 
Do While matchesFound < 3 
    if arrayContainsValue(arr, ThisWorkbook.Sheets("Data").Cell(column,row)) 

    matchesFound = matchesFound + 1 
    Do While ThisWorkbook.Sheets("Data").Cell(column, row) 
     ThisWorkbook.Sheets("postHere").Cell(copyLocationColumn, copyRow) = _ 
         ThisWorkbook.Sheets("postHere").Cell(c + 1, row) 
     copyRow = copyRow+1 
     row = row + 1 
    Loop 
    End If 
row = row + 1 
Loop 

End Sub 

回答

1

有一條註釋許多邏輯錯誤且具,Excel中突出顯示它們自動我會做一個總結解釋一下:
1.功能不能「在中間」的分,光潔度Sub(從子函數中取出並粘貼,直到它說明結束爲止)
2.array是一個禁止的名字,用另一個變量名嘗試
3.For每個= 0?到數組?你試圖表示什麼對於每一個都必須是元素對於Array中的每個元素例如For和To是用數字定義的東西(對於counter = 1到15)

Function arrayContainsValue(***array***, varValue) '2nd problem  
found = false 
    for each = 0 to array '3rd problem 
    if array(i) = varValue then  
     found = true 
     exit for  
    arrayContainsValue = found 
End Function 

.... 4.你錯過了一個,然後在年底

if arrayContainsValue(arr, ThisWorkbook.Sheets("Data").Cell(column,row)) 

我沒有得到有關如何涉及到這個問題的編碼邏輯陳述(?)