Consulting

Results 1 to 7 of 7

Thread: RUN TIME ERROR FOR A VARIANT! DONT KNOW MY MISTAKE HELP

  1. #1
    VBAX Regular
    Joined
    Jun 2019
    Posts
    50
    Location

    RUN TIME ERROR FOR A VARIANT! DONT KNOW MY MISTAKE HELP

    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!

  2. #2
    VBAX Regular
    Joined
    Jun 2019
    Posts
    50
    Location
    Also, my wbk variable is set correctly to open a workbook.

  3. #3
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    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.
    Peace of mind is found in some of the strangest places.

  4. #4
    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.

  5. #5
    VBAX Regular
    Joined
    Jun 2019
    Posts
    50
    Location
    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

  6. #6
    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

  7. #7
    VBAX Regular
    Joined
    Jun 2019
    Posts
    50
    Location
    Got it, thanks so much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •