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
Community
  • 1
  • 1
Sean Bailey
  • 375
  • 3
  • 15
  • 33
  • 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`? – Racil Hilan Oct 21 '15 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? – MatthewD Oct 21 '15 at 13:21

1 Answers1

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.

ga56mor
  • 73
  • 8
  • 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:http://stackoverflow.com/questions/10558540/vba-macro-crashes-after-32000-rows). You will get an `Overflow` error if the X or Y is too high of a number for an `Integer` type. – Scott Holtzman Oct 21 '15 at 13:25
  • I though it wasn't worth mentioning, but for anyone interested : http://stackoverflow.com/questions/26409117/why-use-integer-instead-of-long , true there is no value in declaring variable as integer, but habits are hard to break =) – ga56mor Oct 21 '15 at 13:27