2014-02-18 39 views
0

所以我的問題是這樣的。我有一個工作簿可以說2張。我已經從另一個程序和sheet1中自動創建了sheet2,我只希望sheet2中的一些信息。如果滿足某些條件,如何從另一張表中的每一行復制特定單元格?

我現在正在嘗試創建一個宏,它將檢查從14開始的每一行,E%大於15的值。如果滿足條件,我希望宏從C%和E%複製單元格值到sheet1讓我們在A5和B5中說,然後繼續到sheet2中的下一行,將值粘貼到A6 B6等等。

Sub Test() 
    Dim rng As Range 
    Dim lastRow As Long 
    Dim cell As Variant 


    With Sheets("Sheet2") 


     lastRow = .Range("E" & .Rows.Count).End(xlUp).Row 
     Set rng = .Range("E14:E" & lastRow) 


     For Each cell In rng 
      If cell.Value > 15 Then 
      'And here is where it gets bugged. I know theres something wrong with the .select but I couldnt think of any other way to 
      'pick only just the 2 cells needed. 
       Range(cell.Offset(0, -1), cell.Offset(0, 0)).Select 
       Selection.Copy 

       'In here there should also be some code to select where to place the copyed 
       'data but since it already got bugged couldnt really find a solution for 
       'it.. 
       Sheets("Sheet1").Select 
       ActiveSheet.Paste 
       Sheets("Sheet2").Select 

      End If 
     Next 
    End With 


End Sub 
+0

sooo你在哪裏呢?你有什麼嘗試,你遇到什麼錯誤?沒有人會爲你編寫你的宏,但發佈你的代碼,並告訴我們你看到了什麼錯誤,我們將通過 – user1759942

+0

幫助你是啊對不起我的壞..在一秒鐘內有原始代碼 – DeIta

+0

嗯所以看起來像什麼即將發生?當我運行代碼時,它根本不會失敗。當細胞空白時它會卡住嗎?或者當他們是文字而不是數字?我在第14行至第28行的e列中粘貼了各種數字,並將它們全部循環。單元格被正確設置,它選擇了列e中的單元格和該列的左側(列d,如果您想選擇右側,使用'offset'中的正數),它複製了該選擇,它激活了sheet1(儘管我會使用'.activate'來代替),然後粘貼,然後返回到sheet2 – user1759942

回答

0

所以我想我會把它放在一起:

Sub Test() 
Dim rng As Range 
Dim lastRow As Long 
Dim cell As Variant 
dim count as long 
count = 0 

With Sheets("Sheet2") 


    lastRow = .Range("E" & .Rows.Count).End(xlUp).Row 
    Set rng = .Range("E14:E" & lastRow) 


    For Each cell In rng 
     If cell.Value > 15 Then 
     'And here is where it gets bugged. I know theres something wrong with the   .select but I couldnt think of any other way to 
     'pick only just the 2 cells needed. 
      Range(cell.Offset(0, -1), cell.Offset(0, 0)).Select 
      Selection.Copy 
     'maybe use: Range(cell.Offset(0, -1), cell.Offset(0, 0)).copy 


      'In here there should also be some code to select where to place the copyed 
      'data but since it already got bugged couldnt really find a solution for 
      'it.. 
      Sheets("Sheet1").Activate 
      Range("A5", B5).offset(count, 0).PasteSpecial 'this will make it so that it starts in a5, and moves down a row each time 
      count = count + 1   'dont forget to increment count 

      Sheets("Sheet2").Activate 

     End If 
    Next 
End With 


End Sub 

,這就是有點粗糙的東西..

你可能包括一些錯誤處理,如:if not cell.value = "" then或也if not isNumeric(cell.value) then和那些一起將確保您只處理與數字非空白單元格。

+0

謝謝先生。稍微調整一下,我就完美了。很高興能有人幫助新鮮的大腦。礦是如此從今天過度,不能得到一個今天的想法和最後期限推3小時.. – DeIta

+0

高興地幫助,GL :) – user1759942

相關問題