Border in different sheet

Cyril Beki

Board Regular
Joined
Sep 18, 2021
Messages
57
Office Version
  1. 2016
Platform
  1. Windows
How to apply border to different sheet after clicking on command button (active control) from different sheet? Here is the code i used
VBA Code:
Private Sub CommandButton1_Click()
With Sheets("GraphReport")
Dim LastRow As Long, LastCol As Long
  Cells.Borders.LineStyle = xlNone
  LastRow = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
  LastCol = Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
  With Range("E4", Cells(LastRow, LastCol))
    .BorderAround xlDouble
    .Rows.Borders(xlInsideHorizontal).LineStyle = xlDash
    .Rows.Borders(xlInsideVertical).LineStyle = xlContinuous
    .Columns.AutoFit
End With
End With
End Sub

The command button (active control) is in Sheet7(Dashboard) but i wish to apply the border to Sheet("GraphReport") once i clicked on the button from sheet7.
Although the code do the job but the problem is it will create border in Sheet7(Dashboard) as well, i only wanted it to be created in Sheet("GraphReport") once i clicked the command button. Thanks in advance
 

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).
You need to put a period infront of all the ranges & cells inside the With statement, otherwise it's looking at the active sheet.
VBA Code:
Private Sub CommandButton1_Click()
With Sheets("GraphReport")
Dim LastRow As Long, LastCol As Long
  .Cells.Borders.LineStyle = xlNone
  LastRow = .Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
  LastCol = .Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
  With .Range("E4", .Cells(LastRow, LastCol))
    .BorderAround xlDouble
    .Rows.Borders(xlInsideHorizontal).LineStyle = xlDash
    .Rows.Borders(xlInsideVertical).LineStyle = xlContinuous
    .Columns.AutoFit
End With
End With
End Sub
 
Upvote 0
Solution
You need to put a period infront of all the ranges & cells inside the With statement, otherwise it's looking at the active sheet.
VBA Code:
Private Sub CommandButton1_Click()
With Sheets("GraphReport")
Dim LastRow As Long, LastCol As Long
  .Cells.Borders.LineStyle = xlNone
  LastRow = .Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
  LastCol = .Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
  With .Range("E4", .Cells(LastRow, LastCol))
    .BorderAround xlDouble
    .Rows.Borders(xlInsideHorizontal).LineStyle = xlDash
    .Rows.Borders(xlInsideVertical).LineStyle = xlContinuous
    .Columns.AutoFit
End With
End With
End Sub
Thank you , it works
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,950
Messages
6,175,582
Members
452,653
Latest member
craigje92

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