Format text column as a date unless there is specific text - excel

I am new to Power Query.
I have a database, containing all text fields, to populate onto an excel spreadsheet as a requirement for business purposes. I am not allowed to add or delete tables in the spreadsheet. The problem I'm having is with the End Date column.
The End Date column can either have a date format or "TBD" as text if there is a start date but no end date.
Essentially I need power query to format the incoming in the End Date column as a date, unless the row contains "TBD". In that case I need the row to remain as "TBD". However as you can see from the picture above, it fails to populate correctly.
This is the formula I've been trying to get working but I haven't had any luck. Is there a way to accomplish this task?
= Table.TransformColumns(
_qryPerstat,{{"END DT",
each if qryPerstat[END DT] = "TBD" then "TBD" else Date.From(DateTimeZone.From()), type date}})

Related

Is there a way to link the source data for a chart to the values selected from a dropdown box?

So I've created a graph to appear on this separate sheet titled "Report", and above it you can see that there are two dropdown boxes in which a user can select a specific date range. I would essentially like to link this user-specified date range to the range displayed on the graph. I'm having trouble figuring out how to link the source data for the chart to the date selectors.
The data for the chart is sourced from this separate sheet titled "Database", in which the data is chronologically ordered with corresponding Feed values. The goal is to match the dates selected by the user in the "Report" section to the dates in this "Database" sheet, and after finding the corresponding initial and start date, apply that same logic to the feed data to strictly display the feed data within that date range.
I am currently sourcing the chart data using this Macro, the xData and yData are linked to lists titled "Dates" and "FeedTonnage", which are basically tables I made out of the columns in the database. I'm not sure if the tables are even needed, but the database will continue to be updated overtime as more data will be coming in so I thought a table would've been useful for this.
So I've been trying to write a sub that sifts through the Date column to look for a date that matches the dates selected in the "Report" sheet. I'm doing this because I intend on using the cell address to get the corresponding data from the Feed column by replacing the column portion of the cell address with that of the Feed, and yeah, I'm kinda lost at this point so I could really use some input.
Instead of changing the data range of the chart, you can hide the rows of data outside the selected dates:
Worksheets("Database").Rows("3:100").Hidden = False
For i = 3 To 100
If Worksheets("Database").Range("A" & i).Value < Worksheets("Report").Range("B1").Value Or Worksheets("Database").Range("A" & i).Value > Worksheets("Report").Range("C1").Value Then
Worksheets("Database").Rows(i).Hidden = True
End If
Next i
The code above assumes the dates are in the range A3:A100, the start date is in B1 and the end date is in C1. Adjust the ranges as needed

VBA min start date displaying a number

I have a column of dates in format dd/mm/yyyy and when I use the following code:
Sub DisplayDate()
MsgBox (WorksheetFunction.Min(Range("StartDate").EntireColumn))
End Sub
the result is 4442.7 which doesn't make sense as all entries in this column are either blank or in dd/mm/yyyy format. I'm trying to display the earliest date of the entire column. "StartDate" is the heading reference for that column, but this heading is in row 5 (there are four blank rows above it) It is working when the heading is not included i.e. taking from row 6 down, but I'm not sure how to code this using the name reference. I must use the name reference, not column number reference,
Using strings, VBA is confused and try guessing what you try extracting (as date). The returned value means "28/02/1912 16:48:00", but it is a wrong guess...
Sub DisplayDate()
MsgBox WorksheetFunction.Min(Range("StartDate").EntireColumn.Resize(Range("StartDate").rows.count - 1).Offset(1))
End Sub

TextBox swaps Date format from dd/mm/yy to mm/dd/yy

I have an Userform where you get the dates from a Table. The Dates in this Table are in "dd/mm/yy" format. When you get the Dates it appears like this:
I can write a new date on either of the textboxes and with the Modify Button I can modify the dates in my Table.
The problem is that when the new dates appear in the Table, they change format and turn into mm/dd/yy.
I'm already using format when putting the date in the text box and when puting it in the Table:
'putting date in Text Box:
FMDateEditor.tbFechaRecibida.Text = Format(MainCode.myRange.Cells(MainCode.EditIndexFechas, shMaster.ListObjects("MasterTable").ListColumns("Fecha recibida").index).Value, "dd/mm/yy")
'
'Puttting date in Table:
MainCode.myRange.Cells(MainCode.EditIndexFechas, shMaster.ListObjects("MasterTable").ListColumns("Fecha recibida").index).Value = Format(FMDateEditor.tbFechaRecibida.Text, "dd/mm/yy")
But the problem Persists
The cells in the Table have "dd/mm/yy" format.
Thanks for the help
Excel stores dates as a number representing the mm/dd/yyyy format and performs all its calculations in this format. So avoid applying date formats when assigning from a variable to a variable (or using in calculations)
Try:
FMDateEditor.tbFechaRecibida.Text = _
Format(MainCode.myRange.Cells(MainCode.EditIndexFechas, _
shMaster.ListObjects("MasterTable"). _
ListColumns("Fecha recibida").Index).Value, "dd/mm/yy") & ""

day and month are reversed

