Adding values to excel from an Array

edTech

New Member
Joined
Dec 8, 2019
Messages
33
Office Version
  1. 2019
Platform
  1. Windows
Hello, I am wondering if someone can help me with the following code which is not working correctly. I collect values from an external GUI and add them to an Arraylist. I am trying to add the values to excel in two columns. The results I get is that as it For loops I only get the last value. The amount values into the array will vary between amounts. I also need to add a new row into excel if the last row has a value already in it. Here is my current code:

last_Row = objSheet.Range("B" & objSheet.Rows.Count).End(xlUp).Row

For i=0 to Ubound(ArrayDoc)
If ArrayDoc(i) <> "" or ArrayRev(i) <> "" Then

myArrayList.Add ArrayDoc(i)
myArrayList.Add ArrayRev(I)
End if
next

For each a in myArraylist
objsheet.Range("B2:C" & last_Row) = a

next



Output:

dd
dd
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try this:
VBA Code:
    Dim r As Long
    r = 0
    For i = 0 To myArraylist.Count - 1 Step 2
        objSheet.Range("B2:C2").Offset(r).Value = Array(myArraylist(i), myArraylist(i + 1))
        r = r + 1
    Next
Why do you need an ArrayList? A normal array would probably suffice.
 
Upvote 0
Try this:
VBA Code:
    Dim r As Long
    r = 0
    For i = 0 To myArraylist.Count - 1 Step 2
        objSheet.Range("B2:C2").Offset(r).Value = Array(myArraylist(i), myArraylist(i + 1))
        r = r + 1
    Next
Why do you need an ArrayList? A normal array would probably suffice.

You are correct John, this is what I ended up doing instead of doing the Arraylist based on your advice, but I do not know how to start in Cell 17

For i=0 to Ubound(ArrayDoc)

objSheet.Cells(i,1).Value = ArrayDoc(i)
objSheet.Cells(i,2).Value = ArrayRev(i)



Next
 
Upvote 0
What do you mean by cell 17? Row 17 or column 17, or something else?

If you mean row 17, then:
VBA Code:
For i=0 to Ubound(ArrayDoc)
  objSheet.Cells(i+17,1).Value = ArrayDoc(i)
  objSheet.Cells(i+17,2).Value = ArrayRev(i)
 Next
 
Upvote 0
What do you mean by cell 17? Row 17 or column 17, or something else?

If you mean row 17, then:
VBA Code:
For i=0 to Ubound(ArrayDoc)
  objSheet.Cells(i+17,1).Value = ArrayDoc(i)
  objSheet.Cells(i+17,2).Value = ArrayRev(i)
Next

Thank you John_w it worked, but what I did notice is when I add 17 it actually skips two rows and it lands on 19. So I put it for 15 to land on 17.
 
Upvote 0
Seems a bit odd to me to be taking two parallel lists and combining them a row at a time (if relevant) into a single list and then re-dividing them a row at a time into two parallel lists.
Why not skip that middle part and write the relevant components straight to the final destination? Something like

VBA Code:
k = 0
For i = 0 To UBound(ArrayDoc)
  If ArrayDoc(i) <> "" Or ArrayRev(i) <> "" Then
    k = k + 1
    objSheet.Cells(17 + k, 1).Value = ArrayDoc(i)
    objSheet.Cells(17 + k, 2).Value = ArrayRev(i)
  End If
Next i
 
Upvote 0
Seems a bit odd to me to be taking two parallel lists and combining them a row at a time (if relevant) into a single list and then re-dividing them a row at a time into two parallel lists.
Why not skip that middle part and write the relevant components straight to the final destination? Something like

VBA Code:
k = 0
For i = 0 To UBound(ArrayDoc)
  If ArrayDoc(i) <> "" Or ArrayRev(i) <> "" Then
    k = k + 1
    objSheet.Cells(17 + k, 1).Value = ArrayDoc(i)
    objSheet.Cells(17 + k, 2).Value = ArrayRev(i)
  End If
Next i


That was fantastic Peter_SSs thank you
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top