读取Excel中的单元格内容,根据分隔符进行解析并插入到数组中

时间:2013-08-11 01:19:47

标签: arrays excel-vba excel-2003 vba excel

我是Excel VBA的新手,所以请耐心等待。

我试图根据分隔符“:”解析单元格值。分隔符之前和分隔符之后的值将存储在一个数组中,以便我可以引用WHERE COL1中的数组值= [数组中“:”之前的值] AND COL2 = [数组中“:”之后的相应值]子句在SQL查询中。

我的数据如下所示:

T:NYSE
ABX:NASDAQ
V:AIM
(blank)
009123:OTDCXE
(blank)
xxx:dhgjdg

等等。数据存储在固定范围A1:A10中,但可能不是连续的。我想使用记录集.e.g

运行SQL语句
for i=0 to [last element in array]
 'run SELECT statement for each element (excluding nulls)
 next

以下是我到目前为止所做的事情并没有奏效:

Sub test3()


Dim MyArray()
'Dim MyArray()
Dim MyRange As Range

Dim RowCount As Long
Dim ColCount As Long
Dim R As Long
Dim C As Long


Set MyRange = Range("A1:a10") ' <-- Adjust!!!
RowCount = 10 ' <-- Adjust as necessary
ColCount = 1  ' <-- Adjust as necessary

ReDim MyArray(1 To RowCount, 1 To ColCount)

For R = 1 To RowCount
    For C = 1 To ColCount
        MyArray(R, C) = MyRange(R, C).Value
    Next C
Next R

 'Following was done only for debugging purpose
For i = 1 To UBound(MyArray)
For j = 1 To UBound(MyArray)
Sheets(1).Cells(i + 20, j).Value = MyArray(i, j).Value
Next j
Next i


 End Sub

如果有人能够指出我做错了什么并提供一些指导。我花了三天时间找出一个解决方案,以便我可以提取所有非空值并在sql语句中使用它们。

非常感谢至少看一下。

修改1:

最后,做了一些挖掘并想出了一些东西,其中98%有效。

示例输入:

LEG:NYSE
LEG:TAM
SPCC:AIM
ONTPC:ZWSESA
0943292:owndgt
b:a

d:ee

f:aaaaaa

最终输出:

LEG     NYSE
LEG     :TAM
SPCC    C:AIM
ONTPC   ZWSESA
0943292 2:owndgt
b       :a
d       ee
f       aa

不确定为什么我在“:”之后没有得到完整的字符串。 以下是我对上述输出的完整代码

Sub test2()

Set ws = ThisWorkbook.Sheets("Sheet1")

MyArray = ws.Range("A1:A10")

Range("A11:A30").ClearContents


ReDim newarr(LBound(MyArray) To UBound(MyArray))
For i = LBound(MyArray) To UBound(MyArray)
 If MyArray(i, 1) <> "" Then
    j = j + 1
    newarr(j) = MyArray(i, 1)
 End If
Next i
ReDim Preserve newarr(LBound(MyArray) To j)


For R = 1 To UBound(newarr) ' First array dimension is rows.
 ws.Cells(R + 12, 1).Value = newarr(R)
Next R

'Store values preceding and succeeding the delimiter in two arrays

ReDim lftarray(LBound(newarr) To UBound(newarr))
ReDim rtarray(LBound(newarr) To UBound(newarr))

For i = LBound(newarr) To UBound(newarr)
 lftarray(i) = Split(newarr(i), ":")
 rtarray(i) = Right(newarr(i), InStr(1, newarr(i), ":"))
Next i

ReDim Preserve lftarray(LBound(newarr) To i)
ReDim Preserve rtarray(LBound(newarr) To i)

'Print values before ":"
For i = LBound(lftarray) To UBound(lftarray) ' First array dimension is rows.
 ws.Cells(i + 24, 1).Value = lftarray(i)
Next i


'Print values after ":"

For i = LBound(rtarray) To UBound(rtarray)
  ws.Cells(i + 24, 2).Value = rtarray(i)
Next i


End Sub

EDIT2:

Sub test2()

Set ws = ThisWorkbook.Sheets("Sheet1")

MyArray = ws.Range("A1:A10")

Range("A11:A30").ClearContents


ReDim newarr(LBound(MyArray) To UBound(MyArray))
For i = LBound(MyArray) To UBound(MyArray)
 If MyArray(i, 1) <> "" Then
    j = j + 1
    newarr(j) = MyArray(i, 1)
 End If
Next i
ReDim Preserve newarr(LBound(MyArray) To j)


For R = 1 To UBound(newarr) ' First array dimension is rows.
 ws.Cells(R + 12, 1).Value = newarr(R)
Next R

'Store values preceding and succeeding the delimiter in two arrays

ReDim lftarray(LBound(newarr) To UBound(newarr))
ReDim rtarray(LBound(newarr) To UBound(newarr))

For i = LBound(newarr) To UBound(newarr)
 lftarray(i) = Split(newarr(i), ":")
 rtarray(i) = Right(newarr(i), Len(newarr(i)) - InStr(1, newarr(i), ":"))
Next i

ReDim Preserve lftarray(LBound(newarr) To i)
ReDim Preserve rtarray(LBound(newarr) To i)

'Print values before ":"
For i = LBound(lftarray) To UBound(lftarray) ' First array dimension is rows.
 ws.Cells(i + 24, 1).Value = lftarray(i)
Next i


'Print values after ":"

For i = LBound(rtarray) To UBound(rtarray)
  ws.Cells(i + 24, 2).Value = rtarray(i)
Next i


End Sub

1 个答案:

答案 0 :(得分:0)

这是我的答案

Sub test2()

Set ws = ThisWorkbook.Sheets("Sheet1")

 MyArray = ws.Range("A1:A10")

 Range("A11:A30").ClearContents


ReDim newarr(LBound(MyArray) To UBound(MyArray))
 For i = LBound(MyArray) To UBound(MyArray)
   If MyArray(i, 1) <> "" Then
         j = j + 1
         newarr(j) = MyArray(i, 1)
  End If
Next i
ReDim Preserve newarr(LBound(MyArray) To j)


For R = 1 To UBound(newarr) ' First array dimension is rows.
 ws.Cells(R + 12, 1).Value = newarr(R)
Next R

'Store values preceding and succeeding the delimiter in two arrays

ReDim lftarray(LBound(newarr) To UBound(newarr))
ReDim rtarray(LBound(newarr) To UBound(newarr))

For i = LBound(newarr) To UBound(newarr)
  lftarray(i) = Split(newarr(i), ":")
  rtarray(i) = Right(newarr(i), Len(newarr(i)) - InStr(1, newarr(i), ":"))
Next i

ReDim Preserve lftarray(LBound(newarr) To i)
ReDim Preserve rtarray(LBound(newarr) To i)

'Print values before ":"
 For i = LBound(lftarray) To UBound(lftarray) ' First array dimension is rows.
  ws.Cells(i + 24, 1).Value = lftarray(i)
 Next i


'Print values after ":"

For i = LBound(rtarray) To UBound(rtarray)
 ws.Cells(i + 24, 2).Value = rtarray(i)
Next i


End Sub