SorryButImaNewbie

ExcelUDF _RangeInsert (SOLVED)

5 posts in this topic

#1 ·  Posted (edited)

Hello esteem users of this forum!

I have a little problem with the _excel_rangeinsert.

I organize an array in the excel (like from A:8 to S:114) with rangesort. I make it ascending according to a code first, then by a country as follow:

_Excel_RangeSort($ExcelObject, Default, $ColumnOfArrayStart & $CellNumb & ":" & $EndColumnOfArray & $MelysegSzamlalo, $KNKODoszlop & ":" & $KNKODoszlop,$xlAscending,Default,$xlYes,Default,$xlSortColumns,$RENDTAGALLAMoszlop & ":" & $RENDTAGALLAMoszlop,$xlAscending)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeSort Example 1", "Error sorting data." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
;MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeSort Report", "Data successfully sorted in range" & $ColumnOfArrayStart & $CellNumb & ":" & $EndColumnOfArray & $MelysegSzamlalo)

where $KNKODoszlop is holding the latter of the ID code or somethingcolumn, while $RENDTAGALLAMoszlop is the country code columns latter (PL, UK etc.). This happens without a glitch.

Now I try to insert 2 new rows everywhere where a) the KNKOD changes, or b ) if the KNKOD the same but the country code is different.

To do this I played around with this code: (after failing first to do both I try to make it work with one for start)

Local $CellaOlvasoSzamlalo = $CellNumb + 1
;MsgBox($MB_SYSTEMMODAL, "Értesítés", "A $CellaOlvasoSzamlalo : " & $CellaOlvasoSzamlalo )
Do
    Local $KNKODOlvaso1 = _Excel_RangeRead($ExcelObject, Default, $KNKODoszlop & $CellaOlvasoSzamlalo)
    Local $KNKODOlvaso2 = _Excel_RangeRead($ExcelObject, Default, $KNKODoszlop & $CellaOlvasoSzamlalo + 1)
        If $KNKODOlvaso1 <> $KNKODOlvaso2 Then
            _Excel_RangeInsert($ExcelObject, $CellaOlvasoSzamlalo + 1 & ":2")
        EndIf

    $CellaOlvasoSzamlalo = $CellaOlvasoSzamlalo + 1
Until $KNKODOlvaso2 = "" ;and $RENDTAGALLAMOlvaso2 = ""

So I have the $Cellnumb which contains the first row of the array im working with (the header, with titels like countrycode) from before this part of the code (Local, in the same function, I tried to cast it to Int, results are the same if I do, It should be int anyway) from it, I "calculate" "$CellaOlvasoSzamlalo" (sorry for Hungarian names :) ) Then use that to create a Do Until loop to check if the result of a cell is different from the cell beneth it in the KNKOD column, if its different, it should insert 2 rows between them. At least in my mind. After that it incrase the CellaOlvasoSzamlalo to go down to the next cell, and do this until the cell it reads is empty (and of the array).

 

I tried to play around with a few different ways, the best I could achive was that it inserted 9 rows to the 2. row. (there were 9 different KNKOD in my test excel). 

I'm not sure what mi missing, I think that the rangeinsert $Range is read as: "number1:number2" where number1 is the row where I want to insert while number2 is how many rows correct?

Anyway, I welcome any help or nudge of direction or insight if someone is willing to help.

Thank you, and have a nice life

Edited by SorryButImaNewbie
b ) was b)... it just looked a bit bad

Share this post


Link to post
Share on other sites



1 hour ago, SorryButImaNewbie said:

I'm not sure what mi missing, I think that the rangeinsert $Range is read as: "number1:number2" where number1 is the row where I want to insert while number2 is how many rows correct?

_Excel_RangeInsert needs the first and last row, calculates the number of rows and inserts them before the first specified row.
Example "5:7" inserts 3 rows (5, 6, 7) before row 5.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Thank you master water, I really start to owe you a beer :) (if you ever come to Budapest, you may get it :) )

My code is working now, well actually it stops after the first insert (since it gets empty cells), but I can handle that. Sorry for the long reaply time, I'm to tired to even finish this function, maybe tonight. anyway here is the code as it is now:

