Question Man
Question Man

Reputation: 85

Javascript Insert Value to the Next Blank Row

I have this code in which I have bent as much but can't still get it to work.

    <HTML>
    <HEAD>
    <TITLE></TITLE>
    <script language="javascript">
    function testing() {
    var strYourFile   = "C:\\Users\\MilesF\\Desktop\\design prop\\form.xls";
    var strYourSheet = "Sheet1";

    var objExcel     = new ActiveXObject("Excel.Application");     
    var objWorkbook                 = objExcel.Workbooks.Open(strYourFile);
    var objWorksheet                = objWorkbook.Worksheets(strYourSheet);
    var objRange = objWorksheet.UsedRange;

    var name = document.getElementById('txtName').value;
    var row = 3;
    var getrow = 1;

    while (getrow != "")
    {
    if (getrow != ""){
    getrow = objWorksheet.Cells(row, 1).value;
    row++;
    }
    else{
    objWorksheet.Cells(row, 1).Value = name;

    }
    }
    objWorkbook.Save

    objWorkbook.Close

    objExcel.Quit

    }
    </script>
    </HEAD>
    <BODY>
    <input type="text" id="txtName">
    <button onclick="testing()">Ok</button>



    </BODY>
    </HTML>

What I want to do is to find the latest row that is empty. So I declared the while statement, what I want it to do is to find the latest empty row in the excel file. The if statement is for when the program finds the blank row, it will insert the value that I have input in the field.

Any help would be great.

Upvotes: 0

Views: 1595

Answers (2)

SharpEdge
SharpEdge

Reputation: 1762

Your while is telling the code to cycle until getrow != "", so when getrow become empty it will exit from the loop never executing the else part.

while (getrow != "") {
    getrow = objWorksheet.Cells(row, 1).value;
  
    if(getrow != "") {
      row++;
    } else {
      break;
    }
}

objWorksheet.Cells(row, 1).Value = name;

Upvotes: 1

stenci
stenci

Reputation: 8481

The UsedRange object is as wide as all the used cells.

objWorksheet.UsedRange.Rows.Count + 1

Upvotes: 1

Related Questions