选择并复制特定单元格

时间:2015-12-14 06:23:45

标签: excel excel-vba vba

我有一张excel表格,我想根据它们的值选择一些单元格,然后使用VBA将这些单元格复制到另一张表格。

我有一大堆代码遍历所有excel表格并搜索特定值然后返回此单元格的总数。

  • 我现在需要立即复制H列中具有“name”&值的单元格。 “联系”并将所有这些值复制到同一工作簿中的sheet2。
  • 然后我复制名称旁边的单元格并联系。
  • 最终结果是一个新表,其中包含2列名称和联系人,每列下面包含属于它的每个名称和联系人的值

样本数据

sample picture of how the original sheet look like

sample picture of how the result sheet should become

扫描

Private Sub CommandButton1_Click()

row_number = 4
count_of_str = 0
Do
DoEvents
row_number = row_number + 1
item_in_review = Sheets("Sheet1").Range("H" & row_number)
    If InStr(item_in_review, "name") Then
      count_of_str = count_of_str + 1
    End If

Loop Until item_in_review = ""
MsgBox "the str occured: " & count_of_str & " times."
End Sub

2 个答案:

答案 0 :(得分:0)

未测试:

Private Sub CommandButton1_Click()
    Dim count_of_str As Long
    Dim c as Range, d As Range

    count_of_str = 0

    Set c = Sheets("Sheet1").Range("H4")  'cell to check
    Set d = Sheets("Sheet2").Range("A2")  'destination to copy to

    Do While Len(c.Value) > 0

        If InStr(c.Value, "name") > 0 Then
            count_of_str = count_of_str + 1
            c.Copy d
            Set d = d.Offset(1, 0)  'next destination row
        End If

        Set c = c.Offset(1, 0) 'next cell to check

    Loop 

    MsgBox "the str occured: " & count_of_str & " times."

End Sub

答案 1 :(得分:0)

利用Key Password / Find方法

我们还不完全清楚您的数据所在的列。我假设标签FindNextname位于contact,而实际数据位于{{1} }}

此外,我假设每个H都有一个I,并且没有包含任何检查。

name
相关问题