Converting data stored as text to numbers (Oracle SQL) - 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.

Related

How to highlight cells if they match neighboring cells

I regularly work with data spanning multiple columns and need a convenient way to highlight multiple rows that contain the same value in a specific column, but I need to alternate between highlighted and non-highlighted.
For example, I'll have several rows with data in Column A like:
700105862
700105862
700105862
700103235
700103235
700108783
700108783
700108783
And what I'd want to do is highlight the first three rows (700105862), then not highlight 700103235, then again, highlight 700108783.
I was wondering if there was a conditional formatting formula that'd make this possible.
Any help would be greatly appreciated!
Thank you,
if your numbers are divided into chunks of always different numbers repetitions, then you could use this VBA code:
Sub main()
Dim item As Variant
Dim startRow As Long
Dim okHighlight As Boolean
With Range("A1", Cells(Rows.count, 1).End(xlUp))
For Each item In GetUniqueValues(.Cells).Items
If okHighlight Then .Range(.Cells(startRow, 1), .Cells(item, 1)).Interior.ColorIndex = 48
startRow = item + 1
okHighlight = Not okHighlight
Next
End With
End Sub
Function GetUniqueValues(rng As Range) As Dictionary
Dim cell As Range
Dim dict As Dictionary
Set dict = New Dictionary
With dict
For Each cell In rng
.item(cell.Value) = cell.row - rng.Rows(1).row + 1
Next
End With
Set GetUniqueValues = dict
End Function
a Conditional formatting approach is possible on with a helper column
assuming:
your data are in column A and begin from row 2
column B is free
then:
write the following formula in helper column B cells:
=IF(A2<>A1,B1+1,0)
apply conditional formatting to column A with the following formula:
=INT(B2/2)=B2/2
and choosing the format you like to highlight cells
Sure, if you know what ranges you want to highlight you'd simply set the conditional formatting to be between x and y values. Comment on this question with what you dont get and I'll amend the answer accordingly.

Concatenate Column E and G data to Column M

I would like to concatenate my data from Column E and G to Column M, starting from row 2 down to a varying (every month) number of rows
somewhat like m2=e2&g2 until there's no data to concatenate.
I'm looking for possible answers but, I still can't play with all the codes I have found here because I'm just starting to learn VBA coding. Thanks in advance for the help. :)
You don't really need to use VBA if all you want to do is to concatenate two cells together - you could use the CONCATENATE worksheet function inside your worksheet. Most of the worksheets functions can be used in VBA by appending 'Application.' to them but CONCATENATE isn't one of them. In order to do what you want in VBA you can do something like this:
Option Explicit
Sub concatColumns()
With Worksheets(1)
Dim lastRow As Integer, i As Integer
lastRow = .Cells(.Rows.Count, "E").End(xlUp).row
For i = 2 To lastRow:
.Cells(i, "M") = .Cells(i, "E") & .Cells(i, "G")
.Cells.NumberFormat = "#"
Next
End With
End Sub
The above assumes that you're using the 1st worksheet and also explicitly changes the format of each "M" column entry to text so that excel doesn't auto format the result.

Storing dates using Excel VBA

I have a sheet called 'Data,' and in Column A of this sheet, there are various text values, blank values, and date values. I'm trying to find a way to copy each of the date values in Column A of 'Data' and paste it into the first row of another sheet 'Chart.' The end result I'm looking for is to create a chart with column headings for each of the dates. There's more to do with the chart that I've been piecing together little-by-little, but I'm pretty stuck on how to accomplish the column headings.
The main piece I'm looking for is how to copy each of just the date values. I've looked a bit into the Find function, but I can't quite get it to hone in on only date values.
Thank you so much for any help!
Cheers,
B
Public Sub Answer()
Dim DestWS As Worksheet
Dim DestCol As Integer
Dim ColumnA As Variant
Set DestWS = ThisWorkbook.Worksheets("Chart")
ColumnA = ThisWorkbook.Worksheets("Data").Columns(1)
DestCol = 1
For Each c In ColumnA
If IsDate(c) Then
DestWS.Cells(1, DestCol) = c
DestCol = DestCol + 1
End If
Next
End Sub

excel VBA code to Copy and Paste a set of data with a finite amount (count)

In excel on a single sheet, I have a blank template and a set of raw data on the side which needs to be inserted into the template. I need help creating the VBA code to copy and paste the data into the template with it not pasting any extra cells (stop at the end of the data). My raw data changes and should be able to be any length of rows but it is always constant from columns Z:AL. I am interesting in moving it to columns A5:M5.
Thanks in advance!
This is the simplest code I can think of. You might want to throw a worksheet reference in front of the Range and I included a couple of methods of finding the end of the range. I prefer the 3rd method.
dest = "A5"
wsName = "DataSheet"
With Worksheets(wsName)
endRow1 = .Range("Z1").End(xlDown).Row
endRow2 = .Range("Z105000").End(xlUp).Row
endRow3 = .Range("Z:AL").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
.Range("Z1:AL" & endRow3).Copy Destination:=Range(dest)
End With
If there are not blanks in a column in the dataset (I assume column Z) then you can use Range.End to get the last row. I try to avoid using Copy/Paste in macros, because there's a faster way to do it.
Option Explicit
Sub MoveDataRange()
Dim dest As Range, endRow As Integer
With Worksheets("DataSheet")
endRow = .Range("Z1").End(xlDown).Row
Set dest = .Range("A5").Resize(endRow, 13) '13 columns between Z:AL
dest.Value = .Range("Z1:AL" & endRow).Value
End With
End Sub

Excel VBA Application.Match between Long Time and Custom format

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.