I have a cell with the following content:
01/02/2015
The cell is date formatted.
Then I copy the value and put it in my module class:
Set Line = New CTravelLine
Line.Date= Cells(1, 8).value
Everything works fine until the moment I put this value in another cell:
The value 01/02/2015 becomes 02/01/2015.
I am using this format (dd/mm/yyyy). I have the impression that when the days are numerically lower than the month, the 2 values are reversed. The values are reversed whatever the method I tried:
Method 1:
Dim WrdArray() As String, datetest As String
WrdArray() = Split(travelLine.Date, "/")
datetest= WrdArray(0) & "/" & WrdArray(1) & "/" & WrdArray(2)
Cells(5, 5) = datetest
Method 2:
Cells(5, 5) = travelLine.Date
I don't understand how I can solve this problem.
This might have happened due to 'Regional formatting problem'.
Excel has a habit of forcing the American date format (mm/dd/yyyy) when the dates have been imported from another data source. So, if the day in your date happens to be 1 - 12, then Excel will switch the date to mm/dd/yyyy.
When dates are imported from a text file, there is an option in the VBA code to apply regional format which corrects this problem.
OR
Change number format of date column in excelsheet from 'date' format category to 'text'; save it.
(After Saving run the VBA Code if you have any. Now check whether the date format is 'text' or changed back to 'date'.)
If it has changed back to 'date' try to fix it as 'text'
If it's 'text'; Correct the erroneous date cells and save the excel sheet. This will make dates not to change automatically to American Format.
Long story short, I had a similar problem where the dates are working just fine in some cells but keep flipping in others regardless if I copy paste or enter manually, I did the whole data text to column and cell formatting solutions and all of that didn't work.
The solution actually is not in excel, it's in the region and language setting.
To have the dates display as MM/DD/YYYY in the formats tab change the format to US.
To have the dates display as DD/MM/YYYY in the formats tab change the format to UK.
I had the same issue as you .
Let me explain what I want to do :
I have a csv file with some date.
I copy a range of my sheet in variable table. The range contain some columns with dates.
I make some manipulations on my table (very basic ones)
I transpose my variable table (since only the last dimension of a variable table can be increase)
I put my variable table on a new sheet.
What I found:
There is no date issue after executing step 1-4. The date issue shows up when writing on the sheet...
Considering what Avidan said on the post of Feb 24 '15 at 13:36, I guess it is excel which forces the American format mm/dd/yyyy... So I just change the date format at the very beginning of my program :
Before starting any manipulation date:
do
.Cells("where my date is") = Format(.Cells("where my date is"), "mm dd yy")
execute your program
write the table on a sheet
back up to the date format you like directly on the sheet
Just use:
Line.Date = cDate(Cells(1, 8).value2)

Excel Date Conversion from yyyymmdd to mm/dd/yyyy

I have been searching for about an hour on how to do this in Excel.
I have an Excel file that was created from an old system and I am pulling information from a SQL Server Database, I will be inputting the information back into the SQL Server Database and would like the Dates to match.
I have tried Creating a Custom Format, but I am unsure if I even did it Correctly. I found several places where they want to go the other way mm/dd/yyyy to yyyymmdd but they have not been helpful.
I am unfamiliar with using VBA in any Microsoft Products otherwise I am sure that this would be a simple Task.
I have two separate columns that need to be changed.
How do I Format the entire column from (float)yyyymmdd to a (Date)mm/dd/yyyy
You can convert the value to a date using a formula like this, next to the cell:
=DATE(LEFT(A1,4),MID(A1,5,2),RIGHT(A1,2))
Where A1 is the field you need to convert.
Alternatively, you could use this code in VBA:
Sub ConvertYYYYMMDDToDate()
Dim c As Range
For Each c In Selection.Cells
c.Value = DateSerial(Left(c.Value, 4), Mid(c.Value, 5, 2), Right(c.Value, 2))
'Following line added only to enforce the format.
c.NumberFormat = "mm/dd/yyyy"
Next
End Sub
Just highlight any cells you want fixed and run the code.
Note as RJohnson mentioned in the comments, this code will error if one of your selected cells is empty. You can add a condition on c.value to skip the update if it is blank.
Do you have ROWS of data (horizontal) as you stated or COLUMNS (vertical)?
If it's the latter you can use "Text to columns" functionality to convert a whole column "in situ" - to do that:
Select column > Data > Text to columns > Next > Next > Choose "Date" under "column data format" and "YMD" from dropdown > Finish
....otherwise you can convert with a formula by using
=TEXT(A1,"0000-00-00")+0
and format in required date format
Here is a bare bones version:
Let's say that you have a date in Cell A1 in the format you described. For example: 19760210.
Then this formula will give you the date you want:
=DATE(LEFT(A1,4),MID(A1,5,2),RIGHT(A1,2)).
On my system (Excel 2010) it works with strings or floats.
for converting dd/mm/yyyy to mm/dd/yyyy
=DATE(RIGHT(a1,4),MID(a1,4,2),LEFT(a1,2))
Found another (manual) answer which worked well for me
Select the column.
Choose Data tab
Text to Columns - opens new box
(choose Delimited), Next
(uncheck all boxes, use "none" for text qualifier), Next
use the ymd option from the Date dropdown.
Click Finish

Resources