简体   繁体   English

如果某个范围中的单元格的值为“ x”,则将某个范围的单元格复制并粘贴到另一张工作表中

[英]If cell in a range has value “x” copy and paste a range of cells into a different sheet

I am not a programmer by trade but I am trying to automate a small portion of a report I use every day out of curiosity and self interest. 我不是行业程序员,但是出于好奇和自身利益,我试图使我每天使用的一小部分报告自动化。 Basically, we receive and manually enter contact information (name, e-mail, phone number, etc.) and mark select groups that a person is interested in joining. 基本上,我们接收并手动输入联系信息(姓名,电子邮件,电话号码等),并标记某人有兴趣加入的选定组。 We then copy and paste that contact information entered into a different sheet for each group. 然后,我们将输入的联系信息复制并粘贴到每个组的不同工作表中。

I want to have a macro that checks the specific columns for each interest group for a "x" and if it finds that value copy and paste the contact information collected to the specific interest groups worksheet. 我想要一个宏,用于检查每个兴趣组的特定列是否为“ x”,以及是否找到该值,然后将收集的联系信息粘贴到特定兴趣组工作表中。 People are able to select multiple interest groups and their contact information is added to each separate interest group spreadsheet. 人们可以选择多个兴趣组,并将他们的联系信息添加到每个单独的兴趣组电子表格中。

Report columns look as follows: 报告列如下所示:

Group 1 Group 2 Group 3 Name Organization Phone E-mail  Notes

Row Contact Information looks similar to:
  x  x John ABC Inc. 000-000-0000 john.smith@fake.com  Call me ASAP!

The macro checks the column I have marked interest in Group 1 in and if it finds "x" then it copies the full range to the Group 1 worksheet. 宏会检查我对第1组感兴趣的列,如果找到“ x”,则会将整个范围复制到第1组工作表。

I want it to be able to check multiple columns (ie Group 1, 2, 3) for "x" and then copy and paste the information to the right of those columns to the appropriate sheet for the group. 我希望它能够检查“ x”的多个列(即组1、2、3),然后将这些列右侧的信息复制并粘贴到该组的适当工作表中。 If they have interest in multiple groups, their contact info should be copied to each specific worksheet.\\ 如果他们对多个组感兴趣,则应将其联系信息复制到每个特定的工作表中。\\

Do I need to have separate counters for each group worksheet and is there a way to write a if then statement that checks for x in each of the columns and then runs the appropriate code to copy and paste into that group? 我是否需要为每个组工作表设置单独的计数器,是否可以编写if语句以检查每个列中的x,然后运行适当的代码来复制并粘贴到该组中?

Sub Update()

    Dim c As Range
    Dim j As Integer
    Dim Source As Worksheet
    Dim Target1 As Worksheet
    Dim Target2 As Worksheet
    Dim Target3 As Worksheet

    Set Source = ActiveWorkbook.Worksheets("Interest Group Tracking")
    Set Target1 = ActiveWorkbook.Worksheets("Group 1")

    j = 1   'Start copying to row 1 in target sheet
    For Each c In Source.Range("A1:A1000") 'not sure if there is a way to not set a limit for the range
        If c = "x" Then
            Source.Rows(c.Row).Copy Target1.Rows(j + 1)
            j = j + 1
            End If
    Next c

End Sub

No errors besides the occasional syntax but don't really know how to structure the loop for checking for each group. 除了偶尔的语法外,没有其他错误,但实际上并不知道如何构造用于检查每个组的循环。 I am continuing to research and test things I find and will update if I need to. 我将继续研究和测试发现的问题,并在需要时进行更新。

See if this helps... I've added comments in the code, but please feel free to ask any other questions: 看看是否有帮助...我已经在代码中添加了注释,但是请随时询问其他问题:

Sub Update()
    Dim wb As Workbook: Set wb = ThisWorkbook
    Dim Source As Worksheet: Set Source = wb.Worksheets("Interest Group Tracking")
    Dim Target As Worksheet

    Dim R As Long, C As Long, lRowSrc As Long, lRowDst As Long

    With Source
        lRowSrc = .Cells(.Rows.Count, 1).End(xlUp).Row 'get the last row in your source sheet

        For R = 1 To lRowSrc    'Loop through all rows in the source
            For C = 1 To 3      'Loop through the 3 columns in the source
                If .Cells(R, C) = "x" Then
                    Set Target = wb.Worksheets("Group " & C)    'Assuming all groups have the same names, Group 1, Group 2, etc
                    lRowDst = Target.Cells(Target.Rows.Count, 1).End(xlUp).Row + 1 'get last row + 1 in the target sheet
                    .Rows(R).Copy Target.Rows(lRowDst)
                End If
            Next C
        Next R
    End With

End Sub

EDIT: additional sample 编辑:其他示例

Sub Update()
    Dim wb As Workbook: Set wb = ThisWorkbook
    Dim Source As Worksheet: Set Source = wb.Worksheets("Interest Group Tracking")
    Dim Target As Worksheet
    Dim shNames() As String: shNames = Split("ABC Group,Voter Accesibility,Animal Rights Activism", ",") 'Add sheet names here in the order of the groups

    Dim R As Long, C As Long, lRowSrc As Long, lColSrc As Long, lRowDst As Long

    With Source
        lRowSrc = .Cells(.Rows.Count, 1).End(xlUp).Row 'get the last row in your source sheet

        For R = 1 To lRowSrc    'Loop through all rows in the source
            For C = 1 To 3      'Loop through the 3 columns in the source
                If .Cells(R, C) = "x" Then
                    Set Target = wb.Worksheets(shNames(C - 1)) 'shNames array starts at 0
                    lRowDst = Target.Cells(Target.Rows.Count, 1).End(xlUp).Row + 1 'get last row + 1 in the target sheet
                    Target.Range(Target.Cells(lRowDst, 1), Target.Cells(lRowDst, 10 - C + 1)) = .Range(.Cells(R, C), .Cells(R, 10)).Value 'allocate the values
                End If
            Next C
        Next R
    End With

