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

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
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,224,936
Messages
6,181,850
Members
453,068
Latest member
DCD1872

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