Pergunta

I am a total novice with Excel VBA so please bear with me.

I am trying to parse the cell values based on the delimiter ":". The value before the delimiter and after the delimiter will be stored in an array so that I can reference the array values in WHERE COL1 = [value before ":" in array] AND COL2= [corresponding value after ":" in array] clause in a SQL query.

My data looks like below:

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

and so on. The data is stored in at a fixed range A1:A10 but may not be contiguous. I would want to run the SQL statement using a recordset .e.g

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

Following is what I have done so far and it's not working:

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

If anyone can point out what am I doing incorrect and provide some guidance. I have spent my past three days to figure out a solution so that i can extract all non-null values and use them in the sql statement.

Many thanks in advance for at least looking at.

Edit 1:

Finally, did some digging around and came up with something, 98% of which works.

Sample input:

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

d:ee

f:aaaaaa

Final Output:

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

Not sure why I am not getting the complete string after ":". Below is my complete code for the above output

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
Foi útil?

Solução

Here's my answer

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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top