简体   繁体   English

使用空白中间单元格复制粘贴整行

[英]Copy paste entire row with blank intermediate cell

I have a file that I use as a phone book, In column A and B have surnames and names while in column C have the landline number and in D the mobile number.我有一个用作电话簿的文件,在 A 和 B 列中有姓氏和姓名,而在 C 列中有固定电话号码,在 D 中有手机号码。 Doing the search if the fixed number is not present in column C, it does not even show me the mobile phone, is it possible to correct this error?如果C列中没有固定号码进行搜索,它甚至不显示手机,是否可以更正此错误? Thank you谢谢

Set intervallo = Sheets(4).Range("A2", Sheets(4).Range("A1").End(xlDown)) ``
For Each Cognome In intervallo ``
If Cognome Like Sheets(1).Ricerca & "*" ``
Sheets(4).Range(Cognome, Cognome.End(xlToRight)).Copy ``
Sheets(1).Range("A" & (Rows.Count)).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues ``

For this solution I created a worksheet with a validation dropdown in cell B2 allowing the choice of "Name" or "Surname".对于此解决方案,我在单元格 B2 中创建了一个带有验证下拉列表的工作表,允许选择“姓名”或“姓氏”。 I also entered the text "Find Contact" in A2 as a caption to the cell A3 which is blank.我还在 A2 中输入了文本“查找联系人”作为空白单元格 A3 的标题。 All rows below A3 are also blank. A3 以下的所有行也是空白的。

Observe that the cells A3 and B2 are referred to in the code by name.请注意,代码中按名称引用了单元格 A3 和 B2。 You can transfer their functionality to any other cells on the same worksheet by changing the values of the constants in the code.您可以通过更改代码中常量的值将它们的功能转移到同一工作表上的任何其他单元格。 You can also change the number of columns there (4 for now, as you have specified) and the name of the worksheet containing your phone list ( Worksheets("Sheet4") for now) as well as the syntax by which it is specified.您还可以更改其中的列数(现在为 4,如您所指定)和包含电话列表的工作表的名称Worksheets("Sheet4")现在为Worksheets("Sheet4") )以及指定的语法。

The ActiveSheet is specified by the location of the code. ActiveSheet由代码的位置指定。 Paste it to the code sheet of the worksheet your code identifies as Sheets(1) .将其粘贴到您的代码标识为Sheets(1)的工作表的代码表中 This location is of critical importance.这个位置至关重要。 If you paste the code to a standard code module (with a name like Module1 ) it will lose its automation capabilities.如果您将代码粘贴到标准代码模块(名称类似于Module1 ),它将失去其自动化功能。

Properly installed, it will react to a change in A3 as well as B2.正确安装后,它将对 A3 和 B2 的变化做出反应。 Say, you have selected "Surname" in B2 and enter "Mike" in A3 the code will list all matches with their numbers in A3:D3 and down, to a maximum of 20 (which you can de- or increase in the code).假设您在 B2 中选择了“姓氏”并在 A3 中输入“迈克”,代码将列出所有匹配的数字在 A3:D3 及以下,最多 20 个(您可以在代码中取消或增加) . However, "Mike" isn't a surname.但是,“迈克”不是姓氏。 Therefore the code may return "No match found".因此,代码可能会返回“未找到匹配项”。 You can now change the search field selector in B2 to "Name".您现在可以将 B2 中的搜索字段选择器更改为“名称”。 That will cause the code to look for "Mike" in the other column and list all Mikes found.这将导致代码在另一列中查找“Mike”并列出找到的所有 Mike。 No need to enter the search criterium again.无需再次输入搜索条件。

Private Sub Worksheet_Change(ByVal Target As Range)
    ' 284
    
    Const TriggerAddress    As String = "A3"        ' change to suit
    Const LookUpSpec        As String = "B2"        ' which column to search: change to suit
    Const ClmCount          As Long = 4             ' 4 data columns ("A:D")
    
    Dim Ws                  As Worksheet            ' data source
    Dim LookUpClm           As Long
    Dim Rng                 As Range                ' working range
    Dim Fnd                 As Range                ' search result range
    Dim FirstFound          As Long
    Dim Output              As Variant              ' (maximum 20 rows)
    Dim i                   As Long                 ' index of Output
    Dim C                   As Long                 ' loop counter: columns
    
    With Target
        If .Address = Range(TriggerAddress).Address Or _
           .Address = Range(LookUpSpec).Address Then
           Set Target = Range(TriggerAddress)
        Else
            Exit Sub
        End If
    End With

    Set Ws = Worksheets("Sheet4")           ' change to suit
    LookUpClm = 2 + (StrComp(Range(LookUpSpec).Value, "name", vbTextCompare) = 0)
    ' change LookupSpec colour
    Range(LookUpSpec).Font.Color = Array(12611584, 3506772)(LookUpClm - 1)
    
    With Ws
        ' exclude row 1 (column headers)
        Set Rng = .Range(.Cells(2, LookUpClm), _
                         .Cells(.Rows.Count, LookUpClm).End(xlUp))
    End With
    ReDim Output(1 To ClmCount, 1 To 20)     ' maximum = 20: modify here
    Output(1, 1) = Target.Value
    Output(3, 1) = "No match found"
            
    Set Fnd = Rng.Find(What:=Target.Value, After:=Rng.Cells(Rng.Cells.Count), _
                       LookIn:=xlValues, LookAt:=xlPart, _
                       SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                       MatchCase:=False, SearchFormat:=False)
    If Not Fnd Is Nothing Then
        FirstFound = Fnd.Row
        Do
            ' collect all occurrences
            i = i + 1
            For C = 1 To ClmCount
                Output(C, i) = Ws.Cells(Fnd.Row, C).Value
            Next C
            
            Set Fnd = Rng.FindNext(Fnd)
            If Fnd Is Nothing Then Exit Do
        Loop While Fnd.Row > FirstFound
    End If
            
    Set Rng = Range(Target, Cells(Rows.Count, 1).End(xlUp))
    ' delete previous display
    If Rng.Row >= Target.Row Then Rng.Resize(, ClmCount).ClearContents

    If i = 0 Then i = 1
    ReDim Preserve Output(1 To ClmCount, 1 To i)
    
    ' prevent the next action from calling this procedure
    With Application
        .EnableEvents = False
        Target.Resize(UBound(Output, 2), UBound(Output)).Value = .Transpose(Output)
        .EnableEvents = True
    End With
End Sub

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

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