Copy all cells with certain value into another column skipping blanks

社会主义新天地 提交于 2019-11-29 11:01:00

This will do the trick:

Sub RangeCopyPaste()
  Dim cell As Range
  Dim NewRange As Range
  Dim MyCount As Long
  MyCount = 1

  For Each cell In Worksheets("Sheet1").Range("B1:B30")
      If cell.Value = "YES" Then
          If MyCount = 1 Then Set NewRange = cell.Offset(0,-1)
          Set NewRange = Application.Union(NewRange, cell.Offset(0,-1))
          MyCount = MyCount + 1
      End If
  Next cell

  NewRange.Copy Destination:=activesheet.Range("D1")

End Sub
sertmo

Solution without VBA:

column C contains formulas like:

=COUNTIF(B$1:B1;"yes")

increase number in column C if this row has "yes" value in column B.
This value will by used in next step.

column D contains formulas like:

=INDEX(A:A;MATCH(ROW();C:C;0))

take value from:
table: an entire A row
row number: calculated by match function: find first occurance of row number (row number where we will place the value) in entire C column. 0 meens that we looking for exactly this number not an clossest.

to skip errors:

=IF(ISERROR(MATCH(ROW();C:C;0));"";INDEX(A:A;MATCH(ROW();C:C;0)))

easier can be writen:

=IFERROR(INDEX(A:A;MATCH(ROW();C:C;0));"")

and this means: write the value from rule if this value is not an error or write empty string if the rule is an error

Just used a Andcondition on your If to avoid the empty cells

  1. In C1, put then copy down =IF(AND(LEN(A1>0),B1="YES"),A1,NA()))
  2. Select column C
    • Press F5
    • Special ... check Formulas and then tick Errors (see pic)
    • Delete the selected cells, to leave you with a shorter list of desired names in column C

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!