JohnOne

Excel Inserting Row(s)

22 posts in this topic

#1 ·  Posted (edited)

Needing to Insert some rows into an excel doc.

Went to the help file and found _Excel_RangeInsert and Example 3

The example works fine, but when I try to modify it, to insert 2 rows before row 2 in active worksheet, it only inserts 1 row.

Can anyone see what I'm doing wrong.

#include <Array.au3>
#include <Excel.au3>
#include <MsgBoxConstants.au3>

; Create application object and open an example workbook
Local $oAppl = _Excel_Open()
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeInsert Example", "Error creating the Excel application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
Local $oWorkbook = _Excel_BookOpen($oAppl, @ScriptDir & "\Extras\_Excel1.xls")
If @error Then
    MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeInsert Example", "Error opening workbook '" & @ScriptDir & "\Extra\_Excel1.xls'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    _Excel_Close($oAppl)
    Exit
EndIf
Sleep(1000)
; *****************************************************************************
; Insert 2 rows before row 2 on Active sheet
; *****************************************************************************
_Excel_RangeInsert($oWorkbook.ActiveSheet, "2:2") ; "Before row, number of rows to insert "
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.")

Actual goal is to insert rows at end of sheet, but if I cannot understand this I've no chance of other.

EDIT:

If I change the money line to...

_Excel_RangeInsert($oWorkbook.ActiveSheet, "2:3")

Then it inserts 2 rows instead of 3.

EDIT2:

I think I've established that those numbers ("2:2") I thought meant "Before row number, number of rows to insert" do not mean that at all, after trying to insert rows at end with this...

$insert =  String($oWorkbook.ActiveSheet.UsedRange.Rows.Count + 1) & ":2"
_Excel_RangeInsert($oWorkbook.ActiveSheet, $insert)
Which inserted 2000 rows into the sheet.
 
I know one thing for sure... I'm doing it wrong.
 
EDIT3:
 
Solution:
 
Problem was, I was using the completely wrong function (_Excel_RangeInsert)
Where I should have been using _Excel_RangeWrite.
Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites



Hello John,

I tried to execute the script you shared.

The problem is in this line

_Excel_RangeInsert($oWorkbook.ActiveSheet, "2:2") ; "Before row, number of rows to insert "

In the above line 2:2 indicates, adding of rows from 2nd row to 2nd row. That is the reason it inserts only one row.

If you want to add two rows this is how it should be

_Excel_RangeInsert($oWorkbook.ActiveSheet, "2:3") ; "Before row, number of rows to insert "

Good luck :)

1 person likes this

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

At least I think I got it, I'm trying to insert 2 rows at end of used rows in sheet.

But this inserts 0 rows...

$insert =  String($oWorkbook.ActiveSheet.UsedRange.Rows.Count + 1) & ":" & String($oWorkbook.ActiveSheet.UsedRange.Rows.Count + 3)
_Excel_RangeInsert($oWorkbook.ActiveSheet(), $insert)

And this inserts 3 rows.

$insert =  String($oWorkbook.ActiveSheet.UsedRange.Rows.Count) & ":" & String($oWorkbook.ActiveSheet.UsedRange.Rows.Count + 2)
_Excel_RangeInsert($oWorkbook.ActiveSheet(), $insert)

So it looks like I'm still not getting it.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Hi

As santhoshkumargr stated, the "2:2" is specified as a ExcelRange

So for your goal to insert rows at the end of sheet, you have to get where the sheet actually ends. Therefore you can use something like this:

Local $aResult = _Excel_RangeRead($oWorkbook, Default,$oWorkbook.ActiveSheet.Usedrange.Columns("A:A"))
$iCells = UBound($aResult)-1
MsgBox(0,"UsedRange in Column A",$iCells)

So you know where your sheet actually ends and then you can insert rows with something like that:

$iRows = 3 ;Number of Rows to Insert
_Excel_RangeInsert($oWorkbook.ActiveSheet, $iCells & ":" & $iCells+$iRows)





Edited by draien
1 person likes this

Share this post


Link to post
Share on other sites

OK, I think I need to enhance documentation how _Excel_RangeInsert works.

Then the bugs in the example scripts need to removed.

@JohnOne

If you want to add data to the end of the worksheet use

$oWorkbook.ActiveSheet.UsedRange.Rows.Count

add 1 and you have the row where you can write data using _Excel_RangeWrite.

This is true as long as the usedrange starts in row 1.

For details and examples please check the wiki.

2 people like this

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

#8 ·  Posted (edited)

I mentioned before water, I tried that.

Here is current code...

#include <Array.au3>
#include <Excel.au3>
#include <MsgBoxConstants.au3>

; Create application object and open an example workbook
Local $oAppl = _Excel_Open()
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeInsert Example", "Error creating the Excel application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
Local $oWorkbook = _Excel_BookOpen($oAppl, @ScriptDir & "\Extras\_Excel1.xls")
If @error Then
    MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeInsert Example", "Error opening workbook '" & @ScriptDir & "\Extra\_Excel1.xls'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    _Excel_Close($oAppl)
    Exit