Do
    Local $KNKODOlvaso1 = _Excel_RangeRead($ExcelObject, Default, $KNKODoszlop & $CellaOlvasoSzamlalo)
    Local $KNKODOlvaso2 = _Excel_RangeRead($ExcelObject, Default, $KNKODoszlop & $CellaOlvasoSzamlalo + 1)
        If $KNKODOlvaso1 <> $KNKODOlvaso2 Then
            _Excel_RangeInsert($ExcelObject.Activesheet, $CellaOlvasoSzamlalo + 1 & ":" & $CellaOlvasoSzamlalo + 2)
            If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeInsert Example 3", "Error inserting rows." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
            MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeInsert Example 3", "Rows successfully inserted on worksheet 2.")
        EndIf
    $CellaOlvasoSzamlalo = $CellaOlvasoSzamlalo + 1
Until $KNKODOlvaso2 = "" ;and $RENDTAGALLAMOlvaso2 = ""

I also added Activesheet since @error gave back the value 1. I know its not the best solution but I will have time to make the program efficient and more user friendly once it does what I want it to do first :)

Thank you again

Share this post


Link to post
Share on other sites

:)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

:)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Similar Content

    • LoneWolf_2106
      By LoneWolf_2106
      Hi,
      i have an error:
      ==> The requested action with this object has failed.: $iRowCount = .Range(.Cells(1, 1), .Cells($oRangeLast.Row, $oRangeLast.Column)).Rows.Count $iRowCount = .Range(^ ERROR  
      My code is:
      $oRangeLast = .UsedRange.SpecialCells($xlCellTypeLastCell) Sleep(1000) $iRowCount = .Range(.Cells(1, 1), .Cells($oRangeLast.Row, $oRangeLast.Column)).Rows.Count Sleep(1000) _Excel_RangeWrite($oWorkbook_1, $oWorkbook_1.ActiveSheet,$aFileList[$i][2] , "AB3:AB"&$iRowCount) I have added some sleep because the application was crashing more often before, so i thought to slow down the code execution.
      But i didn't solve the issue.
      Has anyone an idea of what the problem might be?
      Thanks in advance.
    • water
      By water
      Extensive library to control and manipulate Microsoft Excel charts.
      Written by GreenCan and water.
      Theads: General Help & Support - Example Scripts
      BTW: If you like this UDF please click the "I like this" button. This tells me where to next put my development effort

      KNOWN BUGS (last changed: 2017-07-21)
      None. The COM error handling related bugs have been fixed.
       
    • LoneWolf_2106
      By LoneWolf_2106
      Hi,
      i have a problem with the deletion of an empty row in Excel.
      My code:
       
      If $vRow_2 = "" Then _Excel_RangeDelete($oWorkbook_1.ActiveSheet,"2", $xlShiftUp,1) EndIf I want to delete the second row. $vRow_2 is an empty cell, "A2".
      After running the code, the second row is not deleted.
      I have tried also:
       
      If $vRow_2 = Null Then _Excel_RangeDelete($oWorkbook_1.ActiveSheet,"2", $xlShiftUp,1) EndIf But it doesn't work.
      Any suggestion?
      Thanks in advance.
    • VeryGut
      By VeryGut
      I'm trying to insert the following formula in cell A2 using my script:
      =if(A1=""; "YES"; "NO")
      To my understanding, the line of code should be similar to this:
      _Excel_RangeWrite($MasterFile, Default, "=if(A1=""; "YES"; "NO")", "A2")
      However, it does not work, probably due to the multiple quotation marks that confuse the script :C
      How do I avoid this problem?
    • LoneWolf_2106
      By LoneWolf_2106
      Hi everybody,
      i have a question about Excel, i have to create several charts one below the other dynamically.
      I have thought to use:
       
      $oRangeLast = .UsedRange.SpecialCells($xlCellTypeLastCell) $iRowCount = .Range(.Cells(1, 1), .Cells($oRangeLast.Row, $oRangeLast.Column)).Rows.Count  
      And then to use it in this way:
      $Graph_position = "=Test1!A"&$iRowCount+2&":K"&$iRowCount+24 But it doesn't work with charts.
      Does anyone have a suggestion?