Conflict between innertext VBA and excel - excel

I need help scraping information from InternetExplorer to an Excel Workbook .
When i scrap, information in the innertext property is correct, but in the excel it comes wrong.
Using the code:
Workbooks("Automatizar PSA").Sheets(1).Cells(linha, 5).Value = ie.Document.getelementsbyclassname("tr-visualizar-detalhes text-center")(i + 1).innertext
I think this is caused because they assume diferent variables, i used variable inspection and got:
Watch : : ie.Document.getelementsbyclassname("tr-visualizar-detalhes text-center")(i + 1).innertext :
"06/04/2017 11:09 " : Variant/String : Módulo11.PSAScrap
Watch : : Workbooks("Automatizar PSA").Sheets(1).Cells(linha, 5).Value :
04/06/2017 11:09:00 : Variant/Date : Módulo11.PSAScrap
Any help, how can i fix that? Sorry for the way i posted, not familiar with forums, how can i paste it as code?
Thank you!!

This is a regional DMY vs. MDY issue. Your sample is DMY/MDY ambiguous so it is impossible to tell what the dates are to begin with or what your system's regional settings are.
The problem with ambiguous dates (where both the month and day-of-month are less than 13) is that Excel will interpret the DMY as MDY or vice-versa. For any others with a day greater then 12, Excel will just put the bad date in as text.
Here is a 'helper' function that will flip DMY to MDY or MDY to DMY.
function flipDMYMDY(str as string)
str = trim(str)
flipDMYMDY = mid(str, 4, 3) & left(str, 3) & right(str, 10)
end function
Use as,
Workbooks("Automatizar PSA").Sheets(1).Cells(linha, 5).Value = _
flipDMYMDY(ie.Document.getelementsbyclassname("tr-visualizar-detalhes text-center")(i + 1).innertext)

Related

VBA dots from database get loaded into textbox as comma

I know the Headline sounds odd so I will start off with a screenshot:
As you can see, the problem is that the point suddenly changes to a comma when I look up an ID in the UserForm.
Before recalling Infos, I am saving all Information rather straightforward:
with ws
Range("BH" & lastRow).value = Me.payinfoOnTime
Range("BI" & lastRow).value = Me.payinfo30
Range("BJ" & lastRow).value = Me.payinfo60
Range("BK" & lastRow).value = Me.payinfo90
Range("BL" & lastRow).value = Me.payinfo90more
End with
Recalling the respective info for a searched ID is done by:
Set FoundRange = ws.Range("D4:D500").Find(What:=Me.SearchSuppNo, LookIn:=xlValues)
With ws
Me.SEpayinfoontime = FoundRange.Offset(0, 56)
Me.SEpayinfo30 = FoundRange.Offset(0, 57)
Me.SEpayinfo60 = FoundRange.Offset(0, 58)
Me.SEpayinfo90 = FoundRange.Offset(0, 59)
Me.SEpayinfo90more = FoundRange.Offset(0, 60)
end with
The Problem is that later calculations for scores are depending on those textboxes and I constantly get an error, unless I always manually change the commas back to points.
Any ideas how I can fix this?
The line:
Me.SEpayinfoontime = FoundRange.Offset(0, 56)
is in fact:
Me.SEpayinfoontime.Value = FoundRange.Offset(0, 56).Value
When you populate an MSForms.TextBox using the .Value property (typed As Variant), like you implicitly do, and providing a number on the right side, the compiler passes the value to the TextBox as a number, and then the value is automatically converted to string inside the TextBox.
Exactly how that conversion happens does not appear to be documented, and from experiment, it would appear there is a problem with it.
When you freshly start Excel, it would appear assigning .Value will convert the number using the en-us locale, even if your system locale is different. But as soon as you go to the Control Panel and change your current locale to something else, .Value begins to respect the system locale, and changes its result depending on what is currently selected.
It should not be happening and I would see it as an Excel bug.
But if you instead assign the .Text property, the number is converted to string using the current system decimal dot, and that conversion happens outside of the TextBox, because the compiler knows .Text is a string, so it converts the right-hand side number to string beforehand.
So in your situation I would:
Make sure I always use the .Text property explicitly:
Me.SEpayinfoontime.Text = ...
Make sure I explicitly use the correct kind of functions to convert between text and numbers:
Me.SEpayinfoontime.Text = CStr(FoundRange.Offset(0, 56).Value)
MsgBox CInt(Me.SEpayinfoontime.Text) / 10
although this step is optional and represents my personal preference. Given that it's a string on the left side of the assignment, VB will use CStr automatically.
Go to Excel's settings to make sure the "Use system separators" tick is set.
Check what locale is selected in the Control Panel - Language and Regional settings.
If it is not En-Us, I would select En-Us to make sure the decimal separator is a dot there.
Restart Excel.

