简体   繁体   English

宏基于单元格值将行移动到另一张表

[英]Macro to move rows to another sheet based on cell value

The below macros works for - I have a workbook with two sheets (Active and Archive). 以下宏适用于-我有一个包含两张纸的工作簿(活动和存档)。 And in Active sheet i have AB column that contains Active or Archive status. 在活动表中,我有AB列,其中包含活动或存档状态。 If its status Archive macros cuts and moves the row to the sheet Archive. 如果其状态Archive宏被剪切,并将该行移至工作表Archive。 This macros works perfect. 此宏工作完美。

Now i need to add some other sheets to excel and named them (New, Accepted, Rejected) and of course i add the same status to the column AB. 现在,我需要向Excel添加其他工作表并将其命名(新的,已接受,已拒绝),当然我也将相同的状态添加到AB列中。 Now i want macros to do the same if AB = Archive or New or Accepted or Rejected cut and move the row to the sheet named Archive or New or Accepted or Rejected. 现在,如果AB = Archive或New或Accepted或Rejected,我希望宏执行相同的操作,并将行移至名为Archive或New或Accepted或Rejected的工作表。 I tried it by myself but can't do it. 我自己尝试过,但是做不到。

Need ur help. 需要您的帮助。 Thanks in advance. 提前致谢。

Private Sub CommandButton1_Click()
    Dim x As Integer
    Dim y As Integer
    Dim i As Integer
    Dim shSource As Worksheet
    Dim shTarget1 As Worksheet

    Set shSource = ThisWorkbook.Sheets("Active")
    Set shTarget1 = ThisWorkbook.Sheets("Archive")

    If shTarget1.Cells(2, 28).Value = "" Then
        x = 2
    Else
        x = shTarget1.Cells(2, 28).CurrentRegion.Rows.Count + 1
    End If

    i = 2

    Do Until shSource.Cells(i, 28) = ""
        If shSource.Cells(i, 28).Value = "Archive" Then
            shSource.Rows(i).Copy
            shTarget1.Cells(x, 1).PasteSpecial Paste:=xlPasteValues
            shSource.Rows(i).Delete
            x = x + 1
            GoTo Line1
        End If
        i = i + 1
    Line1: Loop 
End Sub

You can set up multiple variables and choose the right ones in a select case. 您可以设置多个变量,并在选择的情况下选择合适的变量。 There is some repetition here that could get cleaned up with arrays. 这里有一些重复可以用数组清除。

    Sub CommandButton1_Click()

Dim x As Integer 'archive target counter
Dim y As Integer 'new target counter
Dim z As Integer 'accepted target counter
Dim w As Integer 'rejected target counter
'the above could be an array if we were trying to generalize

Dim i As Integer 'counts rows in original sheet
Dim shSource As Worksheet

Dim shTarget1 As Worksheet 'archive sheet
Dim shTarget2 As Worksheet 'new sheet
Dim shTarget3 As Worksheet 'accepted sheet
Dim shTarget4 As Worksheet 'rejected sheet
'these 4 could also be an array, as could their names, in which case some things become loops and the select case could be written out


Set shSource = ThisWorkbook.Sheets("Active")
Set shTarget1 = ThisWorkbook.Sheets("Archive")
Set shTarget2 = ThisWorkbook.Sheets("New")
Set shTarget3 = ThisWorkbook.Sheets("Accepted")
Set shTarget4 = ThisWorkbook.Sheets("Rejected")


If shTarget1.Cells(2, 28).Value = "" Then
x = 2
Else
x = shTarget1.Cells(2, 28).CurrentRegion.Rows.Count + 1
End If

If shTarget2.Cells(2, 28).Value = "" Then
y = 2
Else
y = shTarget2.Cells(2, 28).CurrentRegion.Rows.Count + 1
End If

If shTarget3.Cells(2, 28).Value = "" Then
z = 2
Else
z = shTarget3.Cells(2, 28).CurrentRegion.Rows.Count + 1
End If

If shTarget4.Cells(2, 28).Value = "" Then
w = 2
Else
w = shTarget4.Cells(2, 28).CurrentRegion.Rows.Count + 1
End If


i = 2

