+ Reply to Thread
Results 1 to 7 of 7

Insert multiple rows depends on cell value is getting error

  1. #1
    Registered User
    Join Date
    08-01-2011
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    7

    Post Insert multiple rows depends on cell value is getting error

    Hi Friends,

    I am trying to add insert rows using macro.
    L column fills with numbers. if L1 value is 5 then below need to insert 5 rows.
    I tried below Macro.
    But it getting Error.
    Please solve this and let me know whats wrong in it


    Sub InsertRowswork1()

    Dim LastNumber As Long: LastNumber = ActiveSheet.Range("L" & Rows.Count).End(xlUp).Row
    While LastNumber >= 2
    If Not IsEmpty(ActiveSheet.Range("L" & LastNumber)) Then
    Dim NewRows As Long: NewRows = ActiveSheet.Range("L" & LastNumber).Value
    Dim InsertIndex As Long: InsertIndex = 1
    While InsertIndex <= NewRows
    ActiveSheet.Range("L" & LastNumber + 1).EntireRow.Insert
    InsertIndex = InsertIndex + 1
    Wend
    End If
    LastNumber = LastNumber - 1
    Wend

    End Sub

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Insert multiple rows depends on cell value is getting error

    Hi, Dharmadme,

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    08-01-2011
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Insert multiple rows depends on cell value is getting error

    Thank you Holger,
    I have changed as per Forum Rule.

    I got an error on highlighted row below.
    Hints says NewRows = 0 LastNumber = 106 which are not have any data
    Then I found hidden values are there in my sheet which are populate with formula and get no value in it. then using paste special value removed formula.
    where formula result is "", these cells are includes in count after removed formula.

    thats the reason Last number went to empty rows which are not containing any data
    is that problem?
    how can i remove hidden values in cells and change if any bugs in below code



    Please Login or Register  to view this content.

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Insert multiple rows depends on cell value is getting error

    Hi, Dharmadme,

    you could have correctesd the coding in the opening post.

    The information about formulas in the cells should have been given in the opening post as the missing of it may mislead by trying to find a solution:
    Please Login or Register  to view this content.
    Ciao,
    Holger

  5. #5
    Registered User
    Join Date
    08-01-2011
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Insert multiple rows depends on cell value is getting error

    Hi Holger,

    when i try your code its inserting number of columns mentioned in first row.
    But it should be continue till end of data.

    I have more than 100 rows with different values for inserting number of columns

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Insert multiple rows depends on cell value is getting error

    Hi, Dharmadme,

    Iīm sorry but my code isnīt inserting any Columns but Rows. And I canīt make up what you mean by moving to the end of the data - I would suggest you attach a small workbook showing situation as is and outcome as wanted.

    Ciao,
    Holger

  7. #7
    Registered User
    Join Date
    08-01-2011
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Insert multiple rows depends on cell value is getting error

    Hi Holger,
    Thanks for your response.

    I have cleared and got what i expected using below code

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Insert Multiple rows based on the values in the cell
    By rlamba in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-13-2014, 09:27 AM
  2. [SOLVED] Insert multiple rows depending on cell value
    By AR-51 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-03-2013, 04:18 AM
  3. [SOLVED] Insert Multiple Rows Based Off Number in Cell and Copy Data From Above New Rows
    By tstell1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-02-2012, 04:15 PM
  4. Insert multiple rows from one sheet to another based on a value in a cell
    By sashikanth2274 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-14-2010, 11:37 PM
  5. Compare 2 text colomns and match rows depends by cell content
    By PRONET in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-31-2006, 11:58 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1