Do I need .SELECT here? Columns not filling as expected.

logandiana

Board Regular
Joined
Feb 21, 2017
Messages
107
Here's a short section of my code.

Code:
With comp
    .Range("A1").PasteSpecial xlPasteValues
    .Rows("1:3").Delete
    .Columns("B:B").Delete
    .Columns("A:B").RemoveDuplicates Columns:=Array(1, 2), Header:=xlNo
LR2 = comp.Cells(Rows.Count, 1).End(xlUp).Row
    .Range("A1:B" & LR2).Copy
End With
With temp
    .Range("J2").PasteSpecial xlPasteValues
LR3 = temp.Cells(Rows.Count, 10).End(xlUp).Row
    .Range("Z2:Z" & LR3).Value = Range("K2:K" & LR3).Value
    .Range("K2:K" & LR3).Value = Range("J2:J" & LR3).Value

Once it pastes into J2 on temp it doesn't seem to do the following lines of code.
The code doesn't stop, it just ignores it.
LR3 does get defined.
If I put select in there, like this
Code:
With temp
    .Select
    .Range("J2").PasteSpecial xlPasteValues
LR3 = temp.Cells(Rows.Count, 10).End(xlUp).Row
    .Range("Z2:Z" & LR3).Value = Range("K2:K" & LR3).Value
    .Range("K2:K" & LR3).Value = Range("J2:J" & LR3).Value
it works as it should, but why do I need to do that for it to work.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
is temp = sheets("temp"), i'm wondering if its a reserved word, because you should be able to

With sheets("temp")
'.Select
.Range("J2").PasteSpecial xlPasteValues
LR3 = sheets("temp").Cells(Rows.Count, 10).End(xlUp).Row
.Range("Z2:Z" & LR3).Value = Range("K2:K" & LR3).Value
.Range("K2:K" & LR3).Value = Range("J2:J" & LR3).Value
end with
 
Upvote 0
is temp = sheets("temp"), i'm wondering if its a reserved word, because you should be able to

With sheets("temp")
'.Select
.Range("J2").PasteSpecial xlPasteValues
LR3 = sheets("temp").Cells(Rows.Count, 10).End(xlUp).Row
.Range("Z2:Z" & LR3).Value = Range("K2:K" & LR3).Value
.Range("K2:K" & LR3).Value = Range("J2:J" & LR3).Value
end with

the sheet name is "Template" and I Set temp = sheets("Template")
let me be clear, that it works with the .Select, just wondering why it doesn't work without it
 
Last edited:
Upvote 0
I just changed the sheet name and Dim to something else and it still doesn't work, so if temp is reserved, then that's not the issue here
 
Upvote 0
How about
Code:
With temp
    .Range("J2").PasteSpecial xlPasteValues
LR3 = temp.Cells(Rows.Count, 10).End(xlUp).Row
    .Range("Z2:Z" & LR3).Value = [COLOR=#ff0000].[/COLOR]Range("K2:K" & LR3).Value
    .Range("K2:K" & LR3).Value = [COLOR=#ff0000].[/COLOR]Range("J2:J" & LR3).Value
End With
 
Last edited:
Upvote 0
How about
Code:
With temp
    .Range("J2").PasteSpecial xlPasteValues
LR3 = temp.Cells(Rows.Count, 10).End(xlUp).Row
    .Range("Z2:Z" & LR3).Value = [COLOR=#ff0000].[/COLOR]Range("K2:K" & LR3).Value
    .Range("K2:K" & LR3).Value = [COLOR=#ff0000].[/COLOR]Range("J2:J" & LR3).Value
End With
This is the code that I have that is not working. It only works when I put .select somewhere in between
Code:
With temp
and
Code:
.Range("Z2:Z" & LR3).Value = [COLOR=#ff0000].[/COLOR]Range("K2:K" & LR3).Value
 
Upvote 0
Your code does not have a period in front of the 2 ranges on the right of the = sign.
You had
Code:
With temp
    .Range("J2").PasteSpecial xlPasteValues
LR3 = temp.Cells(Rows.Count, 10).End(xlUp).Row
    .Range("Z2:Z" & LR3).Value = Range("K2:K" & LR3).Value
    .Range("K2:K" & LR3).Value = Range("J2:J" & LR3).Value
End With
rather than
Code:
With temp
    .Range("J2").PasteSpecial xlPasteValues
LR3 = temp.Cells(Rows.Count, 10).End(xlUp).Row
    .Range("Z2:Z" & LR3).Value = [COLOR=#ff0000].[/COLOR]Range("K2:K" & LR3).Value
    .Range("K2:K" & LR3).Value = [COLOR=#ff0000].[/COLOR]Range("J2:J" & LR3).Value
End With
note the . in red
 
Upvote 0
Your code does not have a period in front of the 2 ranges on the right of the = sign.
You had
Code:
With temp
    .Range("J2").PasteSpecial xlPasteValues
LR3 = temp.Cells(Rows.Count, 10).End(xlUp).Row
    .Range("Z2:Z" & LR3).Value = Range("K2:K" & LR3).Value
    .Range("K2:K" & LR3).Value = Range("J2:J" & LR3).Value
End With
rather than
Code:
With temp
    .Range("J2").PasteSpecial xlPasteValues
LR3 = temp.Cells(Rows.Count, 10).End(xlUp).Row
    .Range("Z2:Z" & LR3).Value = [COLOR=#ff0000].[/COLOR]Range("K2:K" & LR3).Value
    .Range("K2:K" & LR3).Value = [COLOR=#ff0000].[/COLOR]Range("J2:J" & LR3).Value
End With
note the . in red


Okay! Yes I didn't see that.
I added them and it works as it should, without the select.
Can I ask, what is the rule here? I know I've got this to work before as I had it, although I am not sure it was to a sheet that wasn't the active sheet.
So if I am on the active sheet and my WITH statement is only concerning the non active sheet, then I need the period. ?
Thanks
 
Upvote 0
Anything inside a With statement that starts with a . (period) belongs to the With, so both of the following are the same
Code:
Sheets("sheet1").Activate
With Sheets("Sheet2")
   .Range("A1").Value = .Range("B1").Value
End With

Sheets("Sheet2").Range("A1").Value = Sheets("Sheet2").Range("B1").Value
but if you have a range inside a with that doesn't start with a . it's working on the activesheet, so these 2 are the same
Code:
Sheets("sheet1").Activate
With Sheets("Sheet2")
   .Range("A1").Value = Range("B1").Value
End With

Sheets("Sheet2").Range("A1").Value = Sheets("Sheet1").Range("B1").Value
 
Last edited:
Upvote 0
Anything inside a With statement that starts with a . (period) belongs to the With, so both of the following are the same
Code:
Sheets("sheet1").Activate
With Sheets("Sheet2")
   .Range("A1").Value = .Range("B1").Value
End With

Sheets("Sheet2").Range("A1").Value = Sheets("Sheet2").Range("B1").Value
but if you have a range inside a with that doesn't start with a . it's working on the activesheet, so these 2 are the same
Code:
Sheets("sheet1").Activate
With Sheets("Sheet2")
   .Range("A1").Value = Range("B1").Value
End With

Sheets("Sheet2").Range("A1").Value = Sheets("Sheet1").Range("B1").Value

Perfect explanation thanks! I learn something new every day
 
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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