我需要一个输出类似这样的公式:
如果特定文件夹中的文件名包含(因为文件名在@字符后面会有一个附加字符串),所以如果文件名包含单元格值AB1,则在单元格AC1中添加完整文件名。
这可以用VBA吗?
非常感谢
答案 0 :(得分:4)
这在VBA中是否可行?
不确定。这是我一直使用的VBA辅助函数:
Public Function Contains(ByVal string_source As String, ByVal find_text As String, Optional ByVal caseSensitive As Boolean = False) As Boolean
Dim compareMethod As VbCompareMethod
If caseSensitive Then
compareMethod = vbBinaryCompare
Else
compareMethod = vbTextCompare
End If
Contains = (InStr(1, string_source, find_text, compareMethod) <> 0)
End Function
每当我有多个值要检查时,我也会使用这个值 - 它比执行If {check1} Or {check2} Or {check3}...
的效果更好,因为它会在找到匹配后立即返回,因此{check42}
不会如果{check1}
返回True
,则会对其进行评估:
Public Function ContainsAny(ByVal string_source As String, ByVal caseSensitive As Boolean, ParamArray find_strings() As Variant) As Boolean
Dim find As String, i As Integer, found As Boolean
For i = LBound(find_strings) To UBound(find_strings)
find = CStr(find_strings(i))
found = Contains(string_source, find, caseSensitive)
If found Then Exit For
Next
ContainsAny = found
End Function
答案 1 :(得分:1)
考虑:
Sub qwerty()
Dim FileSpec As String, FileName As String
Dim v As String
FileSpec = "C:\TestFolder\2013\Fed taxes\qwerty31416.xlsm"
ary = Split(FileSpec, "\")
FileName = ary(UBound(ary))
v = Range("AB1").Value
If InStr(1, FileName, v) > 0 Then
Range("AC1").Value = FileName
End If
End Sub