Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS 

Excel-VB : Fill a cell range with current date

Sub FillDate()
    ' Do an operation on a range of columns
    Range("C1", "D4") = Now
End Sub

Calculate last day of current Month

'Determines what the next month is based on today and subtracts 1 day from first day of next month.

'VB.NET
Dim NextMonth As Integer
Dim RptYear As Integer
'Determine next month
NextMonth = DatePart(DateInterval.Month, DateAdd(DateInterval.Month, +1, today))
'Determine the year of the next month, in case you are going from Dec to Jan
RptYear = DatePart(DateInterval.Year, DateAdd(DateInterval.Month, +1, today))
'Subtract 1 day from the first day of next month to get this months last day
Return DateAdd(DateInterval.Day, -1, DateValue(NextMonth.ToString & "/1/" & RptYear.ToString))

Calculate Last Day of Last Month

VB/VBA/VB.NET one-liner to calculate the end of last month. Useful for SSRS/RDL Expressions and Excel/Office Formulas. Note that it does not use string parsing, which can cause localization problems.


DateAdd("d", -1.0 * DatePart("d", Today), Today)

Calculate First Day of Current Month

VB/VBA/VB.NET one-liner to calculate the start of the current month. Useful for SSRS/RDL Expressions and Excel/Office Formulas. Note that it does not use string parsing, which can cause localization problems.

DateAdd("D", -1.0 * DatePart("D", Today) + 1, Today)

Calculate First Day of Last Month

VB/VBA/VB.NET one-liner to calculate the start of the previous month. Useful for SSRS/RDL Expressions and Excel/Office Formulas. Note that it does not use string parsing, which can cause localization problems.

DateAdd("D", -1.0 * DatePart("D", Today) + 1, DateAdd("m", -1, Today))

Email User Control VB .NET

Save as an .ascx file and insert into your project.
Set properties via the properties window.
Includes the form, code, validation, and css.

<%@ Control Language="VB" ClassName="Email" %>
<%@ Import Namespace="System.Net.Mail" %>



<script runat="server">
    Public Property Email() As String
        Get
            Return recipientEmail
        End Get
        Set(ByVal value As String)
            recipientEmail = value
        End Set
    End Property


    Public Property Host() As String
        Get
            Return mhost
        End Get
        Set(ByVal value As String)
            mhost = value
        End Set
    End Property


    Public Property Port() As String
        Get
            Return mport
        End Get
        Set(ByVal value As String)
            mport = value
        End Set
    End Property


    Public Property Message() As String
        Get
            Return sentMessage
        End Get
        Set(ByVal value As String)
            sentMessage = value
        End Set
    End Property
    
    Dim recipientEmail As String
    Dim mhost As String
    Dim mport As Integer
    Dim sentMessage As String
    Dim client As New Net.Mail.SmtpClient()

    Protected Sub btnSendMail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSendMail.Click

        client.Host = Host
        client.Port = Port
        client.Send(txtSenderEmail.Text, recipientEmail, txtSubject.Text, txtMessage.Text)
        lblMessage.Text = sentMessage
    End Sub
</script>

<style type="text/css">
  label
  {
   	   float: left;
   	   width:10em;
   	   text-align:right;
   	   clear:left;
   	   margin-right: 7px;
   	   font-family: Tahoma, Sans-Serif;
   	   font-size:12px;
   	   font-weight:bold;
  	    padding:4px;
   		background:#FFFFFF;
   		color:#333333;
  }
  
  .validate
  {
    font-family: Tahoma, Sans-Serif;
   	font-size:12px;
  }
  
  </style>
  
<label>Email:</label><asp:TextBox ID="txtSenderEmail" runat="server" Width="375px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtSenderEmail"
    ErrorMessage="Required!" CssClass="validate"></asp:RequiredFieldValidator><br />

<label>Subject:</label><asp:TextBox ID="txtSubject" runat="server" Width="375px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtSubject"
    ErrorMessage="Required!" CssClass="validate"></asp:RequiredFieldValidator><br />
<label>Message:</label><asp:TextBox ID="txtMessage" runat="server" TextMode="MultiLine"
        Height="160px" Width="375px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtMessage"
    ErrorMessage="Required!" CssClass="validate"></asp:RequiredFieldValidator><br />
<label><asp:Label ID="lblMessage" runat="server"></asp:Label></label><asp:Button ID="btnSendMail"
        runat="server" Text="Send" />

work days between two dates without cycling through dates

I was thinking about how to optimize figuring out the work days between two dates and came up with this function. It doesn't take into account holidays. You would have to take out the workdays for holidays from the number if you want to take into account holidays, but that should be easy enough.

4GL Version
 
# workdays
# returns the number of working days between two dates
FUNCTION workdays( dt_begin, dt_end )

DEFINE
   dt_begin             DATE,
   dt_end               DATE,
   dt_first_sunday      DATE,
   dt_last_saturday     DATE,
   int_workdays         INTEGER

   # get first sunday
   LET dt_first_sunday = dt_begin + ((7 - WEEKDAY(dt_begin)) MOD 7)

   # get last saturday
   LET dt_last_saturday = dt_end + ((-1 * (WEEKDAY(dt_end) + 1)) MOD 7)

   # get work weeks between first sunday and last saturday
   LET int_workdays = (((dt_last_saturday - dt_first_sunday) + 1) / 7) * 5
   
   # if first sunday is not begin date
   IF dt_first_sunday <> dt_begin THEN

      # assume first sunday is after begin date
      # add workdays from begin date to first sunday
      LET int_workdays = int_workdays + (6 - WEEKDAY(dt_begin))

   END IF

   # if last saturday is not end date
   IF dt_last_saturday <> dt_end THEN

      # assume last saturday is before end date
      # add workdays from last saturday to end date
      LET int_workdays = int_workdays + WEEKDAY(dt_end)  
  
   END IF

   # return working days
   RETURN int_workdays

END FUNCTION


VBA Version
' WorkDays
' returns the number of working days between two dates
Public Function WorkDays(ByVal dtBegin As Date, ByVal dtEnd As Date) As Long

   Dim dtFirstSunday As Date
   Dim dtLastSaturday As Date
   Dim lngWorkDays As Long

   ' get first sunday in range
   dtFirstSunday = dtBegin + ((8 - Weekday(dtBegin)) Mod 7)

   ' get last saturday in range
   dtLastSaturday = dtEnd - (Weekday(dtEnd) Mod 7)

   ' get work days between first sunday and last saturday
   lngWorkDays = (((dtLastSaturday - dtFirstSunday) + 1) / 7) * 5

   ' if first sunday is not begin date
   If dtFirstSunday <> dtBegin Then

      ' assume first sunday is after begin date
      ' add workdays from begin date to first sunday
      lngWorkDays = lngWorkDays + (7 - Weekday(dtBegin))

   End If

   ' if last saturday is not end date
   If dtLastSaturday <> dtEnd Then

      ' assume last saturday is before end date
      ' add workdays from last saturday to end date
      lngWorkDays = lngWorkDays + (Weekday(dtEnd) - 1)

   End If

   ' return working days
   WorkDays = lngWorkDays

End Function
« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS