VBA error- variable not defined

Bryman

Board Regular
Joined
Jun 2, 2010
Messages
96
Can someone explain what the error "variable not defined" means I have this code on my sheet:

Code:
Private Sub WorkSheet_Change(ByVal Target As Range)
With Sheets("Sheet1")
j = 2
For i = 10 To 41
    If UCase(.Range("M" & i).Text) = "Yes" Then
        .Range("F" & i).Copy
        Sheets("Sheet7").Range("A" & j).PasteSpecial Paste:=xlPasteValues
        j = j + 1
    End If
Next i
    If Intersect(Target, .Range("B:B")) Is Nothing Or Target.Cells.Count > 1 Then
         Exit Sub
    Else
          Target.Offset(, 1).Value = Date
          Target.Offset(, 2).Value = Time
          Target.Offset(, 15).Formula = "=$J$7"
          Target.Offset(, 24).Formula = "=$J$7"
    End If
  End With
End Sub

In the VBA window it's telling me that 'variable j is undefined' I not sure I see how define it correctly; otherwise the code should do what I need it to do.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Try this

Rich (BB code):
Private Sub WorkSheet_Change(ByVal Target As Range)
Dim i As Long, j As Long
With Sheets("Sheet1")
j = 2
For i = 10 To 41
    If UCase(.Range("M" & i).Text) = "Yes" Then
        .Range("F" & i).Copy
        Sheets("Sheet7").Range("A" & j).PasteSpecial Paste:=xlPasteValues
        j = j + 1
    End If
Next i
    If Intersect(Target, .Range("B:B")) Is Nothing Or Target.Cells.Count > 1 Then
         Exit Sub
    Else
          Target.Offset(, 1).Value = Date
          Target.Offset(, 2).Value = Time
          Target.Offset(, 15).Formula = "=$J$7"
          Target.Offset(, 24).Formula = "=$J$7"
    End If
  End With
End Sub
 
Upvote 0
Ok thank you, that was evidently only part of my problem

But I guest macro is not doing what I need it do.

What I want it to do is:

If column M = "Yes" (in sheet 1) I want it to copy the entire row of values in that row in sheet 7

The time stamp is working though
 
Last edited:
Upvote 0
Thank you It works now,

However it is only copying column F, how do I make it copy column B:Z ?

I tried switching F to:

Code:
.Range("B:Z" & i).Copy
 
Upvote 0
Thank you Peter it works now.


Let me know where I need to vote for you for MVP.

You're fast and accurate.
 
Upvote 0

Forum statistics

Threads
1,224,732
Messages
6,180,622
Members
452,991
Latest member
JM_000888

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top