Date format is switching month and day - excel

Could you please someone can help me on how to change the date formatting cause i use the following code and instead of today's date it gives me august date. I mean it takes the date of my laptop (08-sep-2015) and it returns on the excel cell (09-Aug-2015).
Private Sub UserForm_Initialize()
'testing variable
'Dim LValue As String
'LValue = Format(#4/17/2004#, "Long Date")
'close testing variable
Me.tbDate = Date
'fill combobox
For Each cell In [cartridges]
Me.cmbCartridges.AddItem cell
Next cell
End Sub
The part that i exclude form the code is a test i made for a solution i found over the internet.
Also the text box that i want to present the date has the following code
ssheet.Cells(nr, 1) = CDate(Me.tbDate)

I found this and sorted my issue. Now everything its working like a charm.
Me.tbDate = Format(Date, "dd/mm/yyyy")

I resolved my issue with this code. I intentionally formated the cell the wrong way (excel default) and then formated it as I needed.
For j = 0 To LISTBOXNAME.ListCount - 1
LISTBOXNAME.List(j) = Format(DateValue(LISTBOXNAME.List(j)), "m/d/yyyy")
LISTBOXNAME.List(j) = Format(DateValue(LISTBOXNAME.List(j)), "dd.mm.yyyy")
Next j

Related

Filtering all but todays date

I'm trying to filter out all but todays date using a macro (for the first time)
I want to create a macro or two that will show only rows using the date in which it's viewed. I've tried using the below, but it hides all rows containing a date
Dim cell As Range
For Each cell In Range("A10:A1000")
If cell.Value <= Date Then
cell.EntireRow.Hidden = False
End If
Next
End Sub
Basically you don't need VBA to achieve this: you could use the filter functionality of Excel.
But if you want to do it via VBA this is a routine that does what you want:
Option Explicit
Public Sub hideRowsIfNotDate(rgToCheckDate As Range, dateToBeVisible As Date)
Dim c As Range
For Each c In rgToCheckDate.Cells
If IsDate(c.Value) Then 'just to be on the safe side if there is no date
If c.Value <> dateToBeVisible Then
'hide rows with different date
c.EntireRow.Hidden = True
Else
'show rows that match the date - just in case they were hidden before
c.EntireRow.Hidden = False
End If
End If
Next
End Sub
Advantage of this solution:
you can pass different dates or ranges, therefore you can reuse the sub for different scenarios
when you call the sub from your main code you simply know by reading the subs name what it is doing (w/o reading the code itself) - someone else (or you in 3 months) will appreciate that :-)
You will call the routine from your main code like this:
Public Sub test_hideRows()
Dim dateToBeVisible As Date
dateToBeVisible = Date '= today
Dim rgToCheck As Range
Set rgToCheck = ActiveSheet.Range("A10:A1000")
hideRowsIfNotDate rgToCheck, Date
End Sub
Have you tried AutoFilter? I assume that your data are stored in Rows("10:1000"), dates - in the column A and you have some headers in Rows(9). With that in mind:
Set Source = Rows("9:1000")
Field = Range("A:A").Column
Value = Format(Date, Range("A10").NumberFormatLocal)
Source.AutoFilter Field, Value
If you have a custom date format, put that instead of ...NumberFormatLocal

best way to select column based on current date (Excel VBA)

