0

我有一个关于分解某些东西的方法的问题。我得到了这个 Excel 电子表格,它为我提供了制作一份报告所需的数据。它非常简单直接,但是其中有一个特别的部分让我有些悲伤。

在 Excel 电子表格中,有一列列出了“相关各方”。通常是 12 个人左右的名字,用逗号隔开,但后面括号里也有 orgID:

乔·史密斯 (DIV32)、约翰·多伊 (DIV12)、罗杰·安德鲁斯 (DIV14、DIV67、DIV01) 等

我需要将它们分成单独的列,以便在我将它们导入访问时它们将成为单独的字段。jon doe我知道如何在 Excel 中“文本到列”,但是当(DIV13、DIV54 等)有多个分区时,这会搞砸。

不知道如何在访问中执行此操作,但很想知道。

请问有人有excel公式或访问方法吗?

4

2 回答 2

1

这是一个解决方案:

我编写了一个函数,它用 strReplace 替换所有出现的字符串 strFind,但前提是 strFind 出现在括号内。因此,您可以将所有“,”字符替换为其他字符(例如“*”),然后将 Excel 的文本运行到列,然后再次将“*”替换为“,”。

Function replace_paren(strSource As String, strFind As String, strReplace As String) As String
    ' Within strString, replaces any occurrence of strFind with strReplace *IF* strFind occurs within parentheses '

    Dim intOpenParenIndex As Integer
    Dim intCloseParenIndex As Integer

    Do
        intOpenParenIndex = InStr(intCloseParenIndex + 1, strSource, "(")
        intCloseParenIndex = InStr(intOpenParenIndex + 1, strSource, ")")
        If intOpenParenIndex = 0 Then
            Exit Do
        End If

        Mid(strSource, intOpenParenIndex, intCloseParenIndex - intOpenParenIndex) = Replace(Mid(strSource, intOpenParenIndex, intCloseParenIndex - intOpenParenIndex), strFind, strReplace)
    Loop

    replace_paren = strSource

End Function

所以步骤是:

1) 将此宏复制到 Excel 工作簿中的模块中

2)假设您的字符串在A列中。在B列中,设置函数来替换逗号,如下所示

=replace_paren(A1,",","*")

3)在列下填写公式

4)将列复制并粘贴为值

5) 使用 Excel 的 text to columns 来解析使用“,”作为分隔符的列

6) 再次使用 replace_paren 将所有出现的“*”替换为“,”

于 2009-07-22T20:18:05.507 回答
1

我假设您正在将其导入数据库。如果没有,即使是暂时的,如果你这样做可能会更容易;并在每次必须运行报告时重做

您最终将拥有三个表来表示这种情况

  1. 党(人)
  2. 组织
  3. 党组织

具有 ID 列的 theParty

theOrganisation 将拥有自己的 ID 列

thePartyOrder 将有它自己的 ID 列,一个用于 theParty,一个用于 theOrganisation

每当您想代表一个政党作为组织的成员时,您必须确保该政党存在/已创建;组织存在/已创建,然后在指向两者的 thePartyOrg 表中创建一个条目。

由于此连接信息仅作为文本存储在单个列中,因此首先将其全部读入临时表然后在 VBA 中解析该列可能是最简单的

于 2009-07-21T15:12:24.763 回答