2010-07-28 81 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