Vba turns cell in to date on mistake - vba

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.

Related

Vba Numberformat muliple condtions to format cell value

Dear all i want to format my cell based on the Cell value. There are 3 possible Conditions:
Cell is larger than 0.05 -> the Value should stay the same but be fromated to 0.0
Cell is smaller than 0.05 -> the Value should be replaced with a String "a.C."
Cell is Zero -> the Value should be replaced with a Dash"
I found thisSolution for the dashes and could combine it with the number formating
.NumberFormat = "0.0;[=0]---"
This works. but if i add an additonal argument the vba code breaks.
.NumberFormat = "0.0;[=0]---;[<0.05]a.C."
Does not work. Would you mind telling me what i am doing wrong?
Thank you in advance
Edit:
FOr what ever reason this Order seems to work:
.NumberFormat = "[=0]---;[<0.05] ""a.C."";0.0 "

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

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")

Excel VBA: Copy the Data up to the last column that has value

The spreadsheet has multiple values in a cell.
What I'm trying to do is get that value found in a cell and paste it to another sheet and copy the other fields(columns) that belong to that value. How do I set the range in order copy the other fields(columns) up to the last column that has value? Thanks in advance.
For iRowGetProdCode = 0 To UBound(sSplitProdCode)
Sheets("Output").Cells(iRowCountOutput, 1).Value = sSplitProdCode(iRowGetProdCode)
iRowCountOutput = iRowCountOutput + 1
Next iRowGetProdCode
here is an idea how to discover an un-empty columns in the same row,
maybe you will find it useful and manipulate it for your needs:
Function LoopUntilLastColumn(ByVal Row As Integer)
Dim i As Integer
i = 1
Do While Cells(Row, i) <> ""
' do somthing
MsgBox (" I AM ALIVE COLUMN!")
i = i + 1
Loop
' you can also use the return value of the function.
LoopUntilLastColumn = i
End Function
I'm not exactly sure about what you're asking, but here are my three best guesses.
1.) Splitting delimited data from a single cell to columns
Without VBA: Use the "Text to Columns" function (Excel Ribbon:
Data|Data Tools).
With VBA: Use the split function MSDN (Related Post), then assign array values to target cells. Or parse your string manually with a loop.
2.) Finding the end of a continuous range
Without VBA: Use ctrl + arrow key
With VBA: Use the Range.End Property
3.) Looping through columns and rows
Used a nested loop:
For c = 1 to 5
For r = 1 to 20
Cells(r,c) = "Row = " & r & ", Column = " & C
Next
Next
Editing Suggestions (I don't have enough reputation to directly comment or edit)
This question as worded may be too specific for StackOverflow. Consider re-wording so that the problem can be understood in a general context and your question can be more useful to others.
Also, the wording is a little confusing. For example, use of the term "value" seems to change from referring to delimited data to referring to cell content in VBA. Likewise, it can be confusing to use "fields" or "columns" to describe the data if it's actually delimited text, so clarity on the data's state of existence would help.
It also seems to me that you've parsed the string on it's delimiter to an array, and that you're looping through this array to write the data in rows. I still can't see how exactly your question about setting a range fits in.

VBA Excel Date Generator

I was wondering if there is a way using vba or excel to make an array of each day starting with Oct. 10th 2010 and ending with Sept. 30 2015. Unfortunately the dates have to be in the format YYYYMMDD, such as 20091001 and 20150930.
Thanks for the help
For a non-VBA solution KingsInnerSoul's solution is very good.
For VBA, you could do something like this:
Sub tgr()
Dim arrDates() As Variant
Dim dtStart As Date
Dim dtEnd As Date
'Assign start and end dates
dtStart = CDate("Oct 10, 2010")
dtEnd = CDate("Sep 30, 2015")
'Create the array by evaluating an Index formula
arrDates = Application.Transpose(Evaluate("INDEX(TEXT(""" & dtStart & """+ROW(1:" & dtEnd - dtStart + 1 & ")-1,""YYYYMMDD""),)"))
'Example of how to access the array values
MsgBox arrDates(1) & Chr(10) & _
arrDates(2) & Chr(10) & _
arrDates(3)
'You could iterate over the array with a loop also,
'or use any other array function, like Filter(), etc
End Sub
Create a list in Excel:
If you go to excel,
In the first top cell type 10/10/2010
Then right click on the cell - and select Format Cells,
Then go to the Custom category
In the Type input area type yyyymmdd - verify that it is the right format in the Sample field (I had it set to yyyymmdd;# )
Press OK
Once the cell is highlighted, you can see a border around it, and a + in the lower right corner of the cell.
Click on the + and drag it down as many rows as you need to - it will copy the cell's content and create a series with each cell adding a day.
Create a single row list in Word:
Once you created that single column in Excel, in the column next to it, type , (comma), and drag it down the same way in order to copy it next to the whole series.
Copy both columns in Excel.
Paste it to MS Word.
In the lower right corner an icon will appear - Paste Options.
Click on the Paste Options icon and select 'Keep Text Only' - It is the 'A' icon. Now you will have a single text column with the number, a tab character, comma, and Enter character.
Open the Search and Replace by pressing CTRL+H.
In the 'Find What' input field type ^p - it is the code for Enter.
Leave the `Replace With' field empty.
Press on `Replace All'.
Confirm the number of total replacements popup window by pressing OK.
To get rid of the Tab character, repeat steps 6-10 but search and replace ^w.
More information about special characters.
Related questions:
VBA convert range to array and format date
excel vba filling array
VBA Excel - Problems with a simple macro to auto-fill cells for a budgeting spreadsheet I'm attempting to make
Fill two columns with random dates
How to force Excel to automatically fill prior year in column instead of current year?
Fill dates array and add dummy variables
https://stackoverflow.com/questions/25277464/fill-dates-gap-in-excel
For the sake of an image, add October 10, 2010 (formatted yyyymmdd) in say A1 and HOME > Editing, - Fill:, Series...
for a column (or change Series in selection for in a row).

VBA CountIf using R1C1 and text

I'm trying to have a formula display the number or occurrences of the previous row of column "C" (RC3).
An ideal result would look like:
Count: 3
If there were 3 occurrences of the value in the previous row.
The following code returns "False".
y.FormulaR1C1 = "Count: " + "=Application.WorksheetFunction.CountIf(range(R3C3:R" & LR & "C3), " = "&R[-1]C3)"
y is a cell
LR is the last row
Thanks, and let me know if I can clarify further.
The CountIf Function is never going to count anything as long as it's inside a test string. And you don't need 'Application.WorksheetFunction' in a cell formula (only if you want to use that function entirely in VBA). Also you don't need a range function in an Excel formula. Try this:
y.FormulaR1C1 = "=""Count: ""&CountIf(R3C3:R" & LR & "C3,R[-1]C3)"
Though if there is nothing between your search range and your criteria cell, you could eliminate LR altogether with:
y.FormulaR1C1 = "=""Count: ""&CountIf(R3C3:R[-2]C3,R[-1]C3)"
If you are trying to make a formula that combines TEXT and builtin functions, use =CONCATENATE(). consider the example below:
If you are to use VBA to fill in formula with relative location (.FormulaR1C1), it would be:
=CONCATENATE("Count: ",COUNTIF(R[-5]C:R[-2]C,"=" &R[-1]C))