1

I am doing a simple loop code where I want Y to equal 10 and each time it goes through the loop to +1 i.e. 10,11,12,13

However I am getting compile errors on the variables X and Y if anyone could let me know why this is occuring it would be much appreciated.

Dim targetrow As Long
targetrow = ActiveSheet.Range("Total").Offset(-2, 0).Row

Y = 10
For X = 19 To targetrow Step 1
If Range("K" & X) <> "" Then

Range("K" & X).Copy
Workbooks(PtchFile).Activate
Range("G" & Y).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

Y = Y + 1
X = X + 1
Else
End If
Next X
2
  • 2
    What error are you getting? Do you not have to declare those variables? I mean Dim Y As Integer and Dim X As Integer? Commented Oct 21, 2015 at 13:15
  • 2
    If you are using Option Explicit which you should be, then you do need to declare them. But @Racil Hilan asked the more important question. What error are you getting? Commented Oct 21, 2015 at 13:21

1 Answer 1

1

declare your variable as following above your code snippet:

Dim X as Integer
Dim Y as Integer

You may also want do activate the workbook where you copy your range before you copy the range, at the next iteration it will copy the Range("K" & X+1) from the Workbook PtchFile

cheers.

Sign up to request clarification or add additional context in comments.

2 Comments

its worth noting that you may need to declare your variables as type Long. You declared target as Long which is great, especially if the row is greater than ~32,000 (see this:stackoverflow.com/questions/10558540/…). You will get an Overflow error if the X or Y is too high of a number for an Integer type.
I though it wasn't worth mentioning, but for anyone interested : stackoverflow.com/questions/26409117/… , true there is no value in declaring variable as integer, but habits are hard to break =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.