简体   繁体   English

参考工作簿在其中包含文件名中的特定文本?

[英]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.

相关问题 如果单元格包含特定文本,则移动到新工作簿 - If cell contains specific text then move to a new workbook 如果文件名包含特定文本,则从文件名中提取信息(excel vba) - If file name contains specific text then pull information from filename (excel vba) VBA参考工作簿,如果活动和文件名的最后7个字符包含X? - vba reference workbook if active and last 7 characters of filename contain x? 将Excel VBA代码保存为特定文件名并关闭活动工作簿 - Excel VBA code to SaveAs a specific filename and close the active workbook 设置工作簿路径中的文本单元格引用 - Text cell reference in setting workbook path 在一个工作簿中查找和 Select 个具有特定文本的单元格并复制到另一个工作簿 - Find and Select cells with specific text in one workbook and copy to another workbook Java Check 工作簿包含特定电子表格或未使用 Apache POI - Java Check workbook contains a specific spreadsheet or not using Apache POI 工作簿文件名的静态变量 - Static variable for workbook filename 从文本文件 VBA 中提取数据,其中该行包含特定字符串 - Extract data from text file VBA, where the line contains a specific string 选择B列中的所有单元格,其中该行中的A列包含特定文本 - Select all cells in column B where Column A in that row contains a specific text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM