Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel vba数据中的粘贴_Excel_Vba - Fatal编程技术网

Excel vba数据中的粘贴

Excel vba数据中的粘贴,excel,vba,Excel,Vba,我正在建立一张有酒店详细信息的表格,“D”栏有靠近PMH、SCGH、FSH的医院。我要做的是根据同一张表上的单元格值搜索列“D”。我有下面的代码,但它只会做我想要的,如果列“D”中的单元格是单条目,例如pmh。我需要能够搜索列“D”中的所有单元格中的任何文本实例 非常感谢您的帮助 `Option Explicit Sub finddata() Dim hospitalname As String Dim finalrow As Integer Dim i As Integer Sheets("

我正在建立一张有酒店详细信息的表格,“D”栏有靠近PMH、SCGH、FSH的医院。我要做的是根据同一张表上的单元格值搜索列“D”。我有下面的代码,但它只会做我想要的,如果列“D”中的单元格是单条目,例如pmh。我需要能够搜索列“D”中的所有单元格中的任何文本实例

非常感谢您的帮助

`Option Explicit
Sub finddata()
Dim hospitalname As String
Dim finalrow As Integer
Dim i As Integer

Sheets("Results").Range("A4:D100").ClearContents

Sheets("Main").Select
hospitalname = Sheets("Main").Range("g3").Value
finalrow = Sheets("Main").Range("A1000").End(xlUp).Row

For i = 2 To finalrow
    If Cells(i, 4) = hospitalname Then
    Range(Cells(i, 1), Cells(i, 4)).Copy
    Sheets("Results").Range("A4").End(xlUp).Offset(1, 0).PasteSpecial       xlPasteFormulasAndNumberFormats
    End If
Next i

Sheets("Main").Range("g3").Select

End Sub

`最简单的两种方法是

  • 使用类似于的
    操作符:

    If Cells(i, 4).Value Like "*" & hospitalname & "*" Then
    
    此方法的缺点是,例如,
    PMH
    的医院名称可能与另一个名称(如
    SPMH
    )匹配

  • 使用
    InStr
    功能:

    If Instr("," & Cells(i, 4).Value & ",", "," & hospitalname & ",") > 0 Then
    
    在这一行中,我用逗号“包装”正在查看的单元格和正在搜索的值,因此它最终搜索字符串(例如)
    ”,PMH,“
    ”,PMH,SCGH,FSH,
    InStr
    将返回出现匹配的字符位置,如果未找到匹配,则返回零。因此,测试
    >0
    就是测试是否发生了匹配