problem with directing my method of range to another worksheet

kbishop94

Active Member
Joined
Dec 5, 2016
Messages
458
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
It refuses to draw the borders on the Worksheet that I am attempting to direct it to... it just keeps drawing the border on my main worksheet(?)

Code:
Private Sub cmdCopyNames_Click()

Dim kCol As Long
    kCol = Sheets("Sheet13").UsedRange.Rows.Count + 1

Dim ws As Worksheet
Set ws = Worksheets("Sheet13")

ActiveWorkbook.Worksheets("Sheet13").Activate

    With ws
        Range(Cells(2, 2), Cells(kCol, 2)).Borders.LineStyle = xlContinuous
        Range(Cells(2, 2), Cells(kCol, 2)).Borders.Weight = xlThin
        Range(Cells(2, 2), Cells(kCol, 2)).Borders.Color = 8210719
    End With
    
End Sub

I've tried putting the "ActiveWorkbook.Worksheets" part of the code in various places, but I dont think it should even matter if its in there or not since I had already set 'ws' as the Worksheet I have in my range (but I was desperate and trying everything lol).

I also tried this code, but it just gives me an error: (Method of 'Range' of object '_Worksheet' failed)

Code:
Range(Worksheets("Sheet13").Cells(2, 2), Worksheets("Sheet13").Cells(kCol, 2)).Borders.LineStyle = xlContinuous

Help! Please and Thank you.
icon14.png
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
I got it!! Finally found a method that worked...

I had to change this:

Code:
Range(Cells(2, 2), Cells(kCol, 2)).Borders.LineStyle = xlContinuous

to this:

Code:
.Range(.Cells(2, 2), .Cells(kCol, 2)).Borders.LineStyle = xlContinuous

So... if someone wants to chime in and tell me WHY this is the way it is, I would super appreciate it :) I just discovered this whilst googling all the possible solutions I could come up with... finally hit the right one lol
 
Last edited:
Upvote 0
So... if someone wants to chime in and tell me WHY this is the way it is, I would super appreciate it :)
Because your code is in a Worksheet Module, not a standard module.
In a worksheet module, any range reference not specifically qualified to another worksheet will refer to the worksheet whose module the code is in, no matter what sheet is active.

In any case your
With ws


End with

only applies to what is within, if those lines within are prefixed with "."
 
Last edited:
Upvote 0
Because your code is in a Worksheet Module, not a standard module.
In a worksheet module, any range reference not specifically qualified to another worksheet will refer to the worksheet whose module the code is in, no matter what sheet is active.

In any case your
With ws


End with

only applies to what is within, if those lines within are prefixed with "."


Awesome... thank you for that explanation. That makes perfect 100% sense now.
icon14.png
 
Upvote 0

Forum statistics

Threads
1,216,119
Messages
6,128,946
Members
449,480
Latest member
yesitisasport

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