简体   繁体   English

是否有R函数来查找特定值并将其粘贴到右侧的下一个单元格(下一列相同的行号)

[英]Is there an R function for finding a specific value and pasting it to the next cell to the right(next column same row number)

I have a report that contains values in two columns (B:C), when column B has a value, column C has an empty cell for the same row. 我有一个报告,其中包含两列(B:C)中的值,当B列具有值时,C列具有用于同一行的空单元格。

What I want to achieve is to create a macro to search for a specific value (ie "Desktop") in the column A and if it matches the search, copy/cut the value and paste it one cell to the next column but in the same row (that is meant to be blank), so all the values are aligned in one column (ie "Desktop" found in A1, then paste it to B1 without creating a new column). 我要实现的是创建一个宏以在A列中搜索特定值(即“桌面”),如果它与搜索匹配,请复制/剪切该值并将其粘贴到下一个列中的一个单元格中同一行(即为空白),因此所有值都在一列中对齐(即在A1中找到“桌面”,然后将其粘贴到B1中而不创建新列)。

Example: 例:

在此处输入图片说明

3 ways to get the job done. 完成工作的3种方法。

Dim cel As Range

    'This will search for "Desktop" and copy it to the cell on the right.
    For Each cel In Range("B2", Range("B" & Rows.Count).End(xlUp))
        If cel.Value = "Desktop" Then
            cel.Offset(, 1).Value = cel.Value
            'The line below will remove "Desktop" from the cell if required
            'cel.ClearContents
        End If
    Next cel

    'Or

    'This will search for "Desktop", insert a cell and shift to the right.
    For Each cel In Range("B2", Range("B" & Rows.Count).End(xlUp))
        If cel.Value = "Desktop" Then
            cel.Insert Shift:=xlToRight
        End If
    Next cel

    'Or

    'This will search for any empty cell in Col C and copy the value from Col B.
    For Each cel In Range("C2", Cells(Rows.Count, 3).End(xlUp))
        If cel.Value = "" Then cel.Value = cel.Offset(, -1).Value

        'The line below will remove "Desktop" from the cell if required
        'cel.Offset(, -1).ClearContents
    Next cel

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将单元格复制并粘贴到列中的下一个空行中 - Copying and pasting a cell into the next empty row in a column 在特定列中查找函数下一个值 - Finding function next value in a specific column 如果单元格具有值,则复制到下一列同一行的VBA代码 - If cell has value copy to the next column same row vba code 复制一行中的特定单元格一定次数,然后移动到行中的下一个单元格并在 VBA 中复制相同的次数 - Copying a specific cell in a row certain number of times and then moving to the next cell in the row and copying it the same number of times in VBA VBA:查找值,复制行,将行粘贴到其他地方,查找下一个值 - VBA: Finding a value, copying row, pasting row somewhere else, Finding the next value 如果该行的一个单元格中的值在下一行的同一列中重复,则删除该行 - Remove row if value in one cell of the row is duplicated in the same column in the next row 查找特定值的下一个实例的行号 - Find row number of next instance of specific value 粘贴到下一个空行 - Pasting into next empty row 如何粘贴到下一个空单元格而不是粘贴到同一单元格 - How to paste at the next empty cell instead of pasting at the same cell 当将某些内容粘贴到工作表 1 的单元格中时,如何自动触发在工作表 2 的下一行中粘贴值 - How to automatically trigger pasting a value in the next row on worksheet 2 when something is pasted into a cell on worksheet 1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM