[英]Reference workbook where it contains specific text in filename?
I have had help from @user3598756 with this code.我从@user3598756 那里得到了这个代码的帮助。
I am trying to copy values from my slave workbook to my master workbook.我正在尝试将值从我的从属工作簿复制到我的主工作簿。
My slave workbook can change name from time to time, but will always include 'depot memo' or 'Depot Memo' in the title.我的从属工作簿可能会不时更改名称,但标题中将始终包含“仓库备忘录”或“仓库备忘录”。
Food Depot Memo
DRINKS DEPOT MEMO
Bakery depot memo 123
So far I have the below code which works if the filename contains 'Depot Memo' with capital letters.到目前为止,如果文件名包含带有大写字母的“Depot Memo”,我有以下代码。
However, this code doesn't work if the 'depot memo' is in lower case.但是,如果“仓库备忘录”是小写的,则此代码不起作用。
Code:代码:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim oCell As Range, targetCell As Range
Dim ws2 As Worksheet
If Not Intersect(Target, Range("I:I")) Is Nothing Then ' <-- run this code only if a value in column I has changed
If Not GetWb("Depot Memo", ws2) Then Exit Sub
With ws2
For Each targetCell In Target
Set oCell = .Range("J1", .Cells(.Rows.Count, "J").End(xlUp)).Find(what:=targetCell.Value, LookIn:=xlValues, lookat:=xlWhole)
If Not oCell Is Nothing Then
Application.EnableEvents = False
targetCell.Offset(0, 1).Value = oCell.Offset(0, -3)
targetCell.Offset(0, 2).Value = oCell.Offset(0, 8)
Application.EnableEvents = True
End If
Next
End With
End If
End Sub
Function GetWb(wbNameLike As String, ws As Worksheet) As Boolean
Dim wb As Workbook
For Each wb In Workbooks
If wb.Name Like "*" & wbNameLike & "*" Then '<-- check if workbook name contains "Depot Memo"
Set ws = wb.Worksheets(1)
Exit For
End If
Next
GetWb = Not ws Is Nothing
End Function
I figured out the answer, and its relatively simple.我想出了答案,它相对简单。
All that needs to be added to the top of the module is:所有需要添加到模块顶部的是:
Option Compare Text
This essentially removes the case sensitivity这基本上消除了区分大小写
Full Code完整代码
Option Explicit
Option Compare Text
Private Sub Worksheet_Change(ByVal Target As Range)
Dim oCell As Range, targetCell As Range
Dim ws2 As Worksheet
If Not Intersect(Target, Range("I:I")) Is Nothing Then ' <-- run this code only if a value in column I has changed
If Not GetWb("Depot Memo", ws2) Then Exit Sub
With ws2
For Each targetCell In Target
Set oCell = .Range("J1", .Cells(.Rows.Count, "J").End(xlUp)).Find(what:=targetCell.Value, LookIn:=xlValues, lookat:=xlWhole)
If Not oCell Is Nothing Then
Application.EnableEvents = False
targetCell.Offset(0, 1).Value = oCell.Offset(0, -3)
targetCell.Offset(0, 2).Value = oCell.Offset(0, 8)
Application.EnableEvents = True
End If
Next
End With
End If
End Sub
Function GetWb(wbNameLike As String, ws As Worksheet) As Boolean
Dim wb As Workbook
For Each wb In Workbooks
If wb.Name Like "*" & wbNameLike & "*" Then '<-- check if workbook name contains "Depot Memo"
Set ws = wb.Worksheets(1)
Exit For
End If
Next
GetWb = Not ws Is Nothing
End Function
Implement something like this into your code to uppercase your slave workbook name, and then checks to see if it contains "DEPOT MEMO".在您的代码中实现类似的内容以大写您的从属工作簿名称,然后检查它是否包含“DEPOT MEMO”。
Sub Example()
Dim IncomingWBName As String
IncomingWBName = "Drinks DEPOT Memo" 'Set incoming name
IncomingWBName = UCase(IncomingWBName) 'Set all to uppercase
If InStr(IncomingWBName, "DEPOT MEMO") > 0 Then 'In String?
MsgBox "Contains DEPOT MEMO"
'Do something
Else
MsgBox "Doesn't contain DEPOT MEMO"
'Do Something else
End If
End Sub
---Implemented into your code--- ---实施到您的代码中---
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim oCell As Range, targetCell As Range
Dim ws2 As Worksheet
If Not Intersect(Target, Range("I:I")) Is Nothing Then ' <-- run this code only if a value in column I has changed
If Not GetWb(ws2) Then Exit Sub
With ws2
For Each targetCell In Target
Set oCell = .Range("J1", .Cells(.Rows.Count, "J").End(xlUp)).Find(what:=targetCell.Value, LookIn:=xlValues, lookat:=xlWhole)
If Not oCell Is Nothing Then
Application.EnableEvents = False
targetCell.Offset(0, 1).Value = oCell.Offset(0, -3)
targetCell.Offset(0, 2).Value = oCell.Offset(0, 8)
Application.EnableEvents = True
End If
Next
End With
End If
End Sub
Function GetWb(ws As Worksheet) As Boolean
Dim wb As Workbook
For Each wb In Workbooks
If InStr(UCase(wb.Name), "DEPOT MEMO") > 0 Then '<-- check if workbook name contains "DEPOT MEMO"
Set ws = wb.Worksheets(1)
Exit For
End If
Next
GetWb = Not ws Is Nothing
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.