所以我的問題是這樣的。我有一個工作簿可以說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
sooo你在哪裏呢?你有什麼嘗試,你遇到什麼錯誤?沒有人會爲你編寫你的宏,但發佈你的代碼,並告訴我們你看到了什麼錯誤,我們將通過 – user1759942
幫助你是啊對不起我的壞..在一秒鐘內有原始代碼 – DeIta
嗯所以看起來像什麼即將發生?當我運行代碼時,它根本不會失敗。當細胞空白時它會卡住嗎?或者當他們是文字而不是數字?我在第14行至第28行的e列中粘貼了各種數字,並將它們全部循環。單元格被正確設置,它選擇了列e中的單元格和該列的左側(列d,如果您想選擇右側,使用'offset'中的正數),它複製了該選擇,它激活了sheet1(儘管我會使用'.activate'來代替),然後粘貼,然後返回到sheet2 – user1759942