【问题标题】:looping through range and copying to range循环遍历范围并复制到范围
【发布时间】:2015-10-17 21:52:39
【问题描述】:

我很难将行从一个循环复制到一个循环。目标循环是所有空白单元格。我已经坚持了3天了,我觉得很没有生产力。我错过了什么?

Sub Testloop()
Dim a As Range, b As Range, d As Range

Sheets("SAP Output DATA").Select
Range(Cells(2, "A"), Cells(Rows.Count, "A").End(xlUp)).Select
Selection.SpecialCells(xlCellTypeBlanks).Offset(0, 4).Select
Set d = Selection

Sheets("Input DATA").Select
Range("B2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Set a = Selection

For Each b In a.Rows
b.Copy
    For Each row In d.Rows
        b.PasteSpecial
    Next row
Next b

End Sub

它复制了数据,但它复制的数据是第 2 行的所有原始数据,下一个空白单元格不包含 Input DATA 表中的下一行数据。我如何遍历行并将它们粘贴到空白单元格中?

看图片:

1。 http://i.stack.imgur.com/Jd95G.png

2。 http://i.stack.imgur.com/444RO.png

经过漫长的一天,我仍然无法解决它。这是我认为永远不会得到它的接近。

Sub Testshttestonemoretime()
    Dim a As Range, b As Range, d As Range, f As Range
    Dim i As Long, r As Range, coltoSearch As String
    Dim sht As Worksheet

    Set sht = ThisWorkbook.ActiveSheet

    Sheets("Input DATA").Select
    Range("B2").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Set a = Selection

Sheets("SAP Output DATA").Select
For Each b In a.Rows
MsgBox b.Address

    For Each Address In b

    coltoSearch = "A"
    For i = 2 To Range(coltoSearch & Rows.Count).End(xlUp).row
    Set r = Range(coltoSearch & i)

        If Len(r.Value) = 0 Then
            MsgBox "No Value, in " & r.Address
            b.Copy Destination:=Cells(i, 5)
        End If

    Next i

    Next Address
Next b

End Sub

Excel工作表下载有问题:

https://drive.google.com/file/d/0B-ZY6BZH9zh5WGpuY0RPZk5Mb2c/view?usp=sharing

该按钮在 sap 数据表上称为“将文本复制到颜色”

到目前为止,我唯一能以一种有效的方式工作,主要是。我不知道为什么有时会失败;这是:

Sub WorkingLoop()
    Dim a As Range, b As Range, d As Range, f As Range, e As Range
    Dim i As Long, r As Range, coltoSearch As String
    Dim sht As Worksheet

    Set sht = ThisWorkbook.Worksheets("Input DATA")

    Sheets("Input DATA").Select
    Range("B2").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Set a = Selection

Sheets("SAP Output DATA").Select

For Each b In a.Rows
'MsgBox b.Address

Set f = sht.Range(b.Address)
f.Copy
    coltoSearch = "A"
    For i = 2 To Range(coltoSearch & Rows.Count).End(xlUp).row
    Set r = Range(coltoSearch & i)


        If Len(r.Value) = 0 Then
            'MsgBox "No Value, in " & r.Address
            Set e = Range(r.Address)

            For Each cell In e
            e.PasteSpecial
            Next cell
        End If

    Next i
e.PasteSpecial
Next b


End Sub

【问题讨论】:

  • 不太确定您要做什么。是不是:将“SAP 输出数据”第 2 行的所有数据复制到“输入数据”表的底部,然后对空白单元格执行(或不执行)某些操作?
  • 我删除了多余的代码。对不起,你是对的。我想将每个单独的行从一个范围复制到另一个范围,并且在目标范围(空白)中将按源范围的顺序填充。
  • 仍然不太明白 - 例如,“SAP 输出数据”上的数据涵盖第 1 到 15 行。“输入数据”表上有数据 - 数据涵盖第 1 到第 1 行3,然后有 3 个空白列,然后是几行数据,然后是空白行 - 您希望将“SAP 输出数据”中的数据粘贴到空白行 1 到 3,然后将其余部分粘贴到其他空白行中?如果是这种情况 - 首先对“输入数据”进行排序不是更好吗,这会将所有数据置于顶部,然后粘贴到其下方?
  • 有没有办法不使用选择复制和粘贴?

标签: vba loops range


【解决方案1】:

在使用最后一个循环之后,它所做的只是复制粘贴 1 行,然后只在所有行的末尾添加另一行,但是这会选择每个空行 Range("D2") 是定义的自定义范围工作表中的第一个起始空白行。这需要针对每个项目进行定义。然后它会进行正常循环。末尾的错误确定它是否到达末尾并向上翻页到第一个范围。抱歉,这是一个难题的简单解决方案。上面的答案是不正确的。我以为是。

Sub PasteinBlankCellsLoop()
Dim sht As Worksheet
Dim i As Long, lastrow As Long
Dim lColumn As Long

Set sht = ThisWorkbook.Sheets("Input DATA")

ThisWorkbook.Sheets("SAP Output DATA").Select
Range("D2").Select

With sht
On Error GoTo Beginning:
lColumn = sht.Cells(1, sht.Columns.Count).End(xlToLeft).Column
lastrow = sht.Cells(sht.Rows.Count, 1).End(xlUp).row

For i = 2 To lastrow
    For x = 1 To lColumn

        Range((sht.Cells(i, 1)), sht.Cells(i, sht.Columns.Count).End(xlToLeft)).Copy

        Selection.PasteSpecial
        Selection.Interior.ColorIndex = 17
        ActiveCell.Offset(1, 0).End(xlDown).Select
        ActiveCell.Offset(1, 0).Select
        Range(ActiveCell, ActiveCell.Offset(0, x)).Select

    Next x
Next i
End With

Beginning:
Range("A1").Select

End Sub

更新它以计算列范围..就是这样......这很完美......

【讨论】:

  • 我将尝试制作一个实际操作的屏幕视频。这是一部了不起的作品,我想一旦你们都看到它的作品,你就会意识到取得了什么成就。
【解决方案2】:

到目前为止,这是工作循环。

Sub Testshttestonemoretime()
'http://stackoverflow.com/questions/18875115/go-to-first-blank-row
'http://www.contextures.com/xlDataEntry02.html
'http://stackoverflow.com/questions/20805874/excel-vba-copy-and-paste-loop-within-loop
'http://stackoverflow.com/questions/1463236/loop-through-each-row-of-a-range-in-excel
'http://stackoverflow.com/questions/28202581/copy-and-paste-in-first-blank-row-loop


    Dim a As Range, b As Range, d As Range, f As Range, e As Range
    Dim i As Long, r As Range, coltoSearch As String
    Dim sht As Worksheet

    Set sht = ThisWorkbook.Worksheets("Input DATA")

    Sheets("Input DATA").Select
    Range("B2").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Set a = Selection

Sheets("SAP Output DATA").Select

For Each b In a.Rows
'MsgBox b.Address

Set f = sht.Range(b.Address)
f.Copy
    coltoSearch = "A"
    For i = 2 To Range(coltoSearch & Rows.Count).End(xlUp).row
    Set r = Range(coltoSearch & i)


        If Len(r.Value) = 0 Then

            'MsgBox "No Value, in " & r.Address
            'b.Copy Destination:=Cells(i, 5)
            Set e = Range(r.Address)
            'f.Copy Destination:=Cells(i, 5)
            'e.Cells(i, 5).Value = f.Value
            For Each cell In e
            e.PasteSpecial
            Selection.Interior.ColorIndex = 17
            Next cell

        End If

    Next i

'f.Copy Destination:=e
On Error GoTo ErrHandler
'e.Offset(0, 4).PasteSpecial
e.PasteSpecial
Selection.Interior.ColorIndex = 17
ErrHandler:
Next b

End Sub

循环是这样运行的:

Sub runallsubssap()
Dim shl
Set shl = CreateObject("WScript.Shell")

application.ScreenUpdating = False
Call Testshttestonemoretime
shl.Run "c:\temp\1000.vbs", 1, True
Call OffsetColoredCells
shl.Run "c:\temp\1000.vbs", 1, True
Call insertselection
shl.Run "c:\temp\1000.vbs", 1, True
Call Selecterange
shl.Run "c:\temp\1000.vbs", 1, True
Call ColorBlankCells
application.ScreenUpdating = True

End Sub

为了解决选择每个单元格并对其进行偏移的问题,我只是对其进行了插入。

Sub OffsetColoredCells()
Dim rngSrch As Range, C As Range

    Dim rCell As Range
    Dim lColor As Long
    Dim rColored As Range

Sheets("SAP Output DATA").Select
Range(Cells(2, "A"), Cells(Rows.Count, "E").End(xlUp)).Select

l7Color = RGB(153, 153, 255)

            Set rColored = Nothing
            For Each rCell In Selection
                If rCell.Interior.Color = l7Color Then
                    If rColored Is Nothing Then
                        Set rColored = rCell
                        'GoTo NextSheet1
                    Else
                       Set rColored = Union(rColored, rCell)
                    End If

                End If
            Next

            'If rColored Is Nothing Then
            '    MsgBox "Nothing is Selected"
            'Else

rColored.Select

End Sub

Sub insertselection()

    application.CutCopyMode = False
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

End Sub

Sub Selecterange()

    Range("E2").Select
    Call ColumnSelectAndSelect(4)
    Selection.Offset(0, 1).Select
    Selection.Delete Shift:=xlToLeft

End Sub

【讨论】:

    【解决方案3】:

    由于两张图片上的数据与复制和粘贴不匹配,因此仍然不太确定您要做什么。我已经编写了几个程序来展示如何排序和复制粘贴 - 无需选择。

    我建议查找有关“With”和“Cells”关键字的帮助,以便更好地理解代码。

    Sub CopyPaste()
    
        Dim wrkBkTarget As Workbook, wrkShtTarget As Worksheet
        Dim wrkBkSource As Workbook, wrkShtSource As Worksheet
        Dim rLastCellSrc As Range, rLastCellTgt As Range
    
        'Update these to reference your workbooks.
        'If both sheets are in the workbook containing this code then
        'you can remove these references - just set each worksheet reference to ThisWorkbook
        Set wrkBkTarget = ThisWorkbook
        Set wrkBkSource = ThisWorkbook
    
        'Update to reference your worksheets.
        Set wrkShtTarget = wrkBkTarget.Worksheets("Input DATA")
        Set wrkShtSource = wrkBkSource.Worksheets("SAP Output DATA")
    
        Set rLastCellTgt = LastCell(wrkShtTarget)
        Set rLastCellSrc = LastCell(wrkShtSource)
    
        'First sort the target sheet and find the new last cell.
        'Sorts on column A.
        With wrkShtTarget
            .Sort.SortFields.Add Key:=.Range(.Cells(1, 1), .Cells(rLastCellTgt.Row, 1)), Order:=xlAscending
            With .Sort
                .SetRange wrkShtTarget.Range(wrkShtTarget.Cells(1, 1), rLastCellTgt)
                .Header = xlYes
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
        End With
        'Find the new last cell - you need to paste on next row down.
        Set rLastCellTgt = LastCell(wrkShtTarget)
    
        'Now copy the new data to the bottom of the dataset.
        With wrkShtSource
            .Range(.Cells(2, 1), rLastCellSrc).Copy _
                Destination:=wrkShtTarget.Cells(rLastCellTgt.Row + 1, 1)
        End With
    
    End Sub
    
    
    'Returns a reference to the last cell on the sheet - useful in most projects.
    Public Function LastCell(wrkSht As Worksheet, Optional Col As Long = 0) As Range
    
        Dim lLastCol As Long, lLastRow As Long
    
        On Error Resume Next
    
        With wrkSht
            If Col = 0 Then
                lLastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
                lLastRow = .Cells.Find("*", , , , xlByRows, xlPrevious).Row
            Else
                lLastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
                lLastRow = .Columns(Col).Find("*", , , , xlByColumns, xlPrevious).Row
            End If
    
            If lLastCol = 0 Then lLastCol = 1
            If lLastRow = 0 Then lLastRow = 1
    
            Set LastCell = wrkSht.Cells(lLastRow, lLastCol)
        End With
        On Error GoTo 0
    
    End Function
    

    【讨论】:

    • 嘿,它真的把我的整张纸都弄乱了。我可以把我的 excel 表发给你,让你看一下吗?要确切地看到两难境地?
    • 我知道我应该更具体地说,该按钮在 sap 数据表上称为“将文本复制到颜色”
    猜你喜欢
    • 1970-01-01
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 2013-05-03
    • 2016-01-01
    • 1970-01-01
    相关资源
    最近更新 更多