Excel VBA - Number format seems unchangeable [closed] - vba

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an excel table which is straight from a database. Its first column contain dates but are not formatted as dates. They are written as : 03/01/2014 for example.
So I'm trying to comparing values from some cells and the problem is people type dates as : 3/01/2014. When comparing the values no value is returned. I'm using the range.find() command. So I tried to change the format of the date column in the table but it didn't change at all. However if I try to edit a cell and just press enter it becomes a normal date. I wonder if there is a way I can convert all those rigid dates in the table to normal excel dates so that the comparison routine can work.
Thanks you.

Select the cells whose dates you wish to convert and run this small macro:
Sub DateSetter()
Dim r As Range, d As Date, st As String
For Each r In Selection
st = r.Text
If InStr(st, "/") <> 0 Then
ary = Split(st, "/")
d = DateSerial(CInt(ary(2)), CInt(ary(1)), CInt(ary(0)))
r.Clear
r.Value = d
r.NumberFormat = "m/d/yyyy"
End If
Next r
End Sub
It should perform the conversion, but ignore zeros, blanks, and other junk.

Related

How to delete a specific range when specific cell is empty? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have the following problem.
In an excel spreadsheet I am populating a calendar schedule.
Based on a drop down list, you can change the months and year, and therefore the dates adjust automatically to the week days.
Fixed week days = `E6:R6`
Calendar dates = `E7:R7`
Work Shifts = `E8:R15`
Now, when the new schedule populates and like in this case, there is no "Sunday" before October 1st, then I need to have all names from range E8:E15 removed.
I've tried an IF THEN ELSEIF in the sub, and that works, but only for the first range - E8:E15. If I switch the month to September, and we start with a Saturday, then all cells maintain their values, but E8:E15. It's like I can only do "1" IF loop and then it stops.
I'm looking for code that loops through all weeks to check if the cell shows a date or is empty. The only possible weeks with no dates would be week 1, and week 5 + 6. As you can see in the last pic, depending on how long the month is, more or less dates show.
UPDATE:
Hi there, it works now and I was able to work it out the following way.
My original code was:
If Sheets("schedule").Range("E5") = "" Then
Sheets("Schedule").Range("E8:E15").ClearContents
Elseif Sheets("schedule").Range("G5") = "" Then
Sheets("Schedule").Range("G8:G15").ClearContents
..and so on for all other ranges of my calendar.
End if
The new code is:
What I simply tried next was leaving them all separate.
If Sheets("schedule").Range("E5") = "" Then
Sheets("Schedule").Range("E8:E15").ClearContents
End if
If Sheets("schedule").Range("G5") = "" Then
Sheets("Schedule").Range("G8:G15").ClearContents
End if
.... and so on.
And now it's running just fine.
For some reason it was not able to run through several IF-loops before the end if.
Thanks everyone for all the fast help!
I'll definitely be back when I have another question about Excel and VBA :)
I'll start by assuming that range("E8:E15").clearcontents is not a typo despite being a single column while all others are 2 columns.
Code¹:
sub buildSched()
dim c as long
with worksheets("schedule")
for c = 5 to 17 step 2
if .cells(5, c).value2 = vbnullstring
.cells(8, c).resize(8, 1 + abs(cbool(c > 5))).clearcontents
end if
next c
end with
end sub
If range("E8:E15").clearcontents was a typo and you should be clearing two columns every iteration then use,
.cells(8, c).resize(8, 2).clearcontents
¹ Note code that can be copied and pasted from the page directly into a module code sheet in the VBE. Not an image of code that would require retyping to test/debug.

Excel counting rows that are not empty in Vlookup value [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to count the total number of cells that have values for the row that I'll be looking up using their name. I have a different sheet for looking up the value I tried COUNTA and VLOOKUP. Is there any way to combine these two so I'll end up with the correct result?
Please see screenshot.
Thank you!
Use INDEX(,MATCH()):
=COUNTA(INDEX('Sheet1'!C:X,MATCH("Jessel Rayes",'Sheet1'!A:A,0),0))
Here is a vba function that looks up the value you define (first input) in a specified range (second input) and then returns the number of empty cells in the same row right from this cell for a specified amount of columns (third input).
Function TLookupT(Value As Variant, arr As Range, column As Long)
x = 0
For Each Cell In arr
If Cell.Value = Value Then
For i = 1 To column - 1
If Cell.Offset(0, i) = "" Then
x = x + 1
End If
Next i
End If
Next Cell
TLookupT = x
End Function

How to find the days difference between two dates [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have been working on finding the Days difference between the dates found in Column A and column B. The dates in both columns are not constant thus I would need a code that would allow to read both dates in column A and B and find the Days difference between those two dates until the last row is empty.
Is there any code that I could use to find the Days difference between column A and B with a range of more than 500 rows?
Instead of using vba just enter the formula for the first row on C1 :
=ROUND(a1,0)-ROUND(b1,0)
Then just the formula to the end of exisiting rows.
If you insist using vba code use the simple code below:
Dim LastRow As Long
LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
Range("c1").Select
ActiveCell.FormulaR1C1 = "=ROUND(RC[-1],0)-ROUND(RC[-2],0)"
Range("c1").AutoFill Destination:=Range("C1:C" & LastRow)
End Sub

How to find the standard deviation of alternative cells in excel 2007 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I want to calculate the standard deviation of alternative cells in a 10000 rows in excel 2007, how can i do that ? whether i need a formula or a VBA ?. Can some help me.
It's also possible with a formula. This "array formula" gives you standard deviation of every other cell starting at A1
=STDEV(IF(MOD(ROW(A1:A10000)-ROW(A1),2)=0,A1:A10000))
confirm with CTRL+SHIFT+ENTER
I would use both, the following code splits/sorts your rows and selects every second row and pastes it into another column, beginning with the first cell:
Sub splitcells()
Dim rows As Long
Dim i, j As Long
j = 0
rows = ?1?
For i = ?2? To rows Step 2
Cells(i - j, ?4?).Value = Cells(i, ?3?).Value
j = j + 1
Next
End Sub
Then you have to replace:
?1? - with the end row number of your list
?2? - with the beginning row number of your list
?3? - the column number of your list to be sorted
?4? - the column number of your destination column (the one the sorted data is sent to)
Once you have completed this, just run a normal standard deviation function on your new column and you'll have your answer.

Delete spaces in cell - VBA [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the following string in excel:
" 163,40 3,10 1,86 163,30 163,40 167,00 163,30 435862329"
And I have no problem to split up this column into 8 individual columns - one for each block of data. But I saw that the first column - here 163.40 is truncated so it becomes 163 - that is from a float to an integer. I realized later thats because the numbers is preceeded by four spaces - " 163.40".
So my question is how to delete these four spaces - and ONLY these four first spaces.
That would solve my problem.
Any ideas?
Use Mid function like used below for your problem.
Mid(text, 5, Len(text))
Assuming that your source string is in cell A1 and you need the 8 columns data in row 2 ; please refer to below code :
Function SplitMyData()
Dim var As Variant
var = Split(Trim(Range("A1").Value), " ", , vbTextCompare)
For i = 0 To UBound(var)
Cells(2, i + 1).Value = var(i) 'Pasting vals in row 2
Next
End Function
You can change the source and destination cell references as per your requirements. :)