如何通过表达式隐藏excel中的空行?

时间:2018-06-07 08:51:04

标签: excel vba excel-vba ms-office

我在excel中有2张(Sheet1,Sheet2) 的 Sheet 1中

Sheet1有2列A,B和6行数据。

Sheet2
Sheet2有1列A,单元格A1 = if(sheet1!a1="ali", a1,"") 通过增加行数来增加6行中的相同公式,就像在ExCel中一样。 公式运行良好,我只想要与表达式匹配的值而不是像我在代码段中显示的空白行,我该怎么做?

table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
}

tr:nth-child(even) {
    background-color: #dddddd;
}
<table>
  <tr>
    <th>Sheet1!A</th>
    <th>Sheet!B</th>
    <th>Sheet2!C</th>
  </tr>
  <tr>
    <td>Ali</td>
    <td>100</td>
    <td>100</td>
  </tr>
  <tr>
    <td>Irfan</td>
    <td>500</td>
    <td><i>i don't want this</i></td>
  </tr>
  <tr>
    <td>Irfan</td>
    <td>500</td>
    <td><i>i don't want this</i></td>
  </tr>
  <tr>
    <td>Sufyan</td>
    <td>1000</td>
    <td><i>i don't want this</i></td>
  </tr>
  <tr>
    <td>Owais</td>
    <td>900</td>
    <td><i>i don't want this</i></td>
  </tr>
  <tr>
    <td>ali</td>
    <td>600</td>
    <td>600</td>
  </tr>
</table>

1 个答案:

答案 0 :(得分:0)

为什么不使用自动过滤和复制?您的数据需要设置为一个表格,您可以通过选择一个填充的单元格并按 Ctrl + T ;确保你有标题。

Option Explicit
Public Sub test()

    With Worksheets("Sheet1").ListObjects("Table1").Range
    .AutoFilter
    .AutoFilter Field:=1, Criteria1:="Ali"
    .Range("Table1").Copy Worksheets("Sheet2").Range("A1")
    End With

End Sub

或者,如果复制到下一个可用行,请使用:

Option Explicit
Public Sub test()
    Dim wsSource As Worksheet, wsTarget As Worksheet

    Set wsSource = ThisWorkbook.Worksheets("Sheet1") '<==Change name appropriately
    Set wsTarget = ThisWorkbook.Worksheets("Sheet2")
    With wsSource.ListObjects("Table1").Range    '<==Change table name appropriately
        .AutoFilter
        .AutoFilter Field:=1, Criteria1:="Ali"
        .Range("Table1").Copy wsTarget.Range("A" & wsTarget.Cells(wsTarget.Rows.Count, "A").End(xlUp).Row + 1)
    End With
End Sub

过滤后和复制前的数据(带标题):

Data