I have a small tracker program I am building in Excel VBA. I have a userform that I keep up throughout the day, inputting my tasks/data via an 'Add' button. At the end of the day, I click the 'Post' button, and it saves the data to my worksheets where appropriate.
Thought I had it finished and working correctly, but now apparently my sub to select the correct column based on the day's date is not working, and I'm not sure why, as it had been working perfectly throughout development.
This piece is vital, as my other functions to post the day's data rely on this. I've read a lot of other posts about how to do this (selecting a column based on current date), but none have explained why mine isn't working. Trying to become a better coder, and not just a copy/paste coder, so thought I would try asking here. Copy/Paste usually gets me into these messes, where I'm using tools/functions that work, but I don't know why, and can't troubleshoot/debug on my own.
My total project involves 5 worksheets, but this sub should only involve 2 of them. "Tasks" and "Data Tracker", both of which have a row of dates.
Below is the sub in question.
Public Sub currentDate()
'sub to assign current date to global values
Set rng - Range("H2:HZ2")
Set myDate = rng.Find(What:=Int(Date), LookIn:=xlFormulas)
End Sub
If I step through it, Date is pulling the correct date, and xlFormulas shows a value of -4123 (I don't even know if that matters)..
(UPDATE) so apparently, this morning, it decided to work perfectly. facepalm Any clues?
(UPDATE) so, per usual, I try adding features as I fix something else, so this took a bit more researching to solve, but #Super-Symmetry pointed me in the right direction! As noted in a comment down below, I changed my date headers in the two sheets to be more of a "start date + n" situation. Although his suggestion of using xlValue instead of xlFormula was on the right track, Find. was still having trouble with date vs serial. Ultimately this is what I got to work:
Public Sub currentDate()
'sub to assign current date to global values
'load the date range
Set rng = Worksheets("Tasks").Range("H2:HZ2")
'load the values in the range
dateArray = Range("H2:HZ2").Value
Dim day As Variant 'object to load dateArray
Dim loc As Integer 'matches date with cell location
'converting the date to serial
For Each day In dateArray
day = CLng(day)
loc = loc + 1
If day = Date Then 'we found the right column
Set myDate = rng(loc)
'selects the correct cell
If ActiveSheet.name = "Data Tracker" Then 'adjust the row
Cells(myDate.Row + 3, myDate.Column).Select
Else 'sheet must be Tasks
Cells(myDate.Row + 2, myDate.Column).Select
End If
Exit Sub
End If
Next
End Sub
It's not elegant, but it works.. please feel free to educate me if you have any cleaner ways to do this!
Try changing Int(Date) to CLng(Date)
Public Sub currentDate()
'sub to assign current date to global values
Dim rng As Range, myDate As Range
Set rng = Range("H2:HZ2")
Set myDate = rng.Find(What:=CLng(Date), LookIn:=xlValues)
End Sub

Userform that uses a Combobox with SUMIFS to sum data that meets requirements

I am currently working on my first VBA code and am very new to VBA. I am trying to essentially make something that will add my values in column "F" together so long as the date in column "K" occurred in 2018. Column "K" is formatted as a date in my sheet and I am having trouble getting it to work between dates. I have tried a lot of different things to make it work but here is what I have right now.
Private Sub date_box_Change()
Dim STrange As Date, ENDrange As Date, date_value As Date
date_value = date_box
STrange = date_value
ENDrange = DateAdd("d", 365, date_value)
Me.production_amount = Application.WorksheetFunction.SumIfs(Sheet2.Range("F1:F152"), _
Sheet2.Range("K1:K152"), ">=" & "STrange", Sheet2.Range("K1:K152"), "<=" & "ENDrange")
End Sub
Private Sub Userform_Initialize()
Me.date_box.AddItem CDate("1/1/2017")
Me.date_box.AddItem CDate("1/1/2018")
Me.date_box.AddItem CDate("1/1/2019")
Me.date_box.AddItem CDate("1/1/2020")
End Sub
Like I said I am completely new to VBA any help is much appreciated!

Looping through list of dates to display datediff

I am trying to create a macro that as well as many other things will loop through the dates in column and, compare it to the "due date" and show the amount of dates that is left
Unfortunately the macro that I wrote doesn't give the desired outcome and gives me some huge number instead.
my code for that part so far is:
Sub calcDays()
Dim dueDate As Date
Dim macroLastRow As Long
Dim macroWs As Worksheet
Set macroWs = ThisWorkbook.Worksheets("Macro")
macroLastRow = macroWs.Range("A" & Rows.Count).End(xlUp).Row
dueDate = Range("B1").Value
For i = 2 To macroLastRow
Cells(i, 2).Value = DateDiff("d", i, dueDate)
Next i
End Sub
This is what I get after I run the macro:
I hope that you guys will be able to advise! Thanks a mil in advance!
There is no value assigned to invoiceDate. You may replace it with Cells(i, 1).Value and that will do the trick.
I also would recommend you to switch on the setting "Require Variable Declaration". You may find the way to do it here in the answer.
Also, discard my edit to your post for problem to remain.

Getting Excel to Copy Data From One Cell to Another Depending on Date

Apologies in advance as this is my first time posting something on this site and am not the best at explain issues.
I have a spread sheet, this has production data such as meters daily, meters monthly etc. These values are updated by adding TAGS from a PLC using Rockwell VantagePoint Excel add-in (if your unfamiliar with this it shouldn't matter this part is not what I am struggling with)
I need I way to copy data from one cell to another cell on the same sheet at month end. Basically the Meters monthly field needs to copied into another cell at the end of the month to record meters run for that month. The monthly meters run resets back to 0 at the end of the month.
Basically I need to copy the value in J7 into the corresponding month in W column at the end of that month. If it could ignore the year that would be advantageous as I don't need it to keep the old values and would mean I just need one column.
I have some experience at MS-Excel, also VBA but mainly in MS-Access never in MS-Excel. If answers could be explained as simply and hands on as possible it would be appreciated.
After Googling the issue I came across this formula and changed the ranges to fit my sheet but Excel doesn't like it saying it contains an error
=QUERY( A1:B6; "select B where A =date """&TEXT(TODAY();"yyyy-mm-dd")&""" "; 0
Sorry again if I haven't explained myself properly.
If your workbook isn't guaranteed to be open at the end of each month I would update the value every time it gets opened, like(Should be placed in ThisWorkbook):
'Runs when you open the workbook
Private Sub Workbook_Open()
'Loops through U3 to the last used cell in that column
For Each c In Range(Cells(3, 21), Cells(Rows.Count, 21).End(xlUp))
'Applies the J7 value to the current month and exits the sub
If Month(c) = Month(Now) Then c.Offset(, 2).Value = [J7]: Exit Sub
Next c
End Sub
Also, not that it matters but, I would apply the following formula in U3:U14 to always get the correct dates:
=EOMONTH(DATE(YEAR(TODAY()),ROW()-2,15),0)
Okay, I'm still not super sure what the question is and I know more Access VBA than Excel VBA, but here's something that might help to find a solution.
You can make a check date function that returns a Boolean value:
Public Function EoMonthCheck() As Boolean
Dim eo_month As Date, today As Date
eo_month = Format(WorksheetFunction.EoMonth(Now(), 0), "yyyy-MM-dd")
today = Format(Now(), "yyyy-MM-dd")
If today = eo_month Then
EoMonthCheck = True
Else
EoMonthCheck = False
End If
End Function
And the,, to add a value to the "W" column, we might use something like this:
Public Function AppendValue(Optional target_cell As String = "J7")
''' This could be a subroutine, too, I guess, since we're not returning anything.
Dim i As Integer
''' Activate whatever sheet you want to work with
Worksheets("Sheet1").Activate
If EoMonthCheck() = True Then
''' Look up the bottom of the 'W' column and find the first non-empty cell
''' Add 1 to that cell to get you to the next cell (the first empty one).
i = Cells(Rows.Count, "W").End(xlUp).Row + 1
''' Set the value of that empty cell in the 'W' column to the value of 'J7'
''' which will happen after we evaluate whether it is the end of the month.
Cells(i, "W").Value = Range(target_cell).Value
End If
Then, you could maybe trigger that each time the workbook opens.

Resources