2010-07-28 87 views
1

我是VBA編碼的新手,所以我希望你能幫我解決以下問題。有條件的選擇和粘貼excel

我正在尋找最好的方式來組織以下幾點:

從一個數據集我得到不同種類的文件(均具有一定的文檔類型)與他們的信息(例如,客戶名稱,地址,金額,增值稅......)。從這個文件中,我想選擇某些文件類型(例如DG,EG,SA,...),它們每次延遲並複製粘貼與這些項目有關的行。

例如數據我得到

客戶名稱發行總額稅收優惠憑證類型

25739484伯特01/01/2010 100 15%2%EG

現在我的問題是:

  1. 什麼是最簡單的方式來說明哪些文檔類型我想要選擇和粘貼數據。 (這個文件是重複使用的公司)。讓用戶把它們放在不同的單元格中?
  2. 根據用戶選擇的文檔類型,我如何讓宏選擇這些行並將它們複製到新文件中?

非常感謝!

Ellen

回答

0

請注意這是不完整的,我沒有完全測試它。我希望這有助於你開始。

Dim dt As String 
Dim ws As Worksheet 
Dim cnt As Long 
Dim done As Boolean 
Dim emptycount As Long 

'ask the user for the doc type 
dt = InputBox("Enter the doc type") 

'get the active sheet 
ws = ThisWorkbook.ActiveSheet 

If dt <> "" Then 
    'loop over rows 
    Do While Not done 
     cnt = cnt + 1 
     'compare the doc type column to the doc type they selected 
     If ws.Cells(cnt, 6) = dt Then 
      'copy the row here 
     End If 

     'keep track of "empty" rows, after 1000 emptys, exit the loop 
     If ws.Cells(cnt, 6) = "" Then emptycount = emptycount + 1 
     If emptycount = 1000 Then done = True 
    Loop 
End If