Why is VBA changing my decimal format?

I have created a spreadsheet with VBA functions in a PT-BR (comma as decimal) Excel. Everything works fine.
But a client (running it on EN-US) had a weird issue: decimal numbers, such as 3.88888 were copied as hole numbers, like 388888. Numbers with less decimal cases, like 2.5, get copied as 2,5, and not recognized as numbers.
My function is just copying the value, like this:
ResultsSheet.Cells(row, 3).Value=DataSheet.Cells(row, 11).value
Unfortunately, I am unable to reproduce this issue. On either language setting, I get correct results - which is what I would expect.
Anyone having experienced this, or pointing me to some information would be greatly appreciated. I would also help with clues on how to reproduce my client's issue.
As you said, quite tricky to recreate the problem, but type conversion with some checks should work, snippet below.
If InStr(DataSheet.Cells(Row, 11).Value, ",") > 0 Then
'check if text contains a comma, and if so replace with "."
ResultsSheet.Cells(Row, 3).Value = CDec(Replace(DataSheet.Cells(Row, 11).Text, ",", "."))
Else
If IsNumeric(Cells(r, 11).Value) Then
'check if numeric and ensure decimal value on ResultsSheet
ResultsSheet.Cells(Row, 3).Value = CDec(DataSheet.Cells(Row, 11).Text)
Else
'if text then just copy
ResultsSheet.Cells(Row, 3).Value = DataSheet.Cells(Row, 11).Value
End If
End If
Hope this solves your issue. Boa Sorte ^^.
Miguel

Format a date into mm.dd.yyyy format

I have a VBA script in which I am trying to convert the string in "yyyymmdd" format to "mm/dd/yyyy" format. However, when I incorporate format function to achieve this, it's showing
"Run time error-6": Overflow
Can any one help me with this ? The following is the correspondig VBA code.
// NewDate is in the format "yyyymmdd" being extracted out of a file path like "C:\Files\20140611\file.csv"
Required_format=Format(NewDate,"mm/dd/yyyy") // This line shows the error
you will have to format this yourself, because Format doesn't know that it is dealing with a date, it simply sees a string.
Use something like (psuedo code):
y = left(NewDate, 4)
m = mid(NewDate, 5, 2)
d = right(NewDate, 2)
Required_format = m + "/" + d + "/" + y
Just be absolutely sure the format is consitant. Any change (especially the padding is notorious) messes up your format.
Here is one more solution:
CDate(format(NewDate,"mm dd yyyy"))
Here's another method. As you wrote above, NewDate contains an 8 digit string representing a date in yyyymmdd format. The following "one liner" will output a date (as a string) in your desired format:
Format(Format(NewDate, "####/##/##"), "mm/dd/yyyy")

Excel : Date format issue

