VBA translation, C# Linq extension method and Switch speed
Posted: Wed May 05, 2021 10:17 pm
1 In my Excel reader, this will also read empty rows. E.g. when I have 300 rows, select 295 rows and delete these, this will still be 300 while I would say that it is not a UsedRange
oRange:=oWorkSheet:UsedRange
nRowCount :=oRange:Rows.Count
Now in this site there's a well illustrated sample of how to move to the last row:
https://www.wallstreetmojo.com/vba-row-count/
Basically it is, in VBA:
No_Of_Rows = Range("A1").End(xlDown)
But when when I type this in X#, End autocompletes but contrary to the picture in the website, opening a bracket does not show me the 3 Direction As XlDirection options XlToLeft,xlToRight and XlUp. When I try this nevertheless:
ni:=oRange.End(xlDown)
I get -with a . as well as with a : -
Error XS0103 The name 'xlDown' does not exist in the current context
and
Error XS0118 'oRange' is a variable but is used like a type
What should I do differently?
2 I also used ILSpy to convert this method to find empty rows:
public static bool IsDataRowEmpty(this DataRow row)
{
return row == null || row.ItemArray.All(i => i is DBNull);
}
XLSpy translates this to:
Public Static Method IsDataRowEmpty(Self row As DataRow ) As Logic
Return row?:ItemArray:All({i As Object => i Astype DBNull}) ?? True
But I get this error, on the two question marks:
XS9002 Parser: unexpected input ??
What is wrong here? And why does ILSpy give an non working translation?
Finally, following the discussion I had about the advantages of Switch I changed:
Do Case
Case aFieldType[ni]=="C"
odb:Fieldput(ni,cValue)
(..etc)
ENDCASE
to
cFieldType:=alltrim(aFieldType[ni])
Switch (cFieldType)
Case "C"
I used my stopwatch; on the same sheet with DO CASE took 30 seconds to read and with the Switch statement it took 31 seconds. So much for speed gains with new statements
Dick
oRange:=oWorkSheet:UsedRange
nRowCount :=oRange:Rows.Count
Now in this site there's a well illustrated sample of how to move to the last row:
https://www.wallstreetmojo.com/vba-row-count/
Basically it is, in VBA:
No_Of_Rows = Range("A1").End(xlDown)
But when when I type this in X#, End autocompletes but contrary to the picture in the website, opening a bracket does not show me the 3 Direction As XlDirection options XlToLeft,xlToRight and XlUp. When I try this nevertheless:
ni:=oRange.End(xlDown)
I get -with a . as well as with a : -
Error XS0103 The name 'xlDown' does not exist in the current context
and
Error XS0118 'oRange' is a variable but is used like a type
What should I do differently?
2 I also used ILSpy to convert this method to find empty rows:
public static bool IsDataRowEmpty(this DataRow row)
{
return row == null || row.ItemArray.All(i => i is DBNull);
}
XLSpy translates this to:
Public Static Method IsDataRowEmpty(Self row As DataRow ) As Logic
Return row?:ItemArray:All({i As Object => i Astype DBNull}) ?? True
But I get this error, on the two question marks:
XS9002 Parser: unexpected input ??
What is wrong here? And why does ILSpy give an non working translation?
Finally, following the discussion I had about the advantages of Switch I changed:
Do Case
Case aFieldType[ni]=="C"
odb:Fieldput(ni,cValue)
(..etc)
ENDCASE
to
cFieldType:=alltrim(aFieldType[ni])
Switch (cFieldType)
Case "C"
I used my stopwatch; on the same sheet with DO CASE took 30 seconds to read and with the Switch statement it took 31 seconds. So much for speed gains with new statements
Dick