Forum Discussion

Bigbruce3's avatar
Bigbruce3
Copper Contributor
Jul 28, 2020

VBA Object Variable

I keep Having errors with the below code

Public TheDeck As Worksheet

Public TheTable As Worksheet

Public NextCard as Range

Public Hitlocation as Range

Public NumberCardInhand as Interger

Public NumberCardInDeck as Interger

 

Set TheDeck = Thisworkbook.Sheets("Deck")

Set TheTable = Thisworkbook.Sheets("Table")

NumberCardInHand = 1

NumberCardinDeck =1

Set NextCard = TheDeck.Range("A" & NumberCardInDeck + 1)

Set HitLocation = TheTable.Range("H" & (NumberCardInHand + 23))

 

Do until NumberCardinHand = 2

HitLocation.value = NextCard.value

NumberCardinHand = NumberCardInHand + 1

NumberCardInDeck = NumberCardInDeck + 1

Loop

 

There is more code around this but this is the applicable part. This code Works perfect for the first iteration of the loop But t holds the original value of "NextCard" + "HitLocation" indefinitely and doesnt update them if the values they are dependent on change. is that normal and are there any workarounds?

1 Reply

  • Bigbruce3 

    You'd have to move the lines

    Set NextCard = TheDeck.Range("A" & NumberCardInDeck + 1)

    Set HitLocation = TheTable.Range("H" & (NumberCardInHand + 23))

    inside the loop.

Resources