search单元没有提到一个特定的string

我有一些代码,目前search列27提到27,并复制列27,同一行27列C中的单元格是否有办法修改此代码复制单元格的值时,27不在列答:这有道理吗?

示例工作表包含单元格(A1:A30)中的数字27和单元格(B1:B30)中的数字1; 除了在A5,在那里是28,在B5在哪里是2.我知道,在这种情况下,我可以search28,但这个代码的主要原因是,将有不止一个值可能是不同的,即26或22,以及30等

当我想到这个时,我认为我不得不改变路线If Not foundCel Is Nothing Then到不同的地方,但我可能会在这里吠叫错误的树。

 Option Explicit Sub Button1_Click() Dim v As Long v = 1 findStr = "27" Set foundCel = Range("A:A").Find(What:=findStr) If Not foundCel Is Nothing Then 'Yes' firstAddress = foundCel.Address Do Range("C" & v).Value = foundCel.Offset(0, 1).Value Set foundCel = Range("A:A").FindNext(foundCel) v = v + 1 Loop While Not foundCel Is Nothing And foundCel.Address <> firstAddress End If End Sub 

感谢您的任何帮助,您可以提供。

在这里你去,testing27不在列1(A)的string中,如果不是它复制列2(B)到列3(C):

 Private Sub Button1_Click() 'Searches all text in Column 2 on a Sheet for the string located in Column 1 Dim ThisWB As Workbook Dim ThisWS As Worksheet Dim i As Integer Dim Col1 As Double Dim Col2 As Double DIM Col3 As Double Dim Col1_rowSTART As Double Dim Col1_rowEND As Double Dim strTest As String 'Set up parameter that we know Set ThisWB = ActiveWorkbook Set ThisWS = ActiveSheet Col1 = 1 Col2 = 2 Col3 = 3 strTest = "27" 'Define Starting Row for each column Col1_rowSTART = 1 'Define ending row for each column Col1_rowEND = ThisWS.Cells(ThisWS.Rows.Count, Col1).End(xlUp).Row For i = Col1_rowSTART To Col1_rowEND 'make a string out of each cell value in Col1 'Check if 27 is NOT in the cell string being tested 'to test if it IS change to > 0 If InStr(CStr(ThisWS.Cells(i, Col1)), strTest) = 0 Then ThisWS.Cells(i, Col3).Value = ThisWS.Cells(i, Col2) End If Next i MsgBox ("27 Search Complete!") End Sub 

快速和肮脏。 干杯。 -WWC