【发布时间】: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