I've got the following code
Sub test()
my_date = "01/09/2021 7:42"
Worksheets("Sheet1").Range("A1").Value = my_date
End Sub
This returns the following result
Why does it show in the cell "09/01/2021" rather than "01/09/2021" like on my original variable?
Fixed it this way
Sub test()
Dim my_date As Date
my_date = "01/09/2021 7:42"
Worksheets("Sheet1").Range("A1").Value = my_date
End Sub
Related
Suppose If i enter 209. It has to give me Jul 28th.
I could get day number from the date but in reverse. Can someone help me with this?
May be this code will help you
Sub Test()
Dim x
x = 209
MsgBox DateAdd("d", x - 1, "2018/1/1")
End Sub
Just add the number of days to the last day of last year.
=209 +"12/31/2017"
The following
=DATE(2018,1,209)
You can format in different ways the date returned e.g.
=TEXT(DATE(2018,1,209),"Mmm dd yyyy")
VBA and UDF:
Option Explicit
Public Sub test()
Debug.Print GetDate(209)
End Sub
Public Function GetDate(ByVal daynumber As Long) As String
GetDate = Format$(DateSerial(Year(Date), 1, daynumber), "Mmm dd yyyy")
End Function
I am trying to create a button that will hide rows based on the date the function reads.
My excel sheet is for meeting minutes, and based off column D, I will decide whether to hide or show the cell row. Now, column D contains dates of particular minutes, but occasionally contains a string called "Date" as part of a header row. For some reason, I cannot successfully write an if statement to skip said rows. Therefore, I am getting an error where my variable Current_Date is assigned the default VBA date value and my code crashes.
I made sure to format those particular cells as "Text" on the spread sheet, but it seems like my if statement still does not execute.
Can some one please provide me with some guidance.
Thank you in advance.
Private Sub CommandButton1_Click()
Dim x As Integer
Dim Current_Date As Date
Dim Last_Meeting_Date As Date
Dim default_date As Date
' Loop to hide old meeting minutes
For x = 150 To 1000
If Worksheets("Minutes").Cells(x,4) = "Date" Then
x = x + 1
End If
Current_Date = Worksheets("MINUTES").Cells(x, 4)
Last_Meeting_Date = Worksheets("HOME").Cells(19, 16)
If Current_Date < Last_Meeting_Date Then
Worksheets("MINUTES").Rows(x).Hidden = True
End If
Next x
End Sub
You might try:
Private Sub CommandButton1_Click()
Dim x As Integer
Dim Current_Date As Date
Dim Last_Meeting_Date As Date
Dim default_date As Date
Last_Meeting_Date = Worksheets("HOME").Cells(19, 16)
' Loop to hide old meeting minutes
For x = 150 To 1000
If Worksheets("Minutes").Cells(x,4) <> "Date" Then 'You might want to use IsDate()?
Current_Date = Worksheets("MINUTES").Cells(x, 4)
'original code is only able to hide row, this one can unhide them as well
Worksheets("MINUTES").Rows(x).Hidden = (Current_Date < Last_Meeting_Date)
End If
Next x
End Sub
I took a few liberties in reformatting and simplifying your code. I reordered the declarations, removed 'default date' since it was unused, changed your references to column '4' to 'D', reversed the logic of your if statement, and used a 'With' statement to prevent repeated specifications of your Worksheet.
Private Sub CommandButton1_Click()
Dim Last_Meeting_Date As Date
Last_Meeting_Date = CDate(Worksheets("HOME").Cells(19, 16).Value2)
Dim x As Long
Dim Current_Date As Date
' Loop to hide old meeting minutes
With Worksheets("MINUTES")
For x = 150 To 1000
If CStr(.Cells(x, "D").Value2) <> "Date" Then
Current_Date = CDate(.Cells(x, "D").Value2)
If Current_Date < Last_Meeting_Date Then .Rows(x).Hidden = True
End If
Next x
End With
End Sub
I want to write a Function Macro that calculates the days between two dates. It looks like I have that.
Function daysRem(today As Date, eoc As Date) As Integer
daysRem = Abs(DateDiff("d", today, eoc))
End Function
Now, I need to "Call" that function from a Sub to roughly estimate the number of weeks remaining. I'd like that to be in a message box. That's where I've hit about 2 hours of frustration. This has got to be simple, but I just can't figure out what to do.
Try this:
Private Sub DisplayDaysDiff()
Dim dtDate1 as Date
Dim dtDate2 as Date
dtDate1 =ThisWorkbook.Worksheets("Sheet1").Range("A2")
dtDate2=ThisWorkbook.Worksheets("Sheet1").Range("B2")
MsgBox "Days difference: " & CStr(daysRem(dtDate1, dtDate2)), vbInformation
End Sub
Call it from a button_click event
Sub Main
x = MyFunction(Param1)
End Sub
MyFunction(MyDate as Date)
MyFunction = DateDiff("ww", Date(), MyDate)
End Function
You assign the value to the function name. The inbuilt Date() function is todays date.
Functions return something. Subs don't. Macros ARE SUBS.
Functions return something.
So
Function Test
Test = 5
End Function
Sub Main
x = Test() + 5
End Sub
So x = 10.
Sub Main
Param1 = #2/2/2017#
Range("B4").Value = MyFunction(Param1)
End Sub
MyFunction(MyDate as Date)
MyFunction = DateDiff("ww", Date(), MyDate)
End Function
Date is both a data type and a function. One doesn't have brackets and one does.
I have a date in a particular cell (B3) - Date - 12/03/2016. I am trying to assign only the Year part of the date, i.e. 2016 to a variable. Is it possible?
you can use:
Dim yourvariable%
yourvariable = int(mid([B3],7,4))
or
Dim yourvariable%
yourvariable = int(Right([B3],4))
or
Dim yourvariable%
yourvariable = year(cdate([B3]))
Consider:
Sub dural()
Dim v As Variant
v = Split(Range("B3").Text, "/")(2)
MsgBox v
End Sub
In Excel using VBA, I need to set a variable to equal a list of all the dates between a start and end date (similar to equaling a range containing multiple values). The catch is only the start and end date are in a range, non of the values in between.
In SQL Server I've used the Sys.Columns table to generate a list of dates between two dates that are not actually stored on that table. Is there a way to do something similar here without having each date between the start and end date written somewhere? I googled for a couple hours and didn't find anything on how to do this.
What I'm attempting to do is have a variable I can do a For Each loop on. So for each date I will check if it exists in another worksheet, if it does nothing will happen, if it does not it will be added.
I've tried:
Dim DatesInSettings As Date
DatesInSettings = StartDate To EndDate
For Each Date In DatesInSettings
'Insert commands here
Next DatesInSetting
But that clearly isn't the answer. Help?
This searches Sheet2 for dates between the start date and end dates on Sheet1 - in cells A1 and B1:
Sub RunDates()
Dim StartDate As Date
Dim EndDate As Date
Dim i As Date
StartDate = Sheet1.Range("A1")
EndDate = Sheet1.Range("B1")
For i = StartDate To EndDate
If WorksheetFunction.CountIf(Sheet2.Range("A1:A5"), i) > 0 Then
Debug.Print i; "- date found"
Else
Debug.Print i; "- date not found"
End If
Next i
End Sub
The following subroutine calls a dictionary that will store all the dates between two given endpoints. Then it uses a simple existence comparison to check if the dates on your list is inside the dictionary's items. If it's not, it's going to print them out as not in the list.
Modify accordingly to suit your needs. ;)
CODE:
Sub GetListOfDates()
Dim StartDate As Date, EndDate As Date
Dim DictOfDates As Object, DateToCheck As Variant, ListOfDates As Variant
Dim Iter As Long
Set DictOfDates = CreateObject("Scripting.Dictionary")
StartDate = "12/31/2013"
EndDate = "01/15/2014"
For Iter = StartDate + 1 To EndDate - 1
With DictOfDates
If Not .Exists(Iter) Then
.Add Iter, Empty
End If
End With
Next Iter
'--Print them somewhere.
'Range("A1").Resize(DictOfDates.Count, 1).Value = Application.Transpose(DictOfDates.Keys)
ListOfDates = Range("B1:B15").Value
For Each DateToCheck In ListOfDates
If Not DictOfDates.Exists(DateToCheck) Then
Debug.Print Str(DateToCheck) + " is not in the list!" '--Or whatever action you want.
End If
Next DateToCheck
Set DictOfDates = Nothing
End Sub
Let us know if this helps. :)
I solved it with a vector.
I hope it helps
Sub Dates_Vector()
Public Dates() As Date
ReDim Dates(End_Dat - Start_Date)
For x = 0 To End_Dat - Start_Date
Dates(x) = Dat_Ini + x
Next x
For Each Date In Dates
'Insert commands here
Next Date
End Sub