VBA very simple Question - how to use VBA codes according to one specific sheet and not all entire workbook?

Lacan

Board Regular
Joined
Oct 5, 2016
Messages
200
Office Version
  1. 365
Platform
  1. Windows
Hi Guys, 😉👍

Hope all of you are fine!👌🍻
Would like your help in this question:

Have this 3 different codes below that work very fine but would like to use them in one specific sheet and not all entire workbook?
What kind of changes have to add in each one code?


Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("C20")) Is Nothing Then
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
Cells(20, 2).Value = Format(Date, "dd-mmm")
End If
End Sub


-----------------------------------------
Sub Linha()
'
' Linha Macro
'

'
Rows("24:24").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C24:S24").Select
With Selection
.Font.Bold = True
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
With Selection
.Font.Bold = True
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
With Selection.Font
.Color = -65536
.TintAndShade = 0
End With
Range("C24:S24").Select
End Sub


-------------------------------------
Sub Novo()
'
' Novo Macro
'

'
Rows("20:25").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("B20").Select
Selection.NumberFormat = "dd-mmm"
Range("B24").Select
ActiveCell.FormulaR1C1 = "OBJECTIVO"
Range("C20:S20").Select
With Selection
.Font.Bold = True
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
With Selection
.Font.Bold = True
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Selection.AutoFill Destination:=Range("C20:S24"), Type:=xlFillDefault
Range("C20:S24").Select
Range("B20:B24").Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("B20:S24").Select
With Selection.Font
.Name = "Calibri"
.Size = 12
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
With Selection.Font
.Color = -65536
.TintAndShade = 0
End With
Range("C20:S24").Select
End Sub

Customer File.xlsb
ABCDEFGHIJKLMNOPQRST
1
2BioUn. Tipo
3RANK
4Coordenador
5Limpezas
6Horário de Visita
7Observações
8
9N.º
10
11
12
13
14
15
16
17
18
19ANOTE O QUE DE RELEVANTE ACONTECEU NA PRESENTE ENTREVISTA E ESCREVA A FORMA COMO VAI COMEÇAR A PRÓXIMA ENTREVISTA DE VENDAS.
20
21
22
23
24OBJECTIVO
25
26
ORGANIZATION
Cells with Data Validation
CellAllowCriteria
B2List=#REF!$A$3#
C2:G2Any value
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("C20")) Is Nothing Then
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
Cells(20, 2).Value = Format(Date, "dd-mmm")
End If
End Sub
Should be in the worksheet module of the actual worksheet

Both the other codes should have the below as the first line as you are selecting cells

Rich (BB code):
Sheets("Your_Sheet_Name").Activate

I assume Your_Sheet_Name should be ORGANIZATION
 
Upvote 0
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("C20")) Is Nothing Then
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
Cells(20, 2).Value = Format(Date, "dd-mmm")
End If
End Sub
Should be in the worksheet module of the actual worksheet

Both the other codes should have the below as the first line as you are selecting cells

Rich (BB code):
Sheets("Your_Sheet_Name").Activate

I assume Your_Sheet_Name should be ORGANIZATION

Dear @MARK858

Thank you very much for the help.
Let me try it first then give the feedback in return.

Until then please confirm - in the same worksheet module it is possible to write different VBA Codes?

Thank you very much!!! 💪👍👍🍺🍺
 
Upvote 0
Until then please confirm - in the same worksheet module it is possible to write different VBA Codes?
You should really keep to using worksheet event codes in a worksheet module and keep regular subs in a regular module and qualify the ranges with the worksheet.

You can only have one specific type of of worksheet event code in the same worksheet module (you can have have multiple different types of worksheet event code in the same worksheet module)
 
Upvote 0

Forum statistics

Threads
1,221,544
Messages
6,160,428
Members
451,645
Latest member
androidmj

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