PDA

View Full Version : [SOLVED] RUN TIME ERROR FOR A VARIANT! DONT KNOW MY MISTAKE HELP



ytjjjtyj
06-27-2019, 06:10 AM
Hi, can someone please tell me what I am doing wrong? Everything looks fine to me but I am still getting Run-time error '1004': Application-defined or object-defined error. I am trying to make a 2X2 variant from the values in Range("A1:B2")

My code:


Dim Table(1 To 2, 1 To 2) As Variant
For i = i To 2
Table(1, i) = wbk.Worksheets("Sheet1").Range("A1").Item(1, i)
Table(2, i) = wbk.Worksheets("Sheet1").Range("B1").Item(2, i)
Next i

Thanks in advance!

ytjjjtyj
06-27-2019, 06:14 AM
Also, my wbk variable is set correctly to open a workbook.

austenr
06-27-2019, 06:44 AM
it would help i a workbook was posted and the entire code you are having problems with. snippets of code and trying to debug it are useless.

mattreingold
06-27-2019, 06:48 AM
Not sure what youre trying to accomplish with .item. What is your goal?

Also, your forloop says "For i = i To 2" try 1 to 2.

Try:


Table(1, i) = wbk.Worksheets("Sheet1").Range("A1").Value

Or:


For i = 1 To 2
For j = 1 To 2
Table(i, j) = wbk.Sheets(1).Cells(i, j)
Next j
Next i

Again, not sure what youre trying to do, if you explained it'd be easier to help.

ytjjjtyj
06-27-2019, 06:56 AM
It worked! Thanks! I put in your code below and that's exactly what I needed just didn't understand how to do it or explain it. You are the best!
For i = 1 To 2
For j = 1 To 2
Table(i, j) = wbk.Sheets(1).Cells(i, j)
Next j
Next i

mattreingold
06-27-2019, 07:08 AM
Glad I can help - quick tip, you should use #CODE tags around any code you post, its the hashtag looking thing in the toolbar of a post.

Best,
Matt

ytjjjtyj
06-27-2019, 07:26 AM
Got it, thanks so much! :)