简体   繁体   English

VBA Excel:如何读取单元格中可变长度的某些部分

[英]VBA Excel: How to read certain parts of variable length in a cell

So, programming beginner here. 因此,这里是编程初学者。

I need to extract some information from a database. 我需要从数据库中提取一些信息。 Namely, i need the "description" corresponding to each ID that is given to each log in the database. 即,我需要与给数据库中每个日志的每个ID对应的“描述”。 The database gives me the data i need like this: An excel worksheet where for each ID in the database, it fills a cell in the first column with the corresponding data. 数据库为我提供了我需要的数据,如下所示:一个excel工作表,其中对于数据库中的每个ID,它使用相应的数据填充第一列中的单元格。

It looks like this (the stuff in brackets isn't actually part of the data): 看起来像这样(括号中的内容实际上不是数据的一部分):

(Cell A1:) ID, "Project>Project Name","Date","Duration","Person",(....),"Description","Date Updated", (and so on)

(Cell A2:) 12345, "VBAEXCELSTACKOVERFLOW123","2016-03-24","8hrs","j doe",(...),"finding a way to extract this sentence","2016-03-24 10:05:43", (and so on)

(Cell A3:) 12346, "VBAEXCELSTACKOVERFLOW123","2016-03-24","6hrs","w smith",(...),"i need this one too","2016-03-24 10:06:20", (and so on)

(Cell A4:) (....)

I need to get the description part of each cell and save it in a way, where it still corresponds to the ID, so that i can paste it into another spreadsheet, in which only the IDs are given. 我需要获取每个单元格的描述部分,并以某种方式保存它,使其仍然与ID相对应,以便我可以将其粘贴到另一个电子表格中,其中仅给出ID。

I think the biggest clue is that the description part is always the nth object in the cell. 我认为最大的线索是描述部分始终是单元格中的第n个对象。 It always goes: ID, "...","...","...","description","..." . 它总是出现:ID,“ ...”,“ ...”,“ ...”,“描述”,“ ...”。 The only thing, that is variable, is the actual content in the parentheses. 唯一的是可变的,是括号中的实际内容。

So what I want to do is make an array or a dictionary (or the VBA equivalent), that saves the ID corresponding to "the 5th entry in parentheses in cell Ax". 因此,我想做的是制作一个数组或一个字典(或等效的VBA),该数组或字典保存对应于“单元格Ax括号中的第5个条目”的ID。

I don't expect a foolproof code from you. 我不希望您收到万无一失的代码。 I just need some kind of tip where to start, as i'm kind of lost. 我只是需要一些从哪里开始的提示,因为我有点迷路。 The only thing google gave me was people who just counted the words in a cell. 谷歌给我的唯一一件事就是那些只计算一个单元格中单词的人。 But that certainly doesn't solve my problem. 但这当然不能解决我的问题。

Huge thank you in advance 提前谢谢你

You could use split(range("a1").value,","), this will give you and array of the line using the comma as delimiter, then the id will be in index 0 and then you can determine what you want, from the example, you'd add index 0 as your key and index 4 您可以使用split(range(“ a1”)。value,“,”),这将使用逗号作为定界符为您提供行数组,然后id将在索引0中,然后您可以确定所需的内容,在示例中,您将添加索引0作为键,并添加索引4

something like (not tested) 类似(未经测试)

dim strSplit() as string
dim dicIDSandDesc as new scripting.dictionary

strSplit=split(range("a1").value,",")
dicIDSandDesc.add strSplit(0),strSplit(4)

Try this: 尝试这个:

Sub Split_String()
    Dim WordArr() As String
    Dim txt As String
    CurrSheetRowCount = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
    For i = 2 To CurrSheetRowCount
        txt = Cells(i, 1).Value
        WordArr() = Split(txt, ",")
        Cells(i, 2).Value = WordArr(1)     'here 2 is the column number where id will be displyed
        Cells(i, 3).Value = WordArr(6)     'here 3 is the column number where description will be displyed
    Next i
End Sub

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

相关问题 如何按值划分N个等份(excel) - How to devide N equal parts by value (excel) 如何更改一列未格式化的数字,以便 Excel 可以使用 VBA 将它们实际读取为数字 - How can I change a column of unformatted numbers so that Excel can actually read them as numbers with VBA 如何按列名称读取Excel文件数据并将其保存到数据库表中 - How to read excel file data each cell and save that data into database table as per the column name 如何不显示数据库中表的某些部分? - How do I not display certain parts of the table from my database? Excel VBA从设置为只读的Oracle数据库读取数据 - Excel vba to read data from a oracle database which was set to readonly 是否有一个excel公式可以从单元格中的字符串末尾提取数字,其中长度并不总是恒定的 - Is there an excel formula to extract numbers from the end of a string in a cell, where the length is not always constant 如何仅搜索文本文件中具有特定长度的行 - How to search only lines with certain length in text file 单元格的未使用长度/值 - Unused length/value of the cell 如何在dataGridView C#中自动为特定单元格着色 - How to automatically color a certain cell in dataGridView C# 访问在联合查询中删除重复项的某些部分 - Access Removing CERTAIN PARTS of Duplicates in Union Query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM