2016-09-20 29 views
-2

我试图找到如何当满足一定的条件在Excel中从一个工作表复制行到另一种解决方案。我尝试过在这里发布的其他宏/解决方案无济于事。复制一行到新的工作表时,遇到了标准

,我想复制到新的工作表中的数据行GG是文本“地通过了。”它是在一个叫做SchoolEast

片发现

我想它的时候这句话在列GG数据集中的小区发现整个行从A到GH复制。目标工作表称为PassedFC。

很想得到一些帮助。如果您需要更多信息,请告诉我。

+2

'我试过其他的宏/解决方案发布在这里没有用'请发布一个让你最近的并告诉我们当你运行代码时发生了什么错误。使用编辑将原始帖子中的代码发布。不要使用评论来发布代码。 –

回答

0

这是一个非常简单的VBA程序,但我完全能够理解是多么棘手的包装你的头周围这些东西,当你第一次开始了。下面我有一个未经测试的子程序,应该让你喜欢99%的方式。我提出了丰富的意见来帮助。

Sub copyWhenCriteriaIsMet() 
    Dim shFrom as worksheet, shTo as worksheet 
    Dim rngReadCell as Range, rngRead as Range 
    Dim intWriteRow as integer 


    'Change shFrom to the sheet name we are reading and shTo to the sheet we are writing to 
    set shFrom = Sheets("Sheet1") 
    set shTo = Sheets("Sheet2") 

    'Change this to fit the range we are reading 
    set rngRead = shFrom.Range("GG1:GG5000") 

    'Set the initial row to which we are writing the found data 
    'Inside the For loop below we will increase this by one whenever we write a new line 
    intWriteRow = 1 

    'Now loop through each cell in the range using a "For" loop 
    For Each rngReadCell in rngRead.Cells 
     'Everything happening between "For" and "Next" is done over and over again for each cell 
     'We can reference the Cell that is being read as it will be automatically stored in 
     'variable "rngReadCell" 

     'Use an "IF" statement to test if the value in rngReadCell says "Passed with flying colors" 
     If rngReadCell.Value = "Passed with flying colors" 'note this is case sensitive here in VBA 
      'Everything happening between "If" and "End IF" only happens if that conditions is true 

      'Copy the contents of rngReadCell's Row (between Column A and GH) to the other sheet 
      'There are a few ways to reference this A:GH range, I'm going to use "RANGE()" and concatenate 
      'since that's the easier to understand than .offset().resize() or .Row().Range() 
      shFrom.Range("A" & rngReadCell.row & ":GH" & rngReadCell.row).copy Destination:=shTo.Range("A" & intWriteRow) 

      'Now that we wrote that line out, lets increment the intWriteRow variable 
      intWriteRow = intWriteRow + 1 

     End IF 

     'Nothing else to do here, so this will loop back to the top of the For loop and test the next 
     'rngReadCell.Value 
    Next rngReadRow 
End Sub 
相关问题