Excel VBA Application.Match between Long Time and Custom format - vba

I can't for the life of me get this to work.
I have a column r of time values formatted as "yyyy-mm-dd hh:mm:ss.000 AM/PM" (this format is one of the Custom formats one can choose for a cell).
I also have a cell c containing a time value formatted as "*hh:mm:ss" (the first Time format).
I now want to find on which row i the c.Value is in range r. I do this using
i = Application.Match(c.Value, r, 1)
On this row I get the 'Run-time error '13': Type mismatch' and my guess is that it is because the cells in r has a Custom format, or because there is no date in c.Value.
How can I get this to work? Is there some other way I can get the placement of c.Value in the range r?
Code:
Sub test()
Dim r As Range
Set r = Range("A1:A10")
Dim c As Range
Set c = Range("B1")
Dim i As Integer
i = Application.Match(c.Value, r, 1) 'ERROR!
End Sub
SOLVED:
I didn't know Excel treats dates and times as numbers, so I got it to work when saving c.Value as a type Double before using it in the function.

SOLVED:
I didn't know Excel treats dates and times as numbers, so I got it to work when saving c.Value as a type Double before using it in the function.

Related

Count values that aren't blank or N/A - VBA

I had figured out this formula to count all those values that weren't blank, but the system updated the template and now besides blank it has also "N/A" values.
How can I transform this
=COUNTIF('R Plan'!XT2:XT3658,"<>")
To test if besides NOT blank also test for NOT N/A
Because I tried this
=COUNTIF('R Plan'!XT2:XT3658,"<>" OR "<>N/A")
and I'm getting a message that we found an error on your formula
If you're using >=XL2007 then you can use COUNTIFS:
=COUNTIFS('R Plan'!XT2:XT3658,"<>",'R Plan'!XT2:XT3658,"<>N/A")
If you're using <=XL2003 then you can use:
=SUM(COUNTIF('R Plan'!XT2:XT3658,"<>"),-COUNTIF('R Plan'!XT2:XT3658,"N/A"))
Count the number that are not empty and minus those that have N/A.
or
=SUMPRODUCT(('R Plan'!XT2:XT3658<>"")*('R Plan'!XT2:XT3658<>"N/A"))
If you want to use VBA, then this will work for you,
Put this in a cell and choose your range =COUNTBLANKS(B1,A1:A21) , B1 being the value you want to count and the second range is the range you want to count how many times that value occurs.
Add a module and paste this function in it.
Function COUNTBLANKS(r As Range, rng As Range) As Long
Dim x
Dim c
x = rng
For Each c In x
If c = r Then
COUNTBLANKS = COUNTBLANKS + 1
End If
Next c
End Function

Converting data stored as text to numbers (Oracle SQL)

I have some data that i pull from a couple of Oracle databases into Excel via a VBA macro. Some of the columns have data (numbers), afaik, stored as text. I need to convert these "wrongly" formatted data to numbers so that i can do calculations on it, however i cant figure out how to do this effectively using VBA. Furthermore only some of the data is stored as text (columns K-Q are formatted incorrectly, see picture below).
Currently im using this loop to convert the data:
Sub convertTextToNumbers(ByVal sColumnHeader As String)
Dim col As Range, c As Range
Dim colIndexNumber As Integer, lastUsedRow As Integer
colIndexNumber = findColumnIndexNumber(sColumnHeader)
lastUsedRow = ActiveSheet.Cells(Rows.Count, colIndexNumber).End(xlUp).Row
Set col = Range(Cells(2, colIndexNumber), Cells(lastUsedRow, colIndexNumber))
For Each c In col
c.Value = c.Value
Next c
End Sub
Is this the most effective way to do it? Can i convert using SQL? The SQL-query im using is simply:
SELECT * FROM table_name
Also converting by choosing another formatting does not work.
Thanks!
Data wrongly formatted is in columns K-Q:
Use number format on the columns.
Columns("K:Q").Select
Selection.NumberFormat = "0.00"
Or more programmatic
Private Sub formatNumbers()
Dim ws As Excel.Worksheet
Set ws = Application.ActiveSheet
ws.Range("K:Q").NumberFormat = "0.00"
End Sub
Any format you see in the range format menu can be applied this way.

