Deselect Tabs after VBA

Glasgowsmile

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

I've got the below VBA code which works but after it runs when I look at the tab it has the range use selected still. It's not a huge issue but I'm curious, how do I remove that selection on each tab?

VBA Code:
Private Sub CommandButton1_Click()
Application.DisplayAlerts = False
Dim crnt As Workbook
Dim source As Workbook

With Application.FileDialog(msoFileDialogOpen)
    .Filters.Clear
    .Filters.Add "Files", "*.xlsm"
    .AllowMultiSelect = False
    .Show

If .SelectedItems.Count > 0 Then
    Workbooks.Open .SelectedItems(1)
    Set source = ActiveWorkbook
    
    Worksheets("Dtar").Range("A3:K365").Copy
    Sheet1.Range("A3").PasteSpecial xlPasteValues
    
    Worksheets("Mtar").Range("A3:A365").Copy
    Sheet2.Range("A3").PasteSpecial xlPasteValues

    Worksheets("Mtar").Range("C3:H365").Copy
    Sheet2.Range("C3").PasteSpecial xlPasteValues

source.Close savechanges:=False

End If
End With

Sheets("Import_old").Activate
Range("A1").Select
Application.CutCopyMode = False
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
You can replace . . .

Code:
    Worksheets("Dtar").Range("A3:K365").Copy
    Sheet1.Range("A3").PasteSpecial xlPasteValues

with

VBA Code:
    With Worksheets("Dtar").Range("A3:K365")
        Sheet1.Range("A3").Resize(.Rows.Count, .Columns.Count).Value = .Value
    End With

Same the with the other two.

Hope this helps!
 
Upvote 0
Solution
You can replace . . .

Code:
    Worksheets("Dtar").Range("A3:K365").Copy
    Sheet1.Range("A3").PasteSpecial xlPasteValues

with

VBA Code:
    With Worksheets("Dtar").Range("A3:K365")
        Sheet1.Range("A3").Resize(.Rows.Count, .Columns.Count).Value = .Value
    End With

Same the with the other two.

Hope this helps!
My apologies, just getting back to this.

Not only did this work but wow, it is fast. I've never seen this approach but I think it will be my go-to moving forward.
 
Upvote 0
That's great, glad I could help. And thanks for the feedback.

Cheers!
 
Upvote 0
That's great, glad I could help. And thanks for the feedback.

Cheers!
Looking at this new approach, I'm wondering how to do offsets with it?

For example, I have this as the paste after copying data from another sheet.
VBA Code:
Sheet3.Range("B" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues

This is what I tried but it doesn't appear to work as expected though I get no errors.
VBA Code:
With Sheets("Cover").Range("Q36")
                 Sheet3.Range("B" & Rows.Count).End(xlUp).Offset(1).Resize(.Rows.Count, .Columns.Count).Value = .Value
            End With
 
Upvote 0
What do you mean by "it doesn't appear to work as expected"? Can you please be specific?
 
Upvote 0
What do you mean by "it doesn't appear to work as expected"? Can you please be specific?
Well, the first few times I tried it, it didn't look like it was pulling the data correctly but it does appear to work. Are there any issues with that code above for the offset?
 
Upvote 0
The only issue I see is that you'll get an error if the active sheet is not a worksheet. You should qualify the Rows reference with the worksheet name . . .

VBA Code:
Sheet3.Range("B" & Sheet3.Rows.Count). . .

However, since you are only interested in a single value, you can do it this way . . .

VBA Code:
With Sheet3
    .Range("B" & .Rows.Count).End(xlUp).Offset(1).Value = Sheets("Cover").Range("Q36").Value
End With
 
Upvote 0
The only issue I see is that you'll get an error if the active sheet is not a worksheet. You should qualify the Rows reference with the worksheet name . . .

VBA Code:
Sheet3.Range("B" & Sheet3.Rows.Count). . .

However, since you are only interested in a single value, you can do it this way . . .

VBA Code:
With Sheet3
    .Range("B" & .Rows.Count).End(xlUp).Offset(1).Value = Sheets("Cover").Range("Q36").Value
End With
Hi again, you mentioned this works for a single value but what if I'm trying to get multiples, like below?

It pulls L11 but not Q11 or S11.

VBA Code:
With Sheet5
            .Range("C" & Rows.Count).End(xlUp).Offset(1).Value = Sheets("Glance").Range("L11, Q11, S11").Value
         End With
 
Upvote 0
Unfortunately, you won't be able to do it that way. Also, you should qualify your Rows reference. Otherwise, you'll get an error if the active sheet is not a worksheet. Try it like this . . .

VBA Code:
Dim nextRow As Long

With Sheet5
   nextRow = .Range("C" & .Rows.Count).End(xlUp).Row + 1
   .Cells(nextRow, "C").Value = Sheets("Glance").Range("L11").Value
   .Cells(nextRow, "D").Value = Sheets("Glance").Range("Q11").Value
   .Cells(nextRow, "E").Value = Sheets("Glance").Range("S11").Value
End With

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,223,880
Messages
6,175,157
Members
452,615
Latest member
bogeys2birdies

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