vba excel特殊范围粘贴

c9qzyr3d  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(171)

我的问题是有vba excel命令,将允许粘贴到特定的单元格,如果这些特定的单元格已满,找到下一个特定的单元格是avabile粘贴的数据?
例如
单元格区域A1:E5是我可以粘贴数据的特定单元格区域。但单元格区域A6:E11是我不能粘贴数据的单元格区域,需要跳转到下一个可用的单元格区域
提前感谢有相同问题的主题的答案或方向。
我希望你明白我想说的。
我没有代码。试图在互联网上找到鳕鱼或类似的东西,但似乎找不到类似的情况。

qgelzfjb

qgelzfjb1#

填空

Sub FillBlanks()
    
    Dim wb As Workbook: Set wb = ThisWorkbook
    Dim sws As Worksheet: Set sws = wb.Worksheets("Sheet1")
    Dim srg As Range: Set srg = sws.Range("A1").CurrentRegion
    
    Dim dws As Worksheet: Set dws = wb.Worksheets("Sheet2")
    Dim drg As Range: Set drg = dws.Columns("A:C")
    Dim dcCount As Long: dcCount = drg.Columns.Count
    
    Dim dr As Long: dr = 1
    Dim dc As Long: dc = 1

    Dim sCell As Range
    Dim dCell As Range
    
    For Each sCell In srg.Cells
        If Len(CStr(sCell.Value)) > 0 Then ' the source cell is not blank
            Do
                Set dCell = drg.Cells(dr, dc) ' current destination cell
                
                ' Determine the next destination row and column.
                If dc < dcCount Then dc = dc + 1 Else dc = 1: dr = dr + 1
                
                If Len(CStr(dCell.Value)) = 0 Then ' destination cell is blank
                    sCell.Copy dCell ' copy; more illustrative
                    'dCell.Value = sCell.Value ' write values; more efficient
                    Exit Do
                'Else ' the destination cell is not blank; do nothing
                End If
            Loop
        'Else ' the source cell is blank; do nothing
        End If
    Next sCell

End Sub

相关问题