0

我是 Excel VBA 的新手,所以请多多包涵。

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

我的数据如下所示:

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

等等。数据存储在固定范围 A1:A10 中,但可能不连续。我想使用记录集运行 SQL 语句 .eg

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

编辑2:

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
4

1 回答 1

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
于 2013-08-11T15:49:43.453 回答