Find if date is present in Range of cells (Excel VBA)

I have a cell F2 that contains a Date, and is formatted as Custom Date field displaying only day and month.
Then i have a Range of Cells C3;C60 that contains also Dates and is formatted as European Date field displaying dd/mm/yyyy
I am writing a VBA that makes a check on those fields, but it is not working.
in the sample below the variable c can be any cell between F5 and F20.
I get RuntimeError 91.
If Worksheets(1).Range("C3", "C60").Find(Worksheets(1).Cells(2, c.Column).Value) = True Then
c.Value = "Whatever"
Else
.Find() returns a range, your if statement is checking to see if it's TRUE. That will fail.
Dim rng as Range
Set rng = Worksheets(1).Range("C3:C60").Find(Worksheets(1).Cells(2, c.Column).Value)
if not rng is Nothing then
c.Value = "Whatever"
Else
Note the inclusion of Gary's answer
Substitute:
Range("C3:C60")
for
Range("C3", "C60")
There may be other problems.

Excel VBA to force numeric value into time format

There are two columns copied into a workbook from a Access database.
I want to ensure that the data is formatted correctly so I added this code:
'DateTime Column
Sheets("Sheet1").Columns("A:A").Select
Selection.NumberFormat = "m/d/yyyy hh:mm"
'Time Column
Sheets("Sheet1").Columns("B:B").Select
Selection.NumberFormat = "hh:mm"
The datetime column formats correctly.
The time column is initially copied as a numeric equivalent (ie 0.595277777777778) instead of the time value (14:17).
Running the macro code does nothing to the visual display and it isn't until I hit F2 and enter on the cell that the format applies.
Is there a method (short of a loop) to force Excel to apply the formatting?
You can use VBA to accomplish the same effect as Steve Homer's answer by setting the range's value property to itself:
Dim r As Range
Set r = Sheets("Sheet1").Columns("B:B")
r.Value = r.Value
If you copy the column and paste as values (through a macro if needs be) then Excel should re-interpret the data type and the above should work correctly.
You can do this with out declare TMP as DATE:
TMP = CDate(Range("A1").Value) ' we suppose A1 value is 12:45:00
TMP = "" & TMP ' convert to char
TMP = Left(TMP, 5) ' extract the first 5 char
MsgBox TMP ' for displaying 12:45

looping through a range of cells

I need to run the function AddColumnofData on each cell from c27 and every contiguous non empty cell to the right, but I get a "Run-time error 424 object required", any help woul dbe greatly appreciated.
Set col = Range("$C$27", Range("$C$27").End(xlToRight))
For Each c In col
AddColumnofData (c)
Next c
On the assumption you have defined AddColumnofData as
Sub AddColumnofData(c As Range)
...
End Sub
your call to it needs to be
AddColumnofData c
(that is, remove the brackets)
Jesse's advice to DIM your variables, while not not manditory is good advice. Applies to col as well. To make it manditory add Option Explicit to the top of your module.
You can declare or set your range objects before passing them to your function. To prove that you are passing the correct values to your function, try this.
Dim r As Range '-- if you don't declare it as a range type you get a variant type as default
Dim c As Range '-- this is used to store the single cell in the For Each loop
Set r = Range("A1:D1") '-- substitute your range as per your example
For Each c In r '-- you could also use r.cells
MsgBox c.Value '-- pass to your function instead of a call to the Message Box
Next
This produces a series of 4 Message Boxes containing the values in cells A1 to D1 of the active worksheet, if your Range "r" is seriously large then pass it to Debug.Print instead.
If you declare c:
Dim c as Range
Then what you have should work.