[英]VBA Excel: How to read certain parts of variable length in a cell
因此,這里是編程初學者。
我需要從數據庫中提取一些信息。 即,我需要與給數據庫中每個日志的每個ID對應的“描述”。 數據庫為我提供了我需要的數據,如下所示:一個excel工作表,其中對於數據庫中的每個ID,它使用相應的數據填充第一列中的單元格。
看起來像這樣(括號中的內容實際上不是數據的一部分):
(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:) (....)
我需要獲取每個單元格的描述部分,並以某種方式保存它,使其仍然與ID相對應,以便我可以將其粘貼到另一個電子表格中,其中僅給出ID。
我認為最大的線索是描述部分始終是單元格中的第n個對象。 它總是出現:ID,“ ...”,“ ...”,“ ...”,“描述”,“ ...”。 唯一的是可變的,是括號中的實際內容。
因此,我想做的是制作一個數組或一個字典(或等效的VBA),該數組或字典保存對應於“單元格Ax括號中的第5個條目”的ID。
我不希望您收到萬無一失的代碼。 我只是需要一些從哪里開始的提示,因為我有點迷路。 谷歌給我的唯一一件事就是那些只計算一個單元格中單詞的人。 但這當然不能解決我的問題。
提前謝謝你
您可以使用split(range(“ a1”)。value,“,”),這將使用逗號作為定界符為您提供行數組,然后id將在索引0中,然后您可以確定所需的內容,在示例中,您將添加索引0作為鍵,並添加索引4
類似(未經測試)
dim strSplit() as string
dim dicIDSandDesc as new scripting.dictionary
strSplit=split(range("a1").value,",")
dicIDSandDesc.add strSplit(0),strSplit(4)
嘗試這個:
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.