簡體   English   中英

Excel VBA偏移復制粘貼

[英]Excel VBA offset Copy Paste

希望你做得很好。 我將通過說我不是一名程序員來作為開頭,我敢肯定我啟動的代碼到處都是比我想象的更多的錯誤。 希望你能幫助:D。

我有一個從另一個程序生成的Excel工作表,如下所示:

Excel工作表

但是,此表的大小可以隨着其他程序中該表的每個新一代而改變。 (例如,A下一次可以有7,而D可以有9)而且由於工作表本身很難在給定的時間內使用特定的信息組(因此在此示例中是B和僅D。

我希望創建的東西可以將工作表作為其生成的內容,並將其轉換為如下所示的內容:

結果表

這是我到目前為止編寫的代碼,但是由於我真的不知道我在做什么,所以不斷遇到很多問題。 任何幫助,將不勝感激。

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

在注釋中有很多邏輯錯誤需要列舉,Excel會自動突出顯示它們,我將做一個總結來解釋它們:
1. Function不能在sub的“中間”,完成Sub(從Sub取得Function並粘貼,直到說出sub sub)。
2.array是一個禁止的名稱,請嘗試使用另一個變量名稱
3.對於每個= 0? 排列? 你這樣想是什么意思? For Each必須是某物中的元素對於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)) 

我沒有關於與所述問題之間有什么聯系的編碼邏輯(?)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM