Select Previous Cell

Glasgowsmile

Active Member
Joined
Apr 14, 2018
Messages
280
Office Version
  1. 365
Platform
  1. Windows
Hello,

Have the following code which works except it highlights the entire area it posted the data after doing the macro instead of just staying where the cell originally was.

How can I get it to stay on cell B2 after running this code?

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

   If Target.CountLarge > 1 Then Exit Sub
   If Target.Value = "" Then Exit Sub
   If Not Intersect(Target, Range("B2:B20")) Is Nothing Then
      Sheets(Target.Value).Range("A1:U50").Copy Range("L1").Value


End Sub
 
I may be misunderstanding your comments on that. I thought with that formula approach, you were saying to use that in lieu of VBA (that you were just using VBA because you didn't think the formula would adjust itself, which it will - but only on the worksheet formula, not in VBA code).

Regarding the Paste Special code, did you see footoo's comments?
Did you give that a try?

BTW, I have been in-and-out of training the paste two days, which is why it sometimes takes a while for me to reply.

I don't know how to break that pastespecial into two lines exactly, so no I didn't.

So I found a work around -- the VBA formula is pulling that Average formula into the cells but it's showing up blank even though it's not blank on the actual spreadsheet and it's referencing the correct sheet/cells, so I don't understand why it's blank.

I moved the average formula to another section of that sheet itself and then changed it to =BYHAR!Q39 instead, so instead of pulling the formula to make the average the VBA code is pulling that formula which works. If you could explain why the previous method didn't work that would be great though.
 
Upvote 0

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I don't know how to break that pastespecial into two lines exactly, so no I didn't.
Breaking this line:
Code:
      Sheets(Target.Value).Range("A1:U50").Copy Range("M1").PasteSpecial Paste:=xlPasteValues
up into two lines would look something like this:
Code:
      Sheets(Target.Value).Range("A1:U50").Copy 
      Range("M1").PasteSpecial Paste:=xlPasteValues
      Application.CutCopyMode = False
the VBA formula is pulling that Average formula into the cells but it's showing up blank even though it's not blank on the actual spreadsheet and it's referencing the correct sheet/cells, so I don't understand why it's blank.
I am sorry, but I really am not following what you are saying here.
 
Last edited:
Upvote 0
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count > 1 Or Target(1) = "" Then Exit Sub
If Not Intersect(Target, Range("C2:C18")) Is Nothing Then
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        Sheets(Target.Value).Range("A1:U50").Copy
        Range("M1").PasteSpecial Paste:=xlPasteValues
        Range("M1").PasteSpecial Paste:=xlPasteFormats
        Target.Select
        .CutCopyMode = False
        .EnableEvents = True
    End With
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,244
Messages
6,170,976
Members
452,372
Latest member
Natalie18

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