Do Until shSource.Cells(i, 28) = ""
Select Case shSource.Cells(i, 28).Value
    Case "Archive":
        shSource.Rows(i).Copy
        shTarget1.Cells(x, 1).PasteSpecial Paste:=xlPasteValues
        shSource.Rows(i).Delete
        x = x + 1
    Case "New":
        shSource.Rows(i).Copy
        shTarget2.Cells(y, 1).PasteSpecial Paste:=xlPasteValues
        shSource.Rows(i).Delete
        y = y + 1
    Case "Accepted":
        shSource.Rows(i).Copy
        shTarget3.Cells(z, 1).PasteSpecial Paste:=xlPasteValues
        shSource.Rows(i).Delete
        z = z + 1
    Case "Rejected":
        shSource.Rows(i).Copy
        shTarget4.Cells(w, 1).PasteSpecial Paste:=xlPasteValues
        shSource.Rows(i).Delete
        w = w + 1
    Case Else 'no cutting so move to next input line
        i = i + 1
End Select
Loop
End Sub

EDIT: Below is the array based version that repeats itself less. 编辑:下面是基于数组的版本,重复较少。 Also, I found I kept overwriting my top row in the target sheets, so I added 2 (not 1) to the target counters when I initialized them. 另外,我发现我一直覆盖目标表中的第一行,因此在初始化目标计数器时,我在目标计数器上添加了2(而不是1)。 If the original was working in your context, you may switch it back. 如果原始文档在您的上下文中有效,则可以将其切换回原位。

Sub CommandButton1_Click()
Dim TargetCounters(3) As Integer
Dim TargetNames(3) As String
TargetNames(0) = "Archive"
TargetNames(1) = "New"
TargetNames(2) = "Accepted"
TargetNames(3) = "Rejected"

Dim i As Integer 'counts rows in original sheet
Dim shSource As Worksheet

Dim shTargets(3) As Worksheet

Set shSource = ThisWorkbook.Sheets("Active")

For i = 0 To 3
    Set shTargets(i) = ThisWorkbook.Sheets(TargetNames(i))
    If shTargets(i).Cells(2, 28).Value = "" Then
        TargetCounters(i) = 2
    Else 'there is stuff. Imagine for example it is in rows 2 to 7. Count will be 6. We need to start pasting in row 8
        TargetCounters(i) = shTargets(i).Cells(2, 28).CurrentRegion.Rows.Count + 2 'changed this from orinal + 1
    End If
    Next i

    i = 2
    Dim MatchIndex As Integer

    Do Until shSource.Cells(i, 28).Value = ""
'you could switch this case to a call on the application's match function against TargetNames
'if you take care with the case where it is not found and indexing being right and not off by 1
            Select Case shSource.Cells(i, 28).Value
            Case "Archive":
                MatchIndex = 0
            Case "New":
                MatchIndex = 1
            Case "Accepted":
                MatchIndex = 2
            Case "Rejected":
                MatchIndex = 3
            Case Else 'no cutting so set signal and we will move to next input line
                MatchIndex = -1
            End Select
            If (MatchIndex = -1) Then
                i = i + 1
            Else
                shSource.Rows(i).Copy
                shTargets(MatchIndex).Cells(TargetCounters(MatchIndex), 1).PasteSpecial Paste:=xlPasteValues
                shSource.Rows(i).Delete
                TargetCounters(MatchIndex) = TargetCounters(MatchIndex) + 1
            End If
        Loop
    End Sub

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

相关问题 运行宏以根据单元格值隐藏不同工作表中的行 - Running a Macro to hide rows in different sheet based on a cell value 根据不起作用的单元格条件将行移动到另一个工作表 - Move rows to another sheet based on a cell criteria not working 根据另一张纸上的单元格值隐藏行(包括循环) - Hide rows based on cell value on another sheet (including loop) Excel根据另一个工作表中的单元格值获取行 - Excel fetch rows based on a cell value in another sheet 根据单元格值将行从一个 Excel 工作表复制到另一个 - Copying rows from one Excel sheet to another based on cell value Excel宏可将数据从一个表移动到另一列,并基于列名和单元格内容 - Excel macro to move data from one sheet to another based column name and cell content 宏可根据不同工作表中的值自动填充单元格 - Macro to autofill a cell based on a value in a different sheet 根据工作表1中的单元格值隐藏工作表1和2中的行 - Hide rows in sheet 1 and 2 based on cell value in sheet 1 如何在不使用宏或VBA的情况下基于另一个工作表中的单元格值隐藏/取消隐藏工作表 - How to hide/un hide a sheet based on a cell value in another sheet without using macro or vba 使用宏根据另一个工作表中的相应单元格值突出显示工作表中的单元格 - Highlight cells in a sheet based on corresponding cell values in another sheet with a macro
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM