VBA - No Select or Activate

The_Rock

Board Regular
Joined
Jul 2, 2007
Messages
174
Hi Folks
I'm trying to delve deeper into VBA and am trying to run the following macro without resorting to 'Select' or 'Activate'.

What I am trying is to get the macro to loop thru two different worksheets, enter a formula, then copy & paste it down to the length of the range.
What happens is that it enters the formula in the correct sheet, but when it has to copy and paste, it does so on the current page!
Appreciate your wisdom on this.

Code:
Sub Concat()

''Fill Concats in following sheets
    
    Dim ws As Worksheet, rng As Range
    Dim cpw As Worksheet
    
    Set ws = Sheets("Missing Details - MPR")
    Set cpw = Sheets("CHIP Pull (Website)")
    
  'Missing Details - MPR
    Set rng = ws.Range("A3")
    Range("A3") = "=RC[1]&RC[2]&RC[3]"
    rng.Copy Destination:=Range("A3:A" & Range("C" & Rows.Count).End(xlUp).Row)
    Set rng = ws.Range("A4:A" & Range("C" & Rows.Count).End(xlUp).Row)
    rng.Copy
    rng.PasteSpecial (xlPasteValues)
  'CHIP Pull (Website)

    Set rng = cpw.Range("A2")
    Range("A2") = "=RC[2]&RC[3]"
    rng.Copy Destination:=Range("A2:A" & Range("C" & Rows.Count).End(xlUp).Row)
    Set rng = cpw.Range("A3:A" & Range("C" & Rows.Count).End(xlUp).Row)
    rng.Copy
    rng.PasteSpecial (xlPasteValues)
End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
"rng.Copy Destination:=Range("A2:A" & Range("C" & Rows.Count).End(xlUp).Row)"

In your Destination statement you are not qualifying the Range. By "Qualifying", you need to tell VBA which worksheet your destination range is located in. Since you are copying from ws I would assuem that the destination worksheet would be cpw so:

Code:
rng.Copy Destination:=cpw.Range("A2:A" & Range("C" & Rows.Count).End(xlUp).Row)
Code:
 
Upvote 0
Try....untested !!
Code:
Sub Concat()

''Fill Concats in following sheets
    
    Dim ws As Worksheet, rng As Range
    Dim cpw As Worksheet, lr As Long
    
    Set ws = Sheets("Missing Details - MPR")
    Set cpw = Sheets("CHIP Pull (Website)")
  'Missing Details - MPR
    lr = ws.Cells(Rows.Count, "C").End(xlUp).Row
    With ws
    .Range("A3:A" & lr).Formula = "=A3&B3&C3"
    .Range("A4:A" & lr).Value = .Range("A4:A" & lr).Value
    End With
  'CHIP Pull (Website)
    lr = cpw.Cells(Rows.Count, "C").End(xlUp).Row
    With cpw
    .Range("A2:A" & lr).Formula = "=B2&C2"
    .Range("A3:A" & lr).Value = .Range("A3:A" & lr).Value
    End With
End Sub
 
Upvote 0
JLGWhiz - unfortunately that did not work, as it still pasted to the current sheets.

Michael M - worked like a charm and less coding needed to. I've learnt something new :)

Many thanks to both of you!!
 
Upvote 0

Forum statistics

Threads
1,215,833
Messages
6,127,156
Members
449,366
Latest member
reidel

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