Split delimited entries into new rows in Access

杀马特。学长 韩版系。学妹 提交于 2019-12-01 20:57:51

If I had to do it I would

  • Import the raw data into a table named [TempTable].
  • Copy [TempTable] to a new table named [ActualTable] using the "Structure Only" option.

Then, in a VBA routine I would

  • Open two DAO recordsets, rstIn for [TempTable] and rstOut for [ActualTable]
  • Loop through the rstIn recordset.
  • Use the VBA Split() function to split the [PO] values on "/" into an array.
  • For Each array item I would use rstOut.AddNew to write a record into [ActualTable]

About about this:

1) Import the source data into a new Access table called SourceData.

2) Create a new query, go straight into SQL View and add the following code:

SELECT * INTO ImportedData
FROM (
  SELECT PO, Vendor, State
  FROM SourceData
  WHERE InStr(PO, '/') = 0
  UNION ALL
  SELECT Left(PO, InStr(PO, '/') - 1), Vendor, State
  FROM SourceData
  WHERE InStr(PO, '/') > 0
  UNION ALL
  SELECT Mid(PO, InStr(PO, '/') + 1), Vendor, State
  FROM SourceData
  WHERE InStr(PO, '/') > 0) AS CleanedUp;

This is a 'make table' query in Access jargon (albeit with a nested union query); for an 'append' query instead, alter the top two lines to be

INSERT INTO ImportedData
SELECT * FROM (

(The rest doesn't change.) The difference is that re-running a make table query will clear whatever was already in the destination table, whereas an append query adds to any existing data.

3) Run the query.

In Excel, try this:

IF(ISERROR(FIND("/",A1,1))=TRUE,A1, Left(A1, 6))

In the cell next to it, put something like this:

IF(ISERROR(FIND("/",A1,1))=TRUE,"", Right(A1, 6))

This will break out your PO's into 2 columns. From there, write a loop that creates a new record where appropriate.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!