如果单元格范围不是空白,则复制行

我想知道是否有人可以帮助我。

我有一个Excel(2003)电子表格,名为“input”,列B到N中的数据。我想能够做的是,如果在列B中的任何单元格中的文本,我想复制行但只有列'B''我'和'N',并粘贴到我的第二个电子表格称为'输出'在单元格参考B2。

如果可能的话,一旦粘贴了信息,如果“B”列中的单元格中有文本,我想在“输出”表单中的“E”列中添加“预定的站点”一词。

我一直在做这个手动,这需要相当一段时间。

我只是想知道是否有人可以告诉我,我怎么能自动化这个。

非常感谢

如果您的数据如下所示,并且您的文本条目不是公式,则此方法将非常快速,因为它利用SpecialCells来避免循环行

Sub MoveEM2() Dim ws1 As Worksheet Dim ws2 As Worksheet Dim rng1 As Range Set ws1 = Sheets("Input") Set ws2 = Sheets("Output") On Error Resume Next Set rng1 = ws1.Columns("B").SpecialCells(xlConstants) On Error GoTo 0 If rng1 Is Nothing Then Exit Sub Application.ScreenUpdating = False Set rng2 = ws2.[b2] rng1.Copy rng2 'copy column I to Output C2 rng1.Offset(0, 7).Copy rng2.Offset(0, 1) 'copy column N to Output d2 rng1.Offset(0, 12).Copy rng2.Offset(0, 2) rng2.Offset(0, 3).Resize(rng1.Cells.Count, 1) = "Scheduled Site" Application.ScreenUpdating = True End Sub 

在这里输入图像描述

[进一步查询更新]

 Sub MoveEM() Dim ws1 As Worksheet Dim ws2 As Worksheet Dim rng1 As Range Set ws1 = Sheets("Input") Set ws2 = Sheets("Output") On Error Resume Next Set rng1 = ws1.Range(ws1.[b4], ws1.Cells(Rows.Count, "B").End(xlUp)).SpecialCells(xlConstants) On Error GoTo 0 If rng1 Is Nothing Then Exit Sub Application.ScreenUpdating = False Set rng2 = ws2.[b2] rng1.Copy rng2.PasteSpecial xlPasteValues 'copy column I to Output C2 rng1.Offset(0, 7).Copy rng2.Offset(0, 1).PasteSpecial xlPasteValues 'copy column N to Output d2 rng1.Offset(0, 12).Copy rng2.Offset(0, 2).PasteSpecial xlPasteValues rng2.Offset(0, 3).Resize(rng1.Cells.Count, 1) = "Scheduled Site" Application.CutCopyMode = False Application.ScreenUpdating = True End Sub 

我正在寻找在Google Docs电子表格中做同样的macros所以macros,我设法做一些中频和VLOOKUPs。 这似乎有点复杂,也许有人有一个更有效的方法来做到这一点,但这应该没有macros的工作:

在input的左边,我创build了一个以0开始的列,并且每次列B都有数据时就增加:

 A1=0 A2=IF(ISBLANK(B2),A1,A1+1) A3=IF(ISBLANK(B3),A2,A2+1) ... 

所以第一张纸看起来像这样:

 0 1 data1 1 2 data2 3 data3 3 3 4 data4 

然后在输出表中,有一个简单递增值的列,并对包含该数字的第一行进行查找:

 A1=1 A2=2 ... 

 B1=VLOOKUP(A1,Sheet1!A:B,2,FALSE) B2=VLOOKUP(A2,Sheet1!A:B,2,FALSE) ... 

所以第二张表看起来像这样:

 1 data1 2 data2 3 data3 4 data4 

做另一个查找任何其他列,你想从第一张转移,然后隐藏其中的数字列。

IRHM,

以防万一,你知道如何处理这个是一个例子。 请记住,每个人都以不同的方式做事,所以这可能不是最快或最优雅的方式。

 Sub MoveData() Sheets("Output").Select 'Select the input sheet OutputRowCounter = Range("A65536").End(xlUp).Row + 1 'find the last used row in column A of the output sheet Sheets("Input").Select 'Select the input sheet InputMaxRow = Range("A65536").End(xlUp).Row 'find the last used row in column A of the input sheet For rowLoop = 2 To InputMaxRow 'loop through the file and copy data from columns BN to output AM If Cells(rowLoop, 2).Value <> "" Then 'if the current cell (changing row and fixed column B) has any data... For ColLoop = 2 To 14 'Loop through columns BN Worksheets("Output").Cells(OutputRowCounter, ColLoop - 1).Value = Cells(rowLoop, ColLoop).Value 'copy selected data Next ColLoop 'go to next column OutputRowCounter = OutputRowCounter + 1 'store the next row in the output sheet End If Next rowLoop End Sub 

这是另一种方法。 这将你的数据放在一个数组中,然后通过数组查看列B中具有值的行。这应该比单元格通过列/表格单元运行得快一点,但是差别可能仅在大数据集。

 Sub summarize() Dim sIn As Worksheet, sOut As Worksheet, rIn As Range, rOut As Range Dim inputdata() As Variant Dim tmpArr(1 To 3) As Variant Dim i As Long, outcount As Long Set sIn = Sheets("Input") Set sOut = Sheets("Output") Set rIn = sIn.UsedRange Set rOut = sOut.Range("B2:D2") 'Loads input data into an array for fast processing. inputdata = rIn.Value outcount = 0 'Reads data from inputdata Array and prints selected values from columns B, I, and N on Output sheet row by row. For i = 1 To UBound(inputdata, 1) If inputdata(i, 1) <> "" Then outcount = outcount + 1 tmpArr(1) = inputdata(i, 1) tmpArr(2) = inputdata(i, 8) tmpArr(3) = inputdata(i, 13) rOut.Offset(outcount - 1, 0).Value = tmpArr Erase tmpArr End If Next i Erase inputdata 'Add "Scheduled Site" to Column E of Output data. If sOut.Range("B2") <> "" Then sOut.Range("E2") = "Scheduled Site" sOut.Range("E2").AutoFill Destination:=sOut.Range("E2", sOut.Range("E2").Offset(outcount - 1, 0)) End If End Sub