我一直在使用一些打开工作簿并根据引用单元格查找值的代码。它搜索一个字符串并将其旁边的单元格。不幸的是,我现在在工作簿中有2个单元格'包含'参考字符串。一个单元格是“当前分数”,另一个单元格是“当前分数的百分比”。有没有办法说明我只想要“当前得分”而不是单元格中的任何其他内容。
很抱歉,如果这有点罗嗦,我可以在必要时提供代码
编辑:以下是代码:
Sub Future_Score()
Dim r
Dim findValues() As String
Dim Wrbk As Workbook
Dim This As Workbook
Dim sht As Worksheet
Dim i
Dim tmp
Dim counter
Dim c As Range
Dim firstAddress
Dim rng As Range
ReDim findValues(1 To 3)
findValues(1) = "Curren" & "*" & "Core"
findValues(2) = "dummyvariable1"
findValues(3) = "dummyvariable2"
counter = 0
r = Range("A163").End(xlDown).Row
Set rng = Range(Cells(163, 1), Cells(r, 1))
Set This = ThisWorkbook
For Each tmp In rng
Workbooks.Open tmp
Set Wrbk = ActiveWorkbook
'For Each sht In Wrbk.Worksheets
Set sht = ActiveSheet
For i = 1 To 3
With sht.Range(Cells(1, 1), Range("A1").SpecialCells(xlCellTypeLastCell))
Set c = .Find(findValues(i), LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Offset(0, 1).Value
secondAddress = c.Offset(0, 3).Value
thirdAddress = c.Offset(0, 4).Value
Do
This.Activate
tmp.Offset(0, 4).Value = firstAddress
Set c = .FindNext(c)
counter = counter + 1
Loop While Not c Is Nothing And c.Value = firstAddress
End If
End With
Wrbk.Activate
Next
'Wrbk.Activate
'Next sht
Wrbk.Close
Next tmp
End Sub
稍微使用了代码。虚拟变量将用于其他功能,但基本上要点是打开工作簿,找到一个单元格,取出它旁边的单元格,粘贴到原始工作簿中。问题是它会拾取包含字符串的多个单元格。我使用了"Curren" & "Core"
,因为宏似乎不能很好地处理字符串中的空格。
答案 0 :(得分:2)
更改
Set c = .Find(findValues(i), LookIn:=xlValues)
到
Set c = .Find(findValues(i), LookIn:=xlValues, LookAt:=xlWhole)
要求整个单元格匹配搜索字符串,而不仅仅是单元格的一部分。