Parsing a Number - vba

Here is my problem:
I am parsing a xml using excel VBA. I open the XML file as regular excel file, Then I go to column which is of my interest.
Cells in that column are 16 character.
So I am parsing each of those characters using Left and Right functions and saving them in a new file.
Mostly those 16 characters are Numeric and sometimes Alpha Character.
example 1111000011110000 or 1111YYYY00001111
when it parses 1111YYYY00001111 -- I get correct output,
like in new file Cell(1,1) =1 Cell(1,1) =1 Cell(1,2) =1 Cell(1,3) =1 Cell(1,4) =1 Cell(1,5) =Y and so on
But When I parse 1111000011110000 -- I dont get the correct output
I get Cell(1,1) =1 Cell(1,2) =. Cell(1,3) =1 Cell(1,4) =1 Cell(1,5) =1 Cell(1,6) =0
I tried to put NumberFormat="0" for column which I was parsing, but still i get the same output.
I also tried to put the debug variables and stored them as strings, Double Integer but I could not get pass it.
Here is my code:
Workbooks.Add
strip_concat = ActiveWorkbook.Name
Workbooks(strip_concat).Sheets(1).Cells(1, 1) = "Subid"
Workbooks(strip_concat).Sheets(1).Cells(1, 2) = "Strip#"
Workbooks(strip_concat).Sheets(1).Cells(1, 3) = "X"
Workbooks(strip_concat).Sheets(1).Cells(1, 4) = "Y"
Workbooks(strip_concat).Sheets(1).Cells(1, 5) = "Reject Code"
Workbooks(strip_concat).Sheets(1).Cells(1, 6) = "X-Y"
Workbooks.OpenXML Filename:=xml_file
stripfile = ActiveWorkbook.Name
For C = 3 To strip_col - 1
r = 1
Do
Y = r
x = C - 2
Workbooks(strip_concat).Sheets(1).Cells(A, 3) = x
Workbooks(strip_concat).Sheets(1).Cells(A, 4) = Y
Workbooks(strip_concat).Sheets(1).Cells(A, 6).NumberFormat = "#"
Workbooks(strip_concat).Sheets(1).Cells(A, 6) = x & "-" & Y
Workbooks(strip_concat).Sheets(1).Cells(A, 1) = Workbooks(stripfile).Sheets(1).Cells(C, Amkor_id_col)
Workbooks(strip_concat).Sheets(1).Cells(A, 2) = ExtractElement(Workbooks(stripfile).Sheets(1).Cells(C, Strip_num_col), 2, ".")
Workbooks(strip_concat).Sheets(1).Cells(A, 5) = Right(Left(Workbooks(stripfile).Sheets(1).Cells(C, Defect_data_col + i), r), 1)
r = r + 1
A = A + 1
Loop Until (r > 16)
If ((x Mod 13) = 0) Then i = i + 1
Next C
from xml file 1131111111111111