End Sub

I changed the main piece of logic at the end but this should work. 最后,我更改了主要逻辑,但这应该可行。 Instead of copy and pasting, I just made the range in the group1 sheet equal to the row's range. 而不是复制和粘贴,我只是使group1工作表中的范围等于行的范围。 I also use the last used row. 我还使用最后使用的行。

Sub Update()
Dim c As Range
Dim j As Integer
Dim k As Integer
Dim Source As Worksheet
Dim Target1 As Worksheet
Dim Target2 As Worksheet
Dim Target3 As Worksheet
Dim curSheet As Worksheet
Dim lastRow, lastRow1, lastRow2, lastRow3, lastCol As Long
Dim group1, group2, group3, curGroup As Long

Set Source = ActiveWorkbook.Worksheets("Interest Group Tracking")
Set Target1 = ActiveWorkbook.Worksheets("Group 1")
Set Target2 = ActiveWorkbook.Worksheets("Group 2")
Set Target3 = ActiveWorkbook.Worksheets("Group 3")

j = 1   
group1 = 1
group2 = 1
group3 = 1
With Source
lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
lastRow1 = .Cells(.Rows.Count, 1).End(xlUp).Row
lastRow2 = .Cells(.Rows.Count, 2).End(xlUp).Row
lastRow3 = .Cells(.Rows.Count, 3).End(xlUp).Row

    If lastRow1 > lastRow2 And lastRow1 > lastRow3 Then
    lastRow = lastRow1
End If
If lastRow2 > lastRow1 And lastRow2 > lastRow3 Then
    lastRow = lastRow2
End If
If lastRow3 > lastRow1 And lastRow3 > lastRow2 Then
    lastRow = lastRow3
End If

For j = 1 To lastRow
    For k = 1 To 3
        If .Cells(j, k) = "x" Then
           Set curSheet = ActiveWorkbook.Sheets("Group" & " " & k)
           If k = 1 Then
                curGroup = group1
           ElseIf k = 2 Then
                curGroup = group2
           ElseIf k = 3 Then
                curGroup = group3
           Else
                GoTo line1
           End If
           curSheet.Range(curSheet.Cells(curGroup, 1), curSheet.Cells(curGroup, lastCol)).Value = .Range(.Cells(j, 1), .Cells(j, lastCol)).Value
        End If
           If k = 1 Then
                group1 = group1 + 1
           ElseIf k = 2 Then
                group2 = group2 + 1
           ElseIf k = 3 Then
                group3 = group3 + 1
           End If
 line1:
    Next k
Next j
End With
 End Sub

Another way of doing it. 另一种方式。

Option Explicit

Sub CopyData()

Dim srcWB As Workbook
Dim srcWS As Worksheet
Dim destWS As Worksheet
Dim CopyRange As Variant

Dim i As Long, j As Long
Dim srcLRow As Long, destLRow As Long
Dim LCol As Long

Set srcWB = ActiveWorkbook
Set srcWS = srcWB.ActiveSheet

srcLRow = srcWS.Cells(srcWS.Rows.Count, "A").End(xlUp).Row
'loop through column 1 to 3
For i = 1 To 3
    For j = 2 To srcLRow
        'loop through rows

            If srcWS.Cells(j, i).value = "x" Then
                Set destWS = srcWB.Sheets("Sheet" & i)
                destLRow = destWS.Cells(destWS.Rows.Count, "A").End(xlUp).Row
                LCol = srcWS.Cells(j, srcWS.Columns.Count).End(xlToLeft).Column 'if you need to grab last used column

                ' Copy data
                Set CopyRange = srcWS.Range(Cells(j, 1), Cells(j, LCol))
                    CopyRange.Copy
                ' paste data from one sht to another
                destWS.Cells(destLRow + 1, 1).PasteSpecial Paste:=xlPasteAll, Transpose:=False
                Application.CutCopyMode = False
            End If
    Next j
Next i
MsgBox "Process completed!", vbInformation
End Sub

暂无
暂无

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

相关问题 当第一个单元格上的值发生变化时,Excel VBA 代码复制一行中的一系列单元格并粘贴到不同的工作表但相同的行/范围 - Excel VBA code to copy a range of cells in a row and paste in a different sheet but same row/range, when the value on 1st cell changes Excel 根据单元格值在特定工作表上复制粘贴范围 - Excel copy-paste range on specific sheet according to cell value 在单元格区域中单击任何单元格时,将其复制并粘贴为值 - Copy and Paste as Value when any cell is clicked within a range of cells 从另一张纸上的单元格值复制同一张纸中范围的一部分的粘贴范围 - Copy Range From One Sheet Paste Part of Range In Same Sheet Based On Cell Value On Another Sheet 将单元格值复制到一系列单元格 - Copy cell value to a range of cells 从范围内的单元格复制值并将它们粘贴到范围内的随机单元格中 - Copy values from cells in range and paste them in random cell in range 如果范围内只有一个单元格有值,如何将单元格范围内的值复制到另一个单元格? - How to copy the value in a range of cells to another cell if only one cell in the range has a value? 复制范围工作表1粘贴到活动单元格工作表2中 - Copy Range Sheet1 Paste in Active Cell Sheet 2 如果区域中只有一个单元格具有值,则将单元格区域中的值复制到另一个单元格 - Copy the value in a range of cells to another cell if only one cell in the range has a value 复制范围表1粘贴到所选单元格表2中 - Copy range sheet1 paste in selected cell sheet 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM