简体   繁体   English

使用Excel和VBA查找字符串?

[英]Find in string with Excel and VBA?

I have a spreadsheet with a few worksheets in it, one being Product Inventory and one being Material Inventory... 我有一个包含一些工作表的电子表格,一个工作表是产品库存,一个工作表是材料库存...

I am trying to figure out how I can have another worksheet to where I can look up a product code, pull the description out of Product Inventory, and extract materials needed from Material Inventory. 我试图弄清楚如何在另一个工作表中查找产品代码,从产品清单中提取描述,并从材料清单中提取所需的材料。 I know how to do this in other languages, but I'm extremely limited to just Excel and I don't know how to go about it. 我知道如何用其他语言来执行此操作,但我仅限于Excel,并且不知道如何执行。

Say I have Product Lookup:$B$2 = "ps26k417" , which I already have pulling info from Product Inventory which pulls the item description, Garden Gate Indigo/Linen Natural 26x26 KE Fiber Pillow* But now I need to figure out how to pull from Material Inventory.. 假设我有Product Lookup:$B$2 = "ps26k417" ,我已经从Product Inventory中获取了相关信息,其中包含了产品说明, Garden Gate Indigo/Linen Natural 26x26 KE Fiber Pillow*但现在我需要弄清楚如何拉从材料库存

From that product description, I need to pull two rows out of the Material Inventory sheet.. I need a way to extract info based on the description, in this example I need the row for 417 Garden Gate Indigo/Linen and the row for 554 Linen Natural/S Backed 从该产品描述中,我需要从“物料清单”工作表中拉出两行。.我需要一种基于描述提取信息的方法,在此示例中,我需要417 Garden Gate Indigo/Linen的行和554 Linen Natural/S Backed的行554 Linen Natural/S Backed

Is it even worth going this in-depth in an Excel macro? 甚至值得在Excel宏中进行深入介绍吗?

Depending on what else you need to do, I would make a properly structured Access database for that. 根据您需要执行的其他操作,我将为此构建一个结构合理的Access数据库。 You need 3 tables: Products, Materials and BOM (Bill of materials). 您需要3个表格:产品,物料和BOM(物料清单)。
BOM will have 3 fields: ProductId, MaterialId, Quantity. BOM将包含3个字段:ProductId,MaterialId,数量。
With just that, and a simple Parameter Query, you can solve your problem. 有了这些,再加上简单的参数查询,就可以解决您的问题。

If each result from the ProductInventory sheet translates into exactly two entries on the MaterialInventory sheet, the simplest option would be to use two VLOOKUPS formulae. 如果ProductInventory工作表中的每个结果准确地转换为MaterialInventory工作表中的两个条目,则最简单的选择是使用两个VLOOKUPS公式。

If you'd like to go down the VBA route, you'll probably need code like the following: 如果您想沿用VBA路线,则可能需要如下代码:

Public Sub SearchForMaterial(materials() As String)
    Dim N As Long
    Dim materialsList As Range
    Set materialsList = ' Set to the relevant range

    Dim cell As Range

    Dim output(UBound(materials) - LBound(materials)) As String
    Dim outputCount As Long

    For N = LBound(materials) To UBound(materials)
        For Each cell In materialsList.Cells
            If cell.Value = materialsList(N) Then
                output(outputCount) = cell.Offset(ColumnOffset:= ??)
            End If
        Next cell
    Next N
End Sub

This code isn't particularly efficient. 这段代码不是特别有效。 You say that you have experience in other languages, so hopefully this should be enough to get you started. 您说自己有其他语言的经验,因此希望这足以使您入门。

I gave up trying to figure this out with just Excel and VBA. 我放弃了尝试仅使用Excel和VBA来解决这个问题。 I made a PHP script that converted all the columns I needed in the Products and Materials worksheets, threw all that into a MySQL database, pulled it all out as CSV and made a new spreadsheet. 我制作了一个PHP脚本,该脚本转换了我在“ ProductsMaterials工作表中所需的所有列,将所有内容都放入一个MySQL数据库中,将其全部提取为CSV并创建了一个新的电子表格。

Does the products table have a unique ID? 产品表是否具有唯一ID? Does the Materials table have a foreign key related to the products table? 材料表是否具有与产品表相关的外键?

If not...Why not? 如果没有...为什么不呢? Would make things a lot easier and faster than searching with text values. 与使用文本值进行搜索相比,这将使事情变得容易和快捷得多。

Check out this Link for using SQL within excel. 查看此链接以在excel中使用SQL。 Should be easy if you have Unique IDs. 如果您具有唯一ID,应该会很容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM