2016-10-28 69 views
0

我想選定的單元格複製到另一個工作表,但我總是收到錯誤消息:或者參數錯誤的無效的屬性賦值如果範圍錯誤的參數個數或無效的屬性賦值

此代碼檢查「細胞(i,20)「小於或大於」細胞(i,4)「10%。如果不是,則刪除該行,如果是,則應將所選單元格複製到另一個從第48行開始的表格。

也許有人可以指出,我在這裏做錯了什麼?這裏是我的代碼看起來是這樣的:

Sub CopyHighLow() 
Sheets("ProductionHighLow").Select 
i = 2 
j = 48 
produced = 0 
While Cells(i, 1) <> "" Or Cells(i + 1, 1) <> "" 
    produced = Cells(i, 20) 
    ordered = Cells(i, 4) 
    If Cells(i, 20) > Cells(i, 4) * 0.9 And Cells(i, 20) < Cells(i, 4) * 1.1 Then 
    Cells(i, 22).Delete Shift:=xlUp 
    i = i - 1 
    Else 
    Range(Cells(i, 1), Cells(i, 2), Cells(i, 3), Cells(i, 4), Cells(i, 20)).Select 
    Selection.Copy Destination:=Sheets("Rytinis").Range(Cells(j, 1), Cells(j, 2), Cells(j, 3), Cells(j, 4), Cells(j, 5)) 
    j = j + 1 
    End If 
    i = i + 1 
Wend 
End Sub 

更新在這裏正在修改的版本:

Sub CopyHighLow() 
Sheets("ProductionHighLow").Select 
i = 2 
j = 48 
produced = 0 
While Cells(i, 1) <> "" Or Cells(i + 1, 1) <> "" 
     produced = Cells(i, 20) 
     ordered = Cells(i, 4) 
     If Cells(i, 20) > Cells(i, 4) * 0.9 And Cells(i, 20) < Cells(i, 4) * 1.1 Then 
      Cells(i, 22).Delete Shift:=xlUp 
      i = i - 1 
     Else 
      Set RangeUnionCopy = Union(Cells(i, 1), Cells(i, 2), Cells(i, 3), Cells(i, 4), Cells(i, 20)) 
      Set RangeUnionPaste = Union(Cells(j, 1), Cells(j, 2), Cells(j, 3), Cells(j, 4), Cells(j, 5)) 
      RangeUnionCopy.Copy Destination:=Sheets("Rytinis").Range(RangeUnionPaste.Address) 
      j = j + 1 
     End If 

i = i + 1 
Wend 
End Sub 
+0

它打破了哪條線?在錯誤出現時點擊調試,看看在vba編輯器中哪一行是暫停的 – Tom

+2

這是兩個調用的問題:'Range(Cells(),Cells(),Cells()...)'Range '方法最多隻能接受兩個參數(一個起始單元格和一個結束單元格) – tigeravatar

+0

聽起來好像你想使用''而不是'Range'來解決這個問題 – tigeravatar

回答

3

問題說明
在這條線上你的問題依靠

Range(Cells(j, 1), Cells(j, 2), Cells(j, 3), Cells(j, 4), Cells(j, 5)) 

範圍對象不能處理兩個以上的命名單元格(這種方式)。你可以直接在編譯器中看到它。在其official documentation


方法解決

enter image description here


更多信息:

我會用聯盟在此之前,像這樣:

Set RangeUnion = Union(Cells(i, 1), Cells(i, 2), Cells(i, 3), Cells(i, 4), Cells(i, 20)) 
RangeUnion.Copy Destination:=Sheets("Rytinis").Range(RangeUnion.Address) 

這應該什麼工作你正在瞄準。

1

使用聯盟更正代碼:

Sub CopyHighLow() 

Dim i, j, produced, ordered 

Sheets("ProductionHighLow").Select 
i = 2 
j = 48 
produced = 0 
While Cells(i, 1) <> "" Or Cells(i + 1, 1) <> "" 
     produced = Cells(i, 20) 
     ordered = Cells(i, 4) 
     If Cells(i, 20) > Cells(i, 4) * 0.9 And Cells(i, 20) < Cells(i, 4) * 1.1 Then 
      Cells(i, 22).Delete Shift:=xlUp 
      i = i - 1 
     Else 
      Union(Cells(i, 1), Cells(i, 2), Cells(i, 3), Cells(i, 4), Cells(i, 20)).Select 
      Selection.Copy Destination:=Sheets("Rytinis").Cells(j, 1) 
      j = j + 1 
     End If 

i = i + 1 
Wend 
End Sub 
0

你需要告訴它什麼表它直接複製。

Sub CopyHighLow() 
Sheets("ProductionHighLow").Select 
i = 2 
j = 48 
produced = 0 
While Cells(i, 1) <> "" Or Cells(i + 1, 1) <> "" 
     produced = Cells(i, 20) 
     ordered = Cells(i, 4) 
     If Cells(i, 20) > Cells(i, 4) * 0.9 And Cells(i, 20) < Cells(i, 4) * 1.1 Then 
      Cells(i, 22).Delete Shift:=xlUp 
      i = i - 1 
     Else 
      ActiveSheet.Range(Cells(i, 1), Cells(i, 2), Cells(i, 3), Cells(i, 4), Cells(i, 20)).Select 
      Selection.Copy Destination:=Sheets("Rytinis").Range(Cells(j, 1), Cells(j, 2), Cells(j, 3), Cells(j, 4), Cells(j, 5)) 
      j = j + 1 
     End If 

i = i + 1 
Wend 
End Sub 
相關問題