I got it working.. This is what I did
Dim x as variant
x = (Workbooks(stripfile).Sheets(1).Cells(C, Defect_data_col + i).Text
Workbooks(strip_concat).Sheets(1).Cells(A, 5) = Right(Left(x,r),1

Related

Link a word graph with a word table

i'm trying to visualize my data that i have stored in a word table. I can call the table data with ThisDocument.Tables(6).Cell(i,j).Range.Text. I tried to copy this data to the datasheet of the word graph, but this was unsuccessful.
Word table with data
The chart has to visualize the amount of currency in a timeline with time on the x-axis and the amount of money on the y-axis. I have allready inserted a chart in my word document but i need to access its datasheet.
Graph in word that i want to show
Does anyone have an example code that i can use to solve this problem?
I'm trying to build my code like this:
Dim graph As Word.Chart
Set graph = ThisDocument.InlineShapes(1).Chart
If Not Len(ThisDocument.Tables(3).Cell(2, 1).Range.Text) = 2 Then
Dim temp As String
For i = 0 To ThisDocument.Tables(3).Rows.Count - 2
graph.ChartData.Workbook.Worksheets(1).Cells(1 + i, 1).Value = Left(ThisDocument.Tables(3).Cell(2 + i, 3).Range.Text, Len(ThisDocument.Tables(3).Cell(2 + i, 3).Range.Text) - 2)
temp = Left(ThisDocument.Tables(3).Cell(2 + i, 4).Range.Text, Len(ThisDocument.Tables(3).Cell(2 + i, 4).Range.Text) - 2)
graph.ChartData.Workbook.Worksheets(1).Cells(1 + i, 2).Value = Right(temp, Len(temp) - 2)
Next i
End If
I'm trying to build my code like this:
Dim graph As Word.Chart
Set graph = ThisDocument.InlineShapes(1).Chart
If Not Len(ThisDocument.Tables(3).Cell(2, 1).Range.Text) = 2 Then
Dim temp As String
For i = 0 To ThisDocument.Tables(3).Rows.Count - 2
graph.ChartData.Workbook.Worksheets(1).Cells(1 + i, 1).Value = Left(ThisDocument.Tables(3).Cell(2 + i, 3).Range.Text, Len(ThisDocument.Tables(3).Cell(2 + i, 3).Range.Text) - 2)
temp = Left(ThisDocument.Tables(3).Cell(2 + i, 4).Range.Text, Len(ThisDocument.Tables(3).Cell(2 + i, 4).Range.Text) - 2)
graph.ChartData.Workbook.Worksheets(1).Cells(1 + i, 2).Value = Right(temp, Len(temp) - 2)
Next i
End If

If statement results overwriting each other VBA

I am experiencing a problem with the outputs from my loop. As the sub is running I can see that the results from the final IF statement are being overwritten by the results from the second one. My code is structured as follows:
for i = 1 to 5
for j = 1 to 50
for each events.value in eventArray
if events.value = arrayElem then
if cells(i,j).value = "x" then
type = "col1"
elseif cells(i,j).value = "y" then
date = "col2"
elseif cells(i,j).value = "z" then
num = "col3"
end if
count = count + 1
activeworkbook.worksheets("output").cells(count + 1, 1) = type
activeworkbook.worksheets("output").cells(count + 1, 2) = date
activeworkbook.worksheets("output").cells(count + 1, 3) = num
end if
next arrayElem
if cells(i,j).value = "a" then
name = "row1"
elseif cells(i,j).value = "b" then
size = "row2"
elseif cells(i,j).value = "c" then
height = "row3"
end if
activeworkbook.worksheets("output").cells(count + 2, 1) = name
activeworkbook.worksheets("output").cells(count + 2, 2) = size
activeworkbook.worksheets("output").cells(count + 2, 3) = height
next j
next i
Obviously these are dumby variables and results, but the overall structure is the same as the real code. I can see "name","size", and "height" being printed, but then they get replaced by "type", "date", and "num". How do I prevent this from happening? Each time a new event is found I need it to print its associated characteristics printed into a new row in the "output" sheet.
Consider the following simplified version of your code:
For i = 1 To 100
If x = y Then
rowNum = rowNum + 1
Cells(rowNum + 1, 1) = "A"
End If
Cells(rowNum + 2, 1) = "B"
Next
Each time through the loop you are writing out either one or two things (two if x = y is true, one if it isn't) but you are only incrementing the row number by zero or one (one if x = y is true, zero if it isn't). Even if you know that x will always equal y, you are still trying to write two rows of information out but only increasing the row counter by one.
Assuming you are not trying to replace the "B"s in my example with the "A"s from the next iteration through the loop, you should change the code to something like:
For i = 1 To 100
If x = y Then
rowNum = rowNum + 1
Cells(rowNum, 1) = "A"
End If
rowNum = rowNum + 1
Cells(rowNum, 1) = "B"
Next

vba array element removal

j = LBound(arrayTime)
Do Until j = UBound(arrayTime)
j = j + 1
b = b + 1
cnc = b + r
MsgBox cnc
If cnc > 7 Then
b = 0
r = 0
cnc = b + r
End If
numMins = Sheet5.Cells(cnc + 3, 2) - arrayTime(j)
If numMins < 0 Then
g = g + 1
ReArrangeArray arrayTime, j
'ReDim Preserve arrayTime(numrows - 1 + g)
'arrayTime(numrows - 1 + g) = arrayTime(j)
'MsgBox (arrayTime(numrows - 1 + g))
Else
Sheet5.Cells(cnc + 3, 2) = numMins
End If
Loop
If the if statement is true I want to be able to put the array value at the end of the array and remove that value from its current spot. As the code is, it just adds it to the end and increases the size of the array from 12 to 13. How can I get the array to remain size 12 and still place the value at the end of the array and then remove it from its original position? I do not want to touch the array values in front. Just want to take that value and move it to the end.
For instance
array(1,2,3,4,5)
If statement
j on third loop.
array(j)=3
end array should be
array(1,2,4,5,3)
You could use a helper Sub like this one:
Sub ReArrangeArray(inputArray as Variant, indexToSwap as long)
Dim I As Long
Dim tempVal As Variant
If indexToSwap >= LBound(inputArray) And indexToSwap < UBound(inputArray) Then
tempVal = inputArray(indexToSwap)
For I = indexToSwap To UBound(inputArray) - 1
inputArray(i) = inputArray(i + 1)
Next I
InputArray(UBound(inputArray)) = tempVal
End If
End Sub
To be called by your main Sub as follows:
ReArrangeArray arrayTime, j

Wrong quotient during division double by string

Good morning. I hate to bother you guys but I encountered with problem which really stumped me.
I have the Excel file where I have 2 columns where I must divided 1 column by another and insert this result into SQL table.
Here is the snapshot of excel file, where I underline the problem row.
If values in both columns have "," like in other rows then everything is fine, but if 1 have "," and another doesn't then I get wrong result.
For example the compiler read the 147 like 147.0 {Double} and 60,75 like "60,75"{String}. If everything is fine I should get the result 2,41, but I got 0,024 (it is like 60,75 is converted to 6075). Unfortunately I can't modify Excel file. How can I get the right result?
Here is the code for division:
Dim usedRange = xlsWorkSheet.Range("E7", "F57")
Dim usedRangeAs2DArray As Object(,) = usedRange.Value2
Dim TeamIndex(), import As String
ReDim TeamIndex(usedRange.Rows.Count)
For i As Integer = 1 To usedRange.Rows.Count
If (usedRangeAs2DArray(i, 1) = 0 And usedRangeAs2DArray(i, 2) = 0)
Or usedRangeAs2DArray(i, 2) = 0 Then
z = 1
TeamIndex(i) = z
Else
g = Convert.ToString(usedRangeAs2DArray(i, 1))
z = g / Convert.ToDouble(usedRangeAs2DArray(i, 2))
z = Math.Floor(100 * z) / 100
TeamIndex(i) = z
End If
Next
I tried to use Convert.ToString(), use Replace "." with "," and String.Format("{0:N2}", g) on first column, but this approaches simply ignored.
Thank you in advance.
I guess I found not very beautiful, but nonetheless working solution to this problem.
The main idea if I have integer value, without "," which is readed by compiler as type {Double}, I simple append ",00" string to this value.
Here is my modified code.
Dim usedRange = xlsWorkSheet.Range("E7", "F57")
Dim usedRangeAs2DArray As Object(,) = usedRange.Value2
Dim TeamIndex(), import As String
ReDim TeamIndex(usedRange.Rows.Count)
For i As Integer = 1 To usedRange.Rows.Count
If (usedRangeAs2DArray(i, 1) = 0 And usedRangeAs2DArray(i, 2) = 0)
Or usedRangeAs2DArray(i, 2) = 0 Then
z = 1
TeamIndex(i) = z
Else
If TypeOf usedRangeAs2DArray(i, 1) Is System.Double Then
usedRangeAs2DArray(i, 1) = String.Concat(usedRangeAs2DArray(i, 1), ",00")
End If
If TypeOf usedRangeAs2DArray(i, 2) Is System.Double Then
usedRangeAs2DArray(i, 2) = String.Concat(usedRangeAs2DArray(i, 2), ",00")
End If
z = usedRangeAs2DArray(i, 1) / usedRangeAs2DArray(i, 2)
z = Math.Floor(100 * z) / 100
TeamIndex(i) = z
End If
Next
UPDATE:
Found better solution, convert both values to Double and replace "," with ".".
Here is code for this:
Dim fDD, sDD As Double
If IsNumeric(usedRangeAs2DArray(i, 2)) And IsNumeric(usedRangeAs2DArray(i, 1)) Then
fDD = CDbl(usedRangeAs2DArray(i, 1).ToString().Replace(",", "."))
sDD = CDbl(usedRangeAs2DArray(i, 2).ToString().Replace(",", "."))
End If
z = fDD / sDD
z = Math.Floor(100 * z) / 100
TeamIndex(i) = z

Working with Excel ranges and arrays

In VBA, I can easily pull in an sheet\range into an array, manipulate, then pass back to the sheet\range. I'm having trouble doing this in VB.Net though.
Here's my code.
Rng = .Range("a4", .Cells(.UsedRange.Rows.Count, .UsedRange.Columns.Count))
Dim SheetArray(,) As Object = DirectCast(Rng.Value(Excel.XlRangeValueDataType.xlRangeValueDefault), Object(,))
For X As Integer = 0 To SheetArray.GetUpperBound(0)
If IsNothing(SheetArray(X, 0)) Then Exit For
SheetArray(X, 6) = SheetArray(X, 3)
SheetArray(X, 7) = CDbl(SheetArray(X, 3).ToString) - CDbl(SheetArray(X, 1).ToString) - _
CDbl(SheetArray(X, 7).ToString)
For Y As Integer = 0 To 3
SheetArray(X, Y * 2 + 1) = Math.Round(CDbl(SheetArray(X, Y * 2 + 1).ToString), 3)
Next
If Math.Abs(CDbl(SheetArray(X, 7).ToString)) > 0.1 Then _
.Range(.Cells(X + 1, 1), .Cells(X + 1, 8)).Font.Color = -16776961
Next
I'm getting an error on the first If IsNothing(SheetArray(X, 0)) Then Exit For
line. It is telling me index is out of bounds of the array. Any idea why? The SheetArray object contains the data, but I just am not sure how to get to it.
In the For you have to loop from 0 to Count - 1:
For X As Integer = 0 To SheetArray.GetUpperBound(0) - 1
'...
Next
That will fix your problem.