2016-12-09 20 views
1

我正在寫一個代碼,它必須從sheet3的表格中獲取數據並將數據傳輸到另一張紙上。有了這部分是一切OK。 同樣的點擊,我也希望該程序查看相同形式的數據sheet3,從Item's ID(它在列A中)從每個填充行,然後在sheet1(列C)找到匹配項。然後代碼查找匹配它識別匹配的行,並在單元格中進行減法(列I),即從表格sheet3獲取值。在列4中。然後在sheet1 - >匹配的行 - >列我使用sheet3的值減法 爲了清晰我附加了printscreen。需要更新另一張紙上的數據EXCEL

enter image description here

Option Explicit 

    Sub Button4_Click() 
    Dim x As Long 
    Dim erow As Long 
    Dim y as Long 
    Dim roww as Long 
    Dim matchess as Integer 
    Dim IDitem as Integer 
    Dim myrange as Long 

    'Calculate starting rows 
    x = 15 
    With Worksheets("sheet2") 
     erow = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Row 
    End With 
    myrange = Worksheets("sheet1").Cells.Range("C:I") 

    With Worksheets("sheet3") 
     Do While .Cells(x, 1) <> "" 

      'The next line copies values to Sheet2 
      Worksheets("sheet2").Range("A" & erow & ":Z" & erow).Value = .Range("A" & x & ":Z" & x).Value 

      'increment row counters 
      x = x + 1 
      erow = erow + 1 
     Loop 
    End With 
    y = 15 
    With Worksheets("sheet1") 
     Do While Worksheets("sheet3").Cells(y, 1) <> "" 
      matchess = Worksheets("sheet3").Cells(y, 1) 
      IDitem = Application.WorksheetFunction.VLookup(matchess, myrange, 4, False) 
      roww = Application.Match(matches, myrange, 0) 
      .cell(roww, 7).Value = .cell(roww, 7).Value - IDitem 
     y = y + 1 
     Loop 
    End With 

End Sub 

我不知道爲什麼,但代碼的第二部分不工作。

附加信息:表3:行從15開始,合併列A和B中的項目ID。合併列「S,T,U」中的金額; sheet1:列C中的項目ID,存儲在列I中;

專家幫我糾正代碼

+0

(a)聲明你的變量。 (b)使用'Option Explicit'確保你做(a)。 (c)你說「代碼的第二部分不起作用」,但不要告訴我們你的意思 - 所以我猜你正在'Set IDItem'行中得到一個錯誤13 Type Mismatch。如果是這樣,從該語句中刪除'Set' - VLookup不會返回一個對象,因此不宜將變量設置爲指向該(不存在的)對象。 – YowE3K

+0

不工作意味着點擊後不顯示任何錯誤,除傳輸外沒有任何事情發生。工作表1中的庫存不會減少。我聲明變量,但正如我所說沒有發生。 –

+0

(a)你的'While While Worksheets(「sheet3」).Cells(x,1)<>「」'語句是說「不要在本節中做任何事情」,因爲在到達該語句之前, x的值確保x行A列中的單元格爲「」。 (b)你能否在你的問題中包括你聲明你的變量的地方('myrange','matchess','matches','IDitem','roww'和其他我錯過的其他地方)。 – YowE3K

回答

0

我已經使用在過去的.Find方法來處理這樣的問題。我在代碼中包含了對循環的解釋,但我很樂意詳細說明您是否需要。

Sub Find_Method() 

    Dim LastRow_1 As Long 
    Dim LastRow_3 As Long 

    Dim Source_Item_ID_Range As Range 
    Dim Search_Item_ID_Range As Range 
    Dim Item_ID_Found_Cell As Range 
    Dim Cell As Range 

    LastRow_1 = Worksheets("Sheet1").Range("C" & Rows.Count).End(xlUp).Row 
    LastRow_3 = Worksheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Row 

    Set Search_Item_ID_Range = Worksheets("Sheet1").Range("C4:C" & LastRow_1) 
    Set Source_Item_ID_Range = Worksheets("Sheet3").Range("A15:A" & LastRow_3) 

    'For each Item ID cell in Sheet3, search for its 
    'string in the Item ID column of Sheet1. If a match 
    'is found, deduct 1 from the Warehouse Stock column 
    'and move on to the next cell. 

    For Each Cell In Source_Item_ID_Range 

     Set Item_ID_Found_Cell = Search_Item_ID_Range.Find(Cell.Value, LookIn:=xlValues) 

     If Not Item_ID_Found_Cell Is Nothing Then 

      Item_ID_Found_Cell.Offset(0, 6).Value = Item_ID_Found_Cell.Offset(0, 6).Value - Cell.Offset(0, 3).Value 

     End If 

    Next Cell 

End Sub 
+0

謝謝MJV。這段代碼已經在某處了。但有一些問題。我不知道爲什麼,但在'表' –

+0

謝謝MJV。這段代碼已經在某處了。但有一些問題。我不知道爲什麼,但是在第一個空行的'sheet1'中,點擊代碼後的第6列寫入了一些負數(例如我得到了-34)。我狡猾這是一些計算,但沒有抓住什麼和第二個。現在我需要添加一些變量,公式中不是減1,而是從sheet3列4中取值,並在sheet1中進行算術運算,即'Item_ID_Found_Cell.Offset(0,6).Value' - '*****。Value 。我試圖這樣做,但得到錯誤。你能幫助我解決這個問題嗎?最終應該如何看待代碼。 –

+0

我已經更改了代碼,因此表3中第4列的值從表1中減去,而不是1. – MJV

相關問題