Showing posts with label Macro. Show all posts
Showing posts with label Macro. Show all posts

Thursday, June 18, 2020

Conditional Formatting periodically using VBA macro


Sub conditionsuperfinal()
Sheets("conditionalformattingvba").Select
Dim dict As Object 'Declare a generic Object reference
Set dict = CreateObject("Scripting.Dictionary") 'Late Binding of the Dictionary
Range("T2").Select
While ActiveCell.Value <> ""
Dim key, val
key = ActiveCell.Value: val = ActiveCell.Offset(0, 1).Address
'Add item to VBA Dictionary
If Not dict.Exists(key) Then
    dict.Add key, val
End If
ActiveCell.Offset(1, 0).Select


Wend

'Debug.Print dict.Count 'Result: 1
'
'Debug.Print dict.Exists("North")
'For Each key In dict.Keys
'   Debug.Print key
'Next key
'
''Print all items
'For Each val In dict.Items
'   Debug.Print val
'Next val


'copy format of cells where condition matches
'Dim tempaddress As String
'Range("V2").Select
'tempaddress = Selection.Address
'For Each val In dict.Items
'
'Range(val).Select
'Selection.Copy
'Range(tempaddress).PasteSpecial Paste:=xlPasteFormats
'tempaddress = ActiveCell.Offset(1, 0).Address
'
'Next val
'Application.CutCopyMode = False

Sheets("conditionalformattingvba").Select
Dim workingrange, cell As Range
Range("I2").CurrentRegion.Select
Set workingrange = Selection
Selection.ClearFormats
For Each cell In workingrange
For Each key In dict.Keys
If InStr(1, cell.Value, key, vbTextCompare) > 0 Then
Range(dict(key)).Select
Selection.Copy
cell.PasteSpecial Paste:=xlPasteFormats
Exit For

Else

cell.ClearFormats
End If
Next key
'Debug.Print cell.Address
Next cell


'Dispose of VBA Dictionary
Set dict = Nothing
Application.CutCopyMode = False



End Sub




Sub formatandclear()


Sheets("conditionalformattingvba").Select
Dim workingrange, cell As Range
Range("I2").CurrentRegion.Select
Set workingrange = Selection
'MsgBox (Selection.Rows.Count)
'MsgBox (Selection.Columns.Count)
Dim startrow, startcolumn As Integer
startrow = Range("I2").Row
'MsgBox (startrow)
startcolumn = Range("I2").Column
'MsgBox (startcolumn)
Dim signal As Boolean
signal = False
If (Cells(startrow, startcolumn).Interior.ColorIndex <> 2) And (Cells(startrow, startcolumn).Font.ColorIndex <> 1) Then
For i = 1 To Selection.Rows.Count
For j = 1 To Selection.Columns.Count



    
    Cells(i + (startrow - 1), j + (startcolumn - 1)).Interior.ColorIndex = 2
    Cells(i + (startrow - 1), j + (startcolumn - 1)).Font.ColorIndex = 1
    



Next j


Next i

Else
conditionsuperfinal
End If

End Sub

Sub start_time()
Application.OnTime Now + TimeValue("00:00:10"), "formatandclear"
End Sub

Sub end_time()
Application.OnTime Now + TimeValue("00:00:05"), "formatandclear", False
End Sub

Sub BlinkCell()

'Dim CellToBlink As Range
'Set CellToBlink = Range("H1")

Do While (True)

formatandclear
Application.Wait (Now + TimeValue("0:00:01"))

formatandclear

Application.Wait (Now + TimeValue("0:00:01"))


DoEvents

If Range("D1").Value = 1 Then Exit Do

Loop



End Sub

Friday, October 14, 2016

VBA Macro to find next workday after variable number of days considering old indian style week off(alternate saturday off and fixed sunday off),VBA Teacher Sourav,Kolkata 09748184075


Sub test()
Sheets(1).Select

Dim exampleDate As Date
Dim initiandate As Date
Dim enddate As Date

exampleDate = DateValue("10/5/2016")
initialdate = DateValue("10/5/2016")
'exampleDate = exampleDate + 1
'MsgBox (Day(exampleDate))
'MsgBox WeekdayName(3, True, vbMonday)
'Dim val As String
' Range("B1").Select
 '   ActiveCell.FormulaR1C1 = "=TEXT(RC[-1],""ddd"")"
  '  val = ActiveCell.Value
 ' MsgBox WeekdayName(Weekday(exampleDate), True, vbSunday)
Dim result As Integer
result = 0
Dim i, j As Integer
j = 21
i = 1
Dim check As Integer
check = 0

While i <= j
If WeekdayName(Weekday(exampleDate), True, vbSunday) = "Sun" Or WeekdayName(Weekday(exampleDate), True, vbSunday) = "Sat" Then
'MsgBox ("Sun")
check = 1
j = j + 1
i = i + 1
GoTo lastline
End If
exampleDate = exampleDate + 1
i = i + 1
'MsgBox (result)
lastline:
If check = 1 Then
exampleDate = exampleDate + 1
check = 0
End If

'MsgBox (exampleDate)
Wend

'MsgBox (exampleDate)
'MsgBox (exampleDate)
enddate = exampleDate

Dim sampledate As Date

Dim sampledatestr As String
sampledatestr = (CStr(Month(initialdate)) + "/" + CStr(1) + "/" + CStr(Year(initialdate)))
'MsgBox (sampledatestr)
sampledate = DateValue(sampledatestr)
Dim tempdate As Date
tempdate = sampledate
'MsgBox (tempdate)
'MsgBox (enddate)
'MsgBox (sampledate)
check = 0
For sampledate = tempdate To enddate
If WeekdayName(Weekday(sampledate), True, vbSunday) = "Sat" Then
If check = 1 Then
exampleDate = exampleDate - 1
check = 0

End If



End If
check = 1

Next sampledate

MsgBox (exampleDate)



End Sub