EndIf

$NumRowsToInsert = 2

$rowcount = $oWorkbook.ActiveSheet().UsedRange.Rows.Count
ConsoleWrite("Start with " & $rowcount & " rows" & @LF)
$rowcount += 1 ; want to add rows below that
$rowcountplus = $rowcount + $NumRowsToInsert

ConsoleWrite("So I insert " & $NumRowsToInsert & " rows from " & $rowcount & " to " & $rowcountplus & @LF)

$insert =  String($rowcount) & ":" & String($rowcountplus)

Sleep(5000)
; *****************************************************************************
; Insert 2 rows before row 1 on worksheet 2
; *****************************************************************************
_Excel_RangeInsert($oWorkbook.ActiveSheet(), $insert, $xlShiftUp)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeInsert Example 3", "Error inserting rows." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

$NewRowCount = $oWorkbook.ActiveSheet.UsedRange.Rows.Count
ConsoleWrite("Now I have " & $NewRowCount & " rows" & @LF)

MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeInsert Example 3", "Rows successfully inserted on worksheet 2.") 

Here is the output from console..

Start with 2000 rows
So I insert 2 rows from 2001 to 2003
Now I have 2000 rows
Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Okay now I am confused.

$rowcount += 1 ; want to add rows below that

You want to add empty rows after the last row used? Why?

I mean this doesn't change a thing at all, because the rows after the last used row, are already empty.

2 people like this

Share this post


Link to post
Share on other sites

If I do not increment the row count, the output is.

Start with 2000 rows
So I insert 2 rows from 2000 to 2002
Now I have 2003 rows

But the result is not what I'd expect.

 

Isnt it doing what its supposed to? You insert from 2000:2002 (3 rows), it is what your picture shows. What do you expect it to do?

1 person likes this

Share this post


Link to post
Share on other sites

As draien said inserting rows after the last used row doesn't make sense.

As I said in post #7 use _Excel_RangeWrite.

1 person likes this

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

Example

#include <Excel.au3>
Global $aStart = [["Row1", "Col1"], ["Row2", "Col1"], ["Row3", "Col1"]]
Global $aAppend = [["Append1", "Col1"], ["Append2", "Col1"], ["Append3", "Col1"]]
Global $oExcel = _Excel_Open()
Global $oWorkBook = _Excel_BookNew($oExcel)
_Excel_RangeWrite($oWorkBook, Default, $aStart, "A1")
Global $iLastUsedRow = $oWorkBook.ActiveSheet.UsedRange.Rows.Count
_Excel_RangeWrite($oWorkBook, Default, $aAppend, "A" & $iLastUsedRow + 1)
1 person likes this

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

OK, I think I need to enhance documentation how _Excel_RangeInsert works.

I will insert the following text in the help file:

"The specified range is inserted above or left of the top left cell of the specified range depending on the value of $iShift."

Does this explain how the function works? Can someone translate it to better english? Or even make it clearer?


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

#17 ·  Posted (edited)

Here's my offering.

"The supplied range is inserted above, or to the left of the target range by default, and can be overridden using $iShift parameter, to below  or to right of target range.
 
The desired position in work sheet must be within the bounds of used range cells."
Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

You can only insert the range above or to the left based on the $iShift parameter. Inserting outside the bounds of the used range is possible but doesn't make sense.

So I suggest:

"The supplied range is inserted above or to the left of the supplied range, depending on the $iShift parameter."

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

You can insert outside the used range. The function does not return an error.

It just doesn't make sense to insert nothing into nothing and move nothing to the right or downwards.

So _Excel_RangeWrite was the function to solve your problem.


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

    • SorryButImaNewbie
      By SorryButImaNewbie
      Hello everyone!
      I try to insert a column with _excel_rangeinsert. I loop over a specified row, with different strings, and I want to insert a colum after the "OSSZEG" string (the columletter for OSSZEG is stored in the $OSSZEGoszlop variable)
      And here I have little logical problem.
      I know that this string is in column, for example, C, but is there a way to tell the UDF to insert to $vRange.
      Like:
      _Excel_RangeInsert($ExcelObject.Activesheet, $OSSZEGoszlop &":"&"D") Here I would like my script to be clever enough to know that If $OSSZEGoszlop is C the it insert between C and D, if its D its insert between D and E.
      So My basic question: Is there a String++ thing in autoit I can use for this? (If $A = "A" , $A++ = "B" or something like this)
      Alternatively I can only think of writing a  /Select Case/ for every letter and call me crazylazy but, I just don't think it is the best solution, also a that long case select would run like 2-4 seconds, which is kind of long for a column insert. (or It runs less then 0.5 sec and I'm just not very good at telling how long a script needs to be executed )
      Thanks for the help!
    • SorryButImaNewbie
      By SorryButImaNewbie
      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