简体   繁体   English

根据单元格值移动范围

[英]Move Range Based On Cell Value

I'm quite new to VBA and am working on a code to copy a range only if a cell value in the same row is "Completed". 我对VBA很陌生,只是在同一行中的单元格值为“已完成”时,我正在处理代码以复制范围。

The copied range is then pasted in another column and the original range is deleted. 然后将复制的范围粘贴到另一列中,并删除原始范围。

It would be great if it could loop as well so that the movement happens automatically when the cell value is changed to completed. 如果它可以循环也会很好,以便当单元格值更改为完成时,移动会自动发生。 My code so far is: 到目前为止我的代码是:

Sub Move()

    Dim r As Range, cell As Range, mynumber As Long

    Set r = Range("O1:O1000")

    mynumber = 1
    For Each cell In r
        If cell.Value = "Completed" Then
        Range("Q15:AE15").Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove

        If cell.Value = "Completed" Then
        ActiveCell.Select
        ActiveCell.Range("B:O").Select
        Selection.Copy
        Range("Q14").Select
        ActiveSheet.Paste

        End If

        Next

    End Sub

You need to use the built-in event Worksheet_Change : 您需要使用内置事件Worksheet_Change

In , on the left, double-click on the sheet you want this code to work. ,在左侧,双击要使此代码工作的工作表。 You'll access the sheet module, you have 2 lists just upon the text editor to select which event you want to use. 您将访问工作表模块,在文本编辑器上有2个列表,用于选择要使用的事件。

You can use this code there, it'll transfer the data of the 'Completed' line from B:O to Q:AE : 你可以在那里使用这个代码,它会将'Completed'行的数据从B:O转移到Q:AE:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub

If Not Application.Intersect(Me.Columns(15), Target) Is Nothing Then
    If Target.Value <> "Completed" Then
    Else
        Dim FirstFreeRowInColQ As Long
        FirstFreeRowInColQ = Me.Range("Q" & Me.Rows.Count).End(xlUp).Row + 1

        Me.Range("Q" & FirstFreeRowInColQ & ":AE" & FirstFreeRowInColQ).Value = _
            Me.Range("B" & Target.Row & ":O" & Target.Row).Value
    End If
Else
End If

End Sub

I used offset to move the data across and an insert "Delete" function to delete the original range. 我使用offset来移动数据并插入“Delete”函数来删除原始范围。 The offset created an un-bordered cell which I had to fix and I also cleared the "Completed" cell once it had been moved to the new range. 偏移创建了一个无边界的单元格,我必须修复它,并且一旦移动到新范围,我也清除了“已完成”单元格。

I'm still struggling with the loop but I'll continue trying. 我仍在努力解决这个问题,但我会继续努力。

Sub Move()

Dim r As Range, cell As Range, mynumber As Long

Set r = Range("O1:O1000")

mynumber = 1
For Each cell In r
    If cell.Value = "Completed" Then
    Range("Q14:AE14").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove

    End If

    If cell.Value = "Completed" Then
    cell.Select
    cell.Value = "Delete"
    Range(ActiveCell, ActiveCell.Offset(0, -14)).Select
    Selection.Copy
    Range("Q14").Select
    ActiveSheet.Paste

       With Selection.Borders(xlEdgeLeft)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
End With

    Range("AE14").ClearContents

    End If

    If cell.Value = "Delete" Then
    cell.Select
    Range(ActiveCell, ActiveCell.Offset(0, -14)).Select
    Selection.Delete Shift:=xlUp

    End If

    Next

End Sub

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM