draw borders vba excel on button with space - vba

Hi I'm new with VBA need some help here if possible. I am trying to make 3 buttons, each button draws a top-line and a bottom line(i'll provide the excel file too):
the first button draws inside of 5 rows a top and a bottom line.
the second button draws inside of 10 rows a top and a bottom line.
the third button draws inside of 20 rows a top and a bottom line.
What I'm trying to achieve:
every time I press button 1 to keep count if already has been drawn the borders, if I press twice in a row button 1 to keep count if I already have drawn the borders, and draw again after keeping a space of 2 rows in between....Same if I would've pressed Button1, then Button 2. Or button 3.
..I am new with VBA I would love some help....
ub Macro2()
'
' Macro2 Macro
'
'
Range("A13:BD23").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThick
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("A27").Select
ActiveWindow.SmallScroll Down:=12
Range("A27").Select
End Sub
Sub Macro1()
'
' Macro1 Macro
'
'
Range("A4:J8").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThick
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("C9").Select
End Sub
Sub Macro3()
'
' Macro3 Macro
'
'
Range("A26:P46").Select
ActiveWindow.SmallScroll Down:=-6
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThick
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThick
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("G34").Select
ActiveWindow.SmallScroll Down:=-15
End Sub
DL LINK:
https://mega.nz/#!sgkVATKQ!k-Nq5gpKf4NfW2afEM8wpg_T5RFqT6y2_iqH7lDTM40

Not sure I completely followed your description, but try this:
EDIT - added a way to reset the "last range" so you can start over.
Option Explicit
Sub DoFive()
DoBorders Range("A4:J8")
End Sub
Sub DoTen()
DoBorders Range("A13:BD23")
End Sub
Sub DoTwenty()
DoBorders Range("A26:P46")
End Sub
'this is called to reset the starting point to whatever is passed.
Sub ResetStart()
DoBorders Nothing
End Sub
Sub DoBorders(rng As Range)
Dim useRange As Range
Static lastRange As Range
'handle resetting the "last range"
If rng Is Nothing Then
Set lastRange = Nothing
Exit Sub
End If
If lastRange Is Nothing Then
Set useRange = rng
Else
Set useRange = lastRange.Cells(1).Offset(lastRange.Rows.Count + 2, 0) _
.Resize(rng.Rows.Count, rng.Columns.Count)
End If
Set lastRange = useRange 'save for next call
With useRange
.Borders.LineStyle = xlNone 'remove all borders
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThick
End With
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThick
End With
End With
End Sub

Related

Activate/Deactivate Hyperlinks

This is my first go at asking a question so apologies to all if I haven't given enough info or messed up in some other way!
I have a sheet with areas which are hidden until required by the user. These are called for with a button and another button to hide them all again when creating a new workbook. The hidden areas each contain a hyperlink (these take the user to other sheets in the workbook) in a text box (this allows the hyperlink to work when the sheet is protected). My problem is that the hyperlinks are still active and the cursor shows a hand when over these hyperlinks even when they are hidden. How can I deactivate a hyperlink while it is hidden using VBA or a macro? Any assistance will be very welcome. Thanks
The sections on the sheet are hidden/shown by code which changes the color to the same as the background I didn't want to close columns or rows as I wanted to maintain the header and size of the sheet.
All the coding works for what I have done but have spent a long time trying to find an answer to my question without success. The hidden sections are up to 12 blocks of data entry cells which will be used dependant on the users project requirements.
A sections of the code I have placed below are for making block 12 appear and then next routine is for hiding all the blocks except number 1. The next is a sub routine that I have for hiding the text box of block number 2. I decided to change from having the hyperlink in a cell to a text box to allow the sheet to be protected but still use the hyperlinks for view only purposes. I have added the code for the routine for hiding the text box and it was this I was experimenting with to disable the hyperlink. However the only bit of code I could find that looked like it might do the job was EnableEvents but I couldn't achieve what I wanted with that, so here I am.
I'm sure you can see that I am note a programmer I borrow bits of code and stick them together and play with it until it works but I've run out of bits to stick and time to play.
Sub AppearCompOp12()
'
' AppearCompOp12 Macro
'
'
Range("ar22:bd28").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
Range("AR22").Select
Selection.Font.Size = 16
With Selection.Font
.Color = -65536
.TintAndShade = 0
End With
End Sub
Sub ResetCapMSADashboard()
'
' ResetCapMSADashboard Macro
'
'
Range( _
"P6:AB12,AD6:AP12,AR6:BD12,AR14:BD20,AR22:BD28,AD14:AP20,AD22:AP28,P14:AB20,B14:N20,B22:N28,P22:AB28" _
).Select
With Selection.Font
.Color = RGB(217, 217, 217)
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.149998474074526
.PatternTintAndShade = 0
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.14996795556505
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.14996795556505
.Weight = xlThin
End With
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.14996795556505
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.14996795556505
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.14996795556505
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.14996795556505
.Weight = xlThin
End With
Range("BG2:BG26").Select
Selection.ClearContents
Range("J7").Select
End Sub
Sub RecolourTB2()
'
' Change text box & text to background colourbox
'
ActiveSheet.Shapes.Range(Array("TextBox 9")).Select
With Selection.ShapeRange.TextFrame2.TextRange.Font.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorBackground1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = -0.25
.Transparency = 0
.Solid
End With
Range("P10:W10").Select
ActiveSheet.Shapes.Range(Array("TextBox 9")).Select
EnableEvents = False
With Selection.ShapeRange.TextFrame2.TextRange.Font.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorBackground1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = -0.150000006
.Transparency = 0
.Solid
End With
Range("P9:W9").Select
End Sub

Select a range of cells, delete the empty ones, and then add border around remaining cells

I would like to use a macro that will look at a range of cells, delete out the empty rows, and then add a border around the remaining cells that actually has content. Here are two macros I have: One is for removing the empty cells, and the other is to add borders. As I mentioned, the issue is, I do not know how to tell Excel to only add a border around the cells that were left over after the Remove macro was completed. I would appreciate any help.
**Sub Remove()**
'
' Remove Macro
'
'
Range("B80:B95").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.EntireRow.Delete
ActiveWindow.SmallScroll Down:=-12
Range("B61:B77").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.EntireRow.Delete
ActiveWindow.SmallScroll Down:=-21
Range("B39:B58").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.EntireRow.Delete
ActiveWindow.SmallScroll Down:=-27
Range("B10:B28").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.EntireRow.Delete
ActiveWindow.SmallScroll Down:=-6
End Sub
and
**Sub Border()**
'
' Border Macro
'
'
Range("B7:K19").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
ActiveWindow.SmallScroll Down:=18
Range("B21:K74").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
ActiveWindow.SmallScroll Down:=54
Range("O76").Select
ActiveWindow.SmallScroll Down:=-81
End Sub
I assume your B to K columns will always remain constant. You basically just want to find the total "used" rows...
Dim cols As Integer, LastRow As Long, TestRow As Long
LastRow = 0
For cols = 2 to 11
TestRow = Cells(Rows.Count, cols).End(xlUp).Row
If TestRow > LastRow Then LastRow = TestRow
Next cols
Range("B7:K" & LastRow).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
ActiveWindow.SmallScroll Down:=18
Range("B21:K74").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
ActiveWindow.SmallScroll Down:=54
Range("O76").Select
ActiveWindow.SmallScroll Down:=-81
I really hate the use of .Select but I am not rewriting all your other code.

Row selection and putting border using VBA macros in excel

I Need to find the non empty rows in a sheet from row 13 and put a top thick border to the selected non empty rows till the last used rows of the sheet. from the column C i need to find the non empty row. I tried this code but it is not working. can u plz help me out
Sub rowfind3()
Dim cell As Range
Dim r1 As Range
For Each cell In ActiveSheet.Range("C:C")
If (cell.Value <> "") Then
Set r1 = Range("A" & ActiveCell.Row & ":AV" & ActiveCell.Row)
r1.Select
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
End If
Next cell
End Sub
In this code only the first row the border is displayed, however for the successive rows the border is not coming.
Also i tried another code for the above scenario, but the same first row is only the border is displayed.
Sub rowfind1()
'
' rowfind Macro
'
'
Dim r1 As Range
Dim lr As Variant
Dim i As Integer
lr = ActiveSheet.UsedRange.Rows.Count
i = 0
For i = 13 To lr - 11
If (Not (IsEmpty(Cells(i, 3).Value))) Then
Set r1 = Range("A" & ActiveCell.Row & ":AV" & ActiveCell.Row)
r1.Select
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
End If
Next i
End Sub
Firstly, no need to use selection. All that ends up doing is potentially confusing the code (as in this case). Secondly, theres no need to re-declare the range inside the loop. Thats what the loop is there for.
Here is how it should look:
Sub rowfind3()
Dim cell As Range
For Each cell In ActiveSheet.Range("C:C")
If (cell.Value <> "") Then
With cell.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With cell.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With cell.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With cell.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
End If
Next cell
End Sub
I would look at changing the Activesheet to reference the actual sheet you want it on and only look at the UsedRange as well to speed it up a bit, but that code will now at least get you there.
Addendum based on Comments clarification:
lr = ActiveSheet.Cells(ActiveSheet.Rows.Count, "C").End(xlUp).Row
With ActiveSheet.Range("C13:C" & lr)
With .Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With .Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
.Borders(xlInsideHorizontal).LineStyle = xlNone
End With
Did you consider to use Conditional Formatting? For example on Columns $A:$AV formula is =$A1<>"", in formatting choose borders.

Repeat the same calculation in multiple sheets

Task: Repeat an identical calculation in multiple sheets.
Background:
multiple sheets labelled by calendar date i.e. 01 04, 02 04, 03 04. These are three discrete sheet names meaning 1st April, 2nd April and 3 April. (actual workbook has all the days in the month).
Data has identical column headings, but the number of rows vary. In brief the data is a list of mastercard and visa transactions.
I want to get the total of column G (happens to contain the monetary transaction value) and only take the Visa transactions.
Result:
the code below does this fine and places the results on the same sheet merely offset by a few columns to the right hand side and highlights the value I need in red. (this is a recorded macro I completed)
Limitation and seeking advise:
1) improve code to repeat this for all sheets by a single click of a mouse button.
(as you will note, its about how to cycle through all the sheets within the same workbook rather than (at present) having to manually go into each sheet and run the macro.
thank you in advance
code is:
Sub sum_visa_trans_together()
'
' sum_visa_trans_together Macro
'
' Keyboard Shortcut: Ctrl+r
'
ActiveCell.Rows("1:1").EntireRow.Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$M$14").AutoFilter Field:=2, Criteria1:="V"
ActiveCell.Offset(0, 6).Columns("A:A").EntireColumn.Select
Selection.Copy
ActiveCell.Offset(0, 4).Range("A1").Select
ActiveSheet.Paste
ActiveCell.Rows("1:1").EntireRow.Select
Application.CutCopyMode = False
Selection.AutoFilter
ActiveCell.Offset(0, 11).Range("A1").Select
ActiveCell.FormulaR1C1 = "max"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "=MAX(C[-1])"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "=SUM(C[-1])"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "visa trans"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "=R[-2]C-R[-3]C"
ActiveCell.Select
With Selection.Font
.Color = -16776961
.TintAndShade = 0
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
End Sub
this wont repeat in the sheets you want because you are using active cell, you can replace active cell with something like this:
sheetname.cells(1,1).value
in this case you are geting the value of cell A1 wich is row=1,column=1 in the sheet named sheetname
the name of your sheet is not necesary the same in vba so chek your narmes in the vba project explorer.
for example you can try something like this(Im not sure exactly what you are trying to do but this will guide you):
Sub s()
For Each ws In Worksheets 'WS will loop trough all worksheets
Dim TargetCell As Range
Set TargetCell = ws.Cells(1, 2) ' in this case you will run this macro in
' the cell A2 of all your sheets
TargetCell.Rows("1:1").EntireRow.Select
Selection.AutoFilter
ws.Range("$A$1:$M$14").AutoFilter Field:=2, Criteria1:="V"
TargetCell.Offset(0, 6).Columns("A:A").EntireColumn.Select
Selection.Copy
TargetCell.Offset(0, 4).Range("A1").Select
ws.Paste
TargetCell.Rows("1:1").EntireRow.Select
Application.CutCopyMode = False
Selection.AutoFilter
TargetCell.Offset(0, 11).Range("A1").Select
TargetCell.FormulaR1C1 = "max"
TargetCell.Offset(1, 0).Range("A1").Select
TargetCell.FormulaR1C1 = "=MAX(C[-1])"
TargetCell.Offset(1, 0).Range("A1").Select
TargetCell.FormulaR1C1 = "=SUM(C[-1])"
TargetCell.Offset(1, 0).Range("A1").Select
TargetCell.FormulaR1C1 = "visa trans"
TargetCell.Offset(1, 0).Range("A1").Select
TargetCell.FormulaR1C1 = "=R[-2]C-R[-3]C"
TargetCell.Select
With Selection.Font
.Color = -16776961
.TintAndShade = 0
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Next
End Sub
Otherwise:
Sub WorksheetLoop()
Dim WS_Count As Integer
Dim I As Integer
' Set WS_Count equal to the number of worksheets in the active
' workbook.
WS_Count = ActiveWorkbook.Worksheets.Count
' Begin the loop.
For I = 1 To WS_Count
' Insert your code here.
' The following line shows how to reference a sheet within
' the loop by displaying the worksheet name in a dialog box.
MsgBox ActiveWorkbook.Worksheets(I).Name
Next I
End Sub
source: http://support.microsoft.com/kb/142126/en

Preventing reset of VBA variables from excel recompile

Private Sub CommandButton2_Click()
Dim TempVar As Integer
TempVar = NumNodes
NumNodes = NumNodes + 1
TempVar = NumNodes
Debug.Print "NumNodes + 1"
Call Node_Button_Duplication
Call Channel_Selection_Duplication
NumNodes = TempVar
Debug.Print "NumNodes = " & NumNodes 'Debug
Debug.Print "TempVar = " & NumNodes 'Debug
End Sub
Public Sub Channel_Selection_Duplication()
Range("Q8:S8").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
Range("Q8:S8").Select
ActiveCell.FormulaR1C1 = "Channel Usage Selection"
Range("Q8:S52").Select
Range("Q52").Activate
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Range("Q8:S8").Select
Selection.Interior.ColorIndex = 36
End Sub
Public Sub Node_Button_Duplication()
Worksheets("Topology").Shapes("CommandButton1").Select
Selection.Copy
Worksheets("Topology").Paste
Selection.ShapeRange.IncrementLeft 339#
Selection.ShapeRange.IncrementTop -12.75
End Sub
I'm trying to save the value of NumNodes (a global variable) before calling the 2 subroutines (Node_Button_Duplication and Channel_Selection_Duplication), the first subroutine called copies and pastes a command button in a spreadsheet. This, I believe, recompiles the VBA project and reset (all?) global variables.
I have tried to write to a cell and read back the value from the cell, but this did not work (essentially the same ideas as using a temp variable).
The above code, when run, causes both TempVar and NumNodes to be reset to 1 each run. I am wondering what the best way is to save the variable from being reset?
Try this
Option Explicit
Private Sub CommandButton2_Click()
Dim NumNodes as Long
NumNodes = Sheets("Temp").Range("A1").Value
NumNodes = NumNodes + 1
Sheets("Temp").Range("A1").Value = NumNodes
MsgBox "NumNodes = " & NumNodes
Call Node_Button_Duplication
Call Channel_Selection_Duplication
End Sub
Ensure that you have a sheet Called "Temp"
Now try it.