【问题标题】:Excel formula only bring over row in other worksheet if cell in column A is not blank如果 A 列中的单元格不为空,Excel 公式仅在其他工作表中引入行
【发布时间】:2018-12-13 16:43:24
【问题描述】:

我在一个 Excel 工作簿中有两个工作表,如果 A 列中有数据,我只想获取单元格中有数据的行(从工作表 1 到工作表 2)。我在工作表 2 中的公式是 =IF('Raw Data'!A2<>"", 'Raw Data'!A2,),但如果没有如第 3 行和第 5 行所示的数据,我实际上根本不希望它带入行。现在它正在带入整行:

如果没有数据,您会看到它仍在将该行带入工作表 2。任何想法如何只引入数据行?

Sub DataInCell()

Dim rw As Long
rw = 2

' Select initial sheet to copy from
Sheets("Raw Data").Select


' Find the last row of data - xlUp will check from the bottom of the spreadsheet up.
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
' For loop through each row
For x = 2 To FinalRow

       If Cells(x, 1).Value <> 0 Then
        Range("A" & x & ":C" & x).Copy
       Sheets("Sheet1").Select
        NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1 'Continue incrementing through the rows.
        Cells(NextRow, 1).Select ' Find the next row.
        ActiveSheet.Cells(NextRow, "A").PasteSpecial xlPasteAll ' Paste information.
        Sheets("Raw Data").Select 'Reselect sheet to copy from. Probably uneccessary.
        End If

Next x
End Sub

【问题讨论】:

  • 您愿意接受VBA 解决方案吗?其他解决方案是仅过滤/复制粘贴可见单元格。或对目标列中的非空白进行过滤的数据透视表。为此使用公式似乎效率很低
  • 我对 VBA 解决方案持开放态度!我只是想可能有一个我缺少的简单公式解决方案。关于如何在 VBA 中执行此操作的任何想法?
  • 您希望将主工作表中的哪些列转移到新工作表中?整排?如果不是整行,请更新您的问题以显示原始列和目标列
  • 我用我在上面发布(编辑)的代码从 A 列引入单元格,但我需要从 A-C 引入单元格。你知道如何引用变量吗?
  • 我得到它来给我范围,见上文!

标签: excel excel-formula


【解决方案1】:

更新第 3 行和第 4 行的工作表名称后,您会看到代码延续了整行。如果需要部分范围,可以使用 Range(Cells, Cells) 进行修改。

Option Explicit

Sub Non_Blanks()

Dim ms As Worksheet: Set ms = ThisWorkbook.Sheets("Sheet1") '<-- Master Sheet
Dim ns As Worksheet: Set ns = ThisWorkbook.Sheets("Sheet2") '<-- New Sheet

Dim i As Long, MoveMe As Range, LR As Long

For i = 2 To ms.Range("B" & ms.Rows.Count).End(xlUp).Row
    If ms.Range("A" & i) = "*" Then
        If Not MoveMe Is Nothing Then
            Set MoveMe = Union(MoveMe, ms.Range("A" & i))
        Else
            Set MoveMe = ms.Range("A" & i)
        End If
    End If
Next i

If Not MoveMe Is Nothing Then
    LR = ns.Range("A" & ns.Rows.Count).End(xlUp).Offset(1).Row
    MoveMe.EntireRow.Copy
    ns.Range("A" & LR).PasteSpecial xlPasteValuesAndNumberFormats
End If

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-02
    • 2013-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多