I have few cells where I fill date in those using 'FormatDatetime' function,
code:
Range("AX1") = FormatDateTime((Docx.getAttribute("r1ed")))
Range("AX2") = FormatDateTime((Docx.getAttribute("r2ed")))
Range("AX3") = FormatDateTime((Docx.getAttribute("r3ed")))
Range("AX4") = FormatDateTime((Docx.getAttribute("r4ed")))
If date is separated by "." all the cells would show like "12.1.2013",but if I change my system date format separated by "-","AX4" shows date as still "12.1.2013".but other shows correctly.
I need to have fix for this,since I use these dates' for calculation later in VBA.
Please suggest some answers.
I think your problem is that FormatDateTime() returns a string, change it to DateValue() instead. If the return from Docx.getAttribute() contains dots you'll need to replace them with slashes first.
So;
'[AX1] is the same as Range("AX1")
[AX1] = DateValue(Docx.getAttribute("r1ed"))
[AX2] = DateValue(Docx.getAttribute("r2ed"))
[AX3] = DateValue(Docx.getAttribute("r3ed"))
[AX4] = DateValue(Docx.getAttribute("r4ed"))
Or, if there are dots;
[AX1] = DateValue(Replace(Docx.getAttribute("r1ed"), ".", "/"))
[AX2] = DateValue(Replace(Docx.getAttribute("r2ed"), ".", "/"))
[AX3] = DateValue(Replace(Docx.getAttribute("r3ed"), ".", "/"))
[AX4] = DateValue(Replace(Docx.getAttribute("r4ed"), ".", "/"))
If this doesn't solve the issue, can you please post more info about what Docx.getAttribute() is returning please.
Edit: Also, knowing the format you need the cells to contain would be helpful - I'm assuming proper dates will be acceptable - You might need a string with a date in a certain format. If that's the case you could wrap the above with something like;
[AX1] = Format(DateValue(Docx.getAttribute("r1ed")), "dd/mm/yyyy")
It might be that FormatDateTime() is betraying you, Format() might be more flexible

Format a cell as arbitrary currency regardless of locale, using VBA

This is really bugging me as it seems pretty illogical the way it's working.
I have a macro to format a cell as a currency using a bit of code to obtain the currency symbol.
Here is the code involved:
Dim sym As String
sym = reportConstants(ISOcode)
'Just use the ISO code if there isn't a symbol available
If sym = "" Then
sym = ISOcode
End If
With range(.Offset(0, 3), .Offset(3, 3))
.NumberFormat = sym & "#,##0;(" & sym & "#,##0)"
Debug.Print sym & "#,##0;(" & sym & "#,##0)"
End With
reportConstants is a dictionary object with currency symbols defined as strings. E.g. reportConstants("USD") = "$". This is defined earlier in the macro.
When the macro runs it gets the ISO code and should then format the cell with the corresponding currency symbol.
When I run it in one instance the ISO code is "USD" - so sym is defined as "$" - but it still formats the cell with a pound sign (£). When I debug.print the format cell string it shows $#,##0;($#,##0) so, as long as I got my syntax correct, it should use a dollar sign in the cell. But it uses a £ sign instead. (I am running a UK version of excel so it may be defaulting to £-sign, but why?)
Any help greatly appreciated.
I just recorded a macro to set the format to $xx.xx and it created this: [$$-409]#,##0.00. Looks like the -409 localises the currency to a particular country; it works without it - try changing yours to .NumberFormat = "[$" & sym & "]#,##0.00"
Btw guess I read your question somewhat after posting ;) Excel is well influenced by the regional settings of your computer for currency, language, dates... Using numberformat can force it to keep the sign you require. if it is a matter of rounding up you can try to: On Excel 2010, go to File - Options - Advanced and scroll down to "When calculating this workbook" and click on the "set precision as displayed" and OK out. 
Try this: given your values are numerics/ integers/decimals....
Range("a2").Style = "Currency"
Or you can use format:
Format(value, "Currency")
Format(Range(a2).value, "Currency")
References:
http://www.mrexcel.com/forum/excel-questions/439331-displaying-currency-based-regional-settings.html
http://www.addictivetips.com/microsoft-office/excel-2010-currency-values/
(PS: I am on mobile, you may try these two links)

Resources