2013-06-28 102 views
0

嘗試在VBA for Excel中編寫宏以查看列表中每行數據的某個列中的值,並且如果該值爲「是」,那麼它將整行復制並粘貼到同一工作簿中的不同工作表上。我們來命名兩張表「數據」和「最終」。我想要引用工作表,因此在運行代碼時打開哪張工作表並不重要。我打算使用Do循環來循環查看一個數據表中的行,直到找到沒有更多的條目和if語句來檢查這些值。使用VBA代碼在工作簿中的工作表中複製和粘貼

我對如何從一張紙切換到下一張感到困惑。

如何特別參考不同工作表中的單元格?

這裏是僞代碼我腦子裏想的:

Do while DataCells(x,1).Value <> " " 
    for each DataCells(x,1).Value="NO" 
     if DataCells(x,2).Value > DataCells(x,3).Value or _ 
     DataCells(x,4).Value < DataCells(x,5).Value 
      'Copy and paste/insert row x from Data to Final sheet adding a new 
      'row for each qualifying row 
     else 
      x=x+1 
     end 
    else if DataCells(x,1).Value="YES" 
Loop 
'copy and paste entire row to a third sheet 
'continue this cycle until all rows in the data sheet are examined 
+0

你需要更清楚地格式化代碼;就像現在這樣,由於缺少行或縮進,代碼不可讀。 – LittleBobbyTables

+0

請使用搜索功能。 – user2140261

回答

0
Sub FilterAndCopy() 

Application.ScreenUpdating = False 
Application.EnableEvents = False 
Application.Calculation = xlCalculationManual 

Dim sh As Worksheet, sh2 As Worksheet 
Dim lastrow1 As Long 
Dim lastcolumn1 As Long 



Set sh = ThisWorkbook.Sheets("Data") 
Set sh2 = ThisWorkbook.Sheets("Final") 

lastrow1 = sh.Cells(Rows.Count, "A").End(xlUp).Row ' Replace "A" With column that has the most Rows 
lastcolumn1 = sh.Cells(1, Columns.Count).End(xlToLeft).Column 

With sh.Range(.Cells(1, 1), .Cells(lastrow1, lastcolumn1)) 

'Replace the number in the field section with your Columns number 
    .AutoFilter , _ 
     Field:=1, _ 
     Criteria1:="yes" 

    .Copy sh2.Range("A1") 

End With 

Application.ScreenUpdating = True 
Application.EnableEvents = True 
Application.Calculation = xlCalculationAutomatic 

End Sub 
相關問題