Restricting the text to be entered in a cell with Custom Formula (Data Validation) - vba

I've a string in a Cell which contains a date in YYMMDD format in it's first 6 characters. I want to restrict the user to enter only the value which is greater than or equal to current date.
170712 should be allowed as it's greater than or equal to current date 170712.
With VBA, the following formula is working fine:
CLng(Left(Range("H10").Value, 6)) >= CLng(Right(Year(Date), 2) & Format(Now, "MMDD"))
The same formula is not working when put it custom formula under data validation !
How should I put this in Data Validation Custom Formula Field in excel so I can restrict user the way I mentioned above?

you will have to put today() in a cell, and do the data validation for Date with refrence to >= to that cell. Inside data validation.

Try this custom formula in data validation.
=(DATEVALUE(TEXT(B1,"mm/dd/yy"))>=DATEVALUE(TEXT($A$1,"mm/dd/yy")))
The above formula is to apply data validation in Column B when the date 170712 is in cell A1

The code would be like this.
If Range("h10") >= Format(Date, "yymmdd") Then
If the cell of validation is b1 cell, the validation formula is bellow.
=B1>=TEXT(TODAY(),"yymmdd")

Related

How to use variable cell value in api query

I am a newbie with VBA and would like to perfect the last step in a complex spreadsheet. I hope someone can help me.
I need to convert EUR to USD based on historical dates which are to be found in adjacent cells and I need to do this for an entire column.
My query works fine with a fixed date inserted, no variable. How can I run this type of query with VBA for each row while inserting the appropriate variable date?
I have column B with date, column R with a modified date and column S with EUR value.
My target cell would be something like the below formula, where I have inserted in brackets where I need the variables applied to the search query. (dates).
Basically: If B5 is empty- do nothing, if R5(modified date) is empty- use B5 date for search, or else use R5 date for search
=IF( B5="", "", IF(R5="",[QUERY DATE VALUE IS B5] S5*XCH!B2, [QUERY DATE VALUE IS R5] B5*XCH!B2))
Sorry if I am not very technical here, thus I'm including all the info I have.
My working query (not VBA) is set up like this: (Note Date in query)
APPLIED STEPS:
Source: = Web.Page(Web.Contents("https://www.x-rates.com/historical/?from=EUR&amount=1&date=2018-07-01"))
Navigation: = Source{0}[Data]
Changed Type: = Table.TransformColumnTypes(Data,{{"Euro", type text}, {"1.00 EUR", type number}, {"inv. 1.00 EUR", type number}})
Removed Bottom Rows: = Table.RemoveLastN(#"Changed Type",8)
For example, in my target cell, the simple formula below works by using the fixed date inserted into the query (not the appropriate date).
=IF( B5="", "", IF(R5="", S5*XCH!B2, S5*XCH!B2))
Now, how do I get the above results for each cell BUT based on the values in R5 and S5 respectively?
Is this even possible to run VBA for each single cell in a column? All data is currently imported via a Macro from another worksheet. No user input.
So I would either use a macro- button to update all values on click, or use VBA to update the EUR value the moment data is imported.
I greatly appreciate any help I can get.

Vba turns cell in to date on mistake

Hi how can I get around vba is converting a cell automatic to a data format when I add a value like 10 - 12 I just want the value in the cell to be 10 - 12 not a date
the code I use is "cFli is the value 10 - 12":
tsheet.Range("E2").Value = cFli
You can prepend a single-quotation mark to the value of a cell, which indicates to Excel to treat the rest of the string as text:
tsheet.Range("E2").Value = "'" & cFli
Note: The ' does not become part of the cell contents, although it will appear when editing the cell in future.
Alternatively, you can achieve the same thing by formatting the cell using the Text format:
tsheet.Range("E2").NumberFormat = "#"
tsheet.Range("E2").Value = cFli
Note: The formatting must be done before putting the value into the cell.

Using conditional formatting in excel, I want to highlight the value in the range if matches with value given in cell B1

Using conditional formatting in excel, I want to highlight the value in the range if matches with value given in cell B1.
Assuming the relevant range is as in example, select A1:A10 and HOME > Styles - Conditional Formatting, New Rule..., Use a formula to determine which cells to format and Format values where this formula is true::
=A1=B$1
Format..., select choice of formatting, OK, OK.

Using Bloomberg BDH formula for several cells in a VBA Macro

Do you think it is possible to use the BDH formula but instead of using the name of one security we use the information within the cells for instance (assume that on the column A you have the ISIN of the securities and I want the price on column C at a certain date, date available in the column B):
for I = 1 to 10
cells(i+1,3) = BDH(cells(i+1,1),"PX_LAST", cells(i+1,3), cells(I+1,3), "Period, Dates", "M,H")
next I
Yes, you can. If the ISIN is stored in cell A1 you can use this formula:
BDH(A1 & " ISIN", "PX_LAST", "8/25/2016", "8/25/2016")
You can also replace the dates with a reference to a cell that contains the date.

How to convert multiple column from text to date?

In my excel file, there are hundred columns. I want to convert some columns (eg. Column A, C,F, G..) from text to date. It really take time if manually change each column one column by one.
How can I do it ? By using VBA ?
The original text column format just like '31-Dec-2011 00:00:00'. I want to convert to 'dd/mm/yyyy' 31/12/2011
Thanks
If you actually want to chop the time off in each of the cells then this might be an approach:
If the target times are in column A of a sheet called "target" and the column header in A1 is "StartTime"
You could amend the below and add an argument so that it takes the column address. Then it could be called several times to carry out this operation on several columns.
Sub ChopOffTime()
dim mySheetName as string
mysheetname = "target" '<<<<change to your sheet name
myLstRow = Excel.ActiveWorkbook.Sheets(mysheetname ).Cells(Excel.ActiveWorkbook.Sheets(mysheetname ).Rows.Count, Excel.ActiveWorkbook.Sheets(mysheetname).Range("A2").Column).End(Excel.xlUp).Row
With Excel.ActiveWorkbook.Sheets(mysheetname)
.Columns("B:B").Insert Excel.xlToRight
.Range("B2:B" & myLstRow).FormulaR1C1 = "=TIME(HOUR(RC[-1]),MINUTE(RC[-1]), SECOND(RC[-1]))"
.Range("B1") = "StartTime" '<<<<<change to appropriate header
.Columns("B:B").Copy
.Columns("A:A").PasteSpecial Excel.xlPasteValues
.Columns("B:B").Delete Excel.xlToLeft
End With
End Sub
Thanks all.
To simplified the procedure, I just developed some macro for user to run to change the column format, and then they can import the excel the mdb without error.
Use the following for the columns in question:
Columns("A:A").Select
Selection.NumberFormat = "m/d/yyyy"