Sub RunMacro() ' Check if the workbook is currently being modified If Worksheets(1).Range("A1").Value = True Then MsgBox "The workbook is currently being modified by another user. Please try again later." Exit Sub End If ' Set the lock to True Worksheets(1).Range("A1").Value = True Private Sub Worksheet_Change(ByVal Target As Range) Dim ws As Worksheet Set ws = Me ' Check if the change is happening in the target column (e.g., Column AE) If Target.Column = 31 Then Dim Path As String Dim NewFolder As String ' Specify the path to the CD Data folder Path = "E:\Bulding Control Data 30-08-2022\Phase-1\CD All Drawings\" ' Get the new folder name from the cell value NewFolder = Path & Target.Value ' Create the new folder If Len(Dir(NewFolder, vbDirectory)) = 0 Then MkDir NewFolder End If End If ' Check if the change is in column M, N, O, or P If Not Intersect(Target, ws.Columns("M:P")) Is Nothing Then Dim cell As Range ' Loop through each cell in the intersected range For Each cell In Intersect(Target, ws.Columns("M:P")) With cell.EntireRow ' Default row color to white .Interior.Color = RGB(255, 255, 255) ' #FFFFFF ' Check the conditions and set the row color accordingly If ws.Cells(cell.Row, "M").Value = 1 Then .Interior.Color = RGB(255, 255, 0) ' #FFFF00 ElseIf ws.Cells(cell.Row, "N").Value = 1 Then .Interior.Color = RGB(255, 0, 0) ' #FF0000 ElseIf ws.Cells(cell.Row, "O").Value = 1 Then .Interior.Color = RGB(146, 208, 80) ' #92D050 End If ' Additional condition for columns O and P both having the value of 1 If ws.Cells(cell.Row, "O").Value = 1 And ws.Cells(cell.Row, "P").Value = 1 Then .Interior.Color = RGB(255, 255, 255) ' #FFFFFF End If ' Additional condition for columns M, N, O & P not having the value of 1 or being blank If (ws.Cells(cell.Row, "M").Value <> 1 And ws.Cells(cell.Row, "N").Value <> 1 And ws.Cells(cell.Row, "O").Value <> 1 And ws.Cells(cell.Row, "P").Value <> 1) Or _ (IsEmpty(ws.Cells(cell.Row, "M").Value) And IsEmpty(ws.Cells(cell.Row, "N").Value) And IsEmpty(ws.Cells(cell.Row, "O").Value) And IsEmpty(ws.Cells(cell.Row, "P").Value)) Then .Interior.Color = RGB(255, 255, 255) ' #FFFFFF End If End With Next cell End If End Sub Sub MultiColumnFilter() Dim rng As Range Dim criteriaRange As Range Dim i As Integer ' Set the range where you want to apply the filter Set rng = ActiveSheet.Range("A2:AH9999") ' Set the range for filter criteria Set criteriaRange = ActiveSheet.Range("D1:F1") ' Turn off any existing AutoFilters ActiveSheet.AutoFilterMode = False ' Apply the AutoFilter For i = 1 To 3 ' Check if the criteria cell is not empty If Not IsEmpty(criteriaRange.Cells(1, i).Value) Then If i = 1 Then rng.AutoFilter Field:=4, Criteria1:=criteriaRange.Cells(1, i).Value Else rng.AutoFilter Field:=i + 3, Criteria1:=criteriaRange.Cells(1, i).Value End If End If Next i End Sub Private Sub Workbook_Open() Application.OnKey "^+m", "MultiColumnFilter" End Sub ' Set the lock to False Worksheets(1).Range("A1").Value = False End Sub
Write, Run & Share VB.net code online using OneCompiler's VB.net online compiler for free. It's one of the robust, feature-rich online compilers for VB.net language, running on the latest version 16. Getting started with the OneCompiler's VB.net compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as VB.net
. OneCompiler also has reference programs, where you can look for the sample code to get started with.
OneCompiler's VB.net online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample VB.net program which takes name as input and prints hello message with your name.
Public Module Program
Public Sub Main(args() As string)
Dim name as String = Console.ReadLine() ' Reading input from STDIN
Console.WriteLine("Hello " & name) ' Writing output to STDOUT
End Sub
End Module
Visual Basic is a event driven programming language by Microsoft, first released in the year 1991.
Variable is a name given to the storage area in order to identify them in our programs.
Simple syntax of Variable declaration is as follows
Dim variableName [ As [ New ] dataType ] [ = initializer ]
variableName = value
If condition-expression Then
'code
End If
If(conditional-expression)Then
'code if the conditional-expression is true
Else
'code if the conditional-expression is false
End If
If(conditional-expression)Then
'code if the above conditional-expression is true
Else If(conditional-expression) Then
'code if the above conditional-expression is true
Else
'code if the above conditional-expression is false
End If
If(conditional-expression)Then
'code if the above conditional-expression is true
If(conditional-expression)Then
'code if the above conditional-expression is true
End If
End If
Select [ Case ] expression
[ Case expressionlist
'code ]
[ Case Else
'code ]
End Select
For counter [ As datatype ] = begin To end [ Step step ]
'code
[ Continue For ]
'code
[ Exit For ]
'code
Next [ counter ]
For Each element [ As datatype ] In group
'code
[ Continue For ]
'code
[ Exit For ]
'code
Next [ element ]
While conditional-expression
'Code
[ Continue While ]
'Code
[ Exit While ]
'Code
End While
Do { While | Until } conditional-expression
'Code
[ Continue Do ]
'Code
[ Exit Do ]
'Code
Loop
Do
'Code
[ Continue Do ]
'Code
[ Exit Do ]
'Code
Loop { While | Until } conditional-expression
Procedure is a sub-routine which contains set of statements. Usually Procedures are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
Procedures are of two types.
Functions return a value when they are called.
[accessModifiers] Function functionName [(parameterList)] As returnType
'code
End Function
Sub-procedures are similar to functions but they don't return any value.
Sub ProcedureName (parameterList)
'Code
End Sub