+ Reply to Thread
Results 1 to 24 of 24

Inserting Rows with Specific Data in Multiple Designated Locations

Hybrid View

  1. #1
    Registered User
    Join Date
    01-24-2014
    Location
    Scottsdale
    MS-Off Ver
    Excel 2013
    Posts
    35

    Inserting Rows with Specific Data in Multiple Designated Locations

    Customer Profilesv4 - Paul 7.24.14.xlsx

    In the CurrentCustomer WS, Column B is "Commodity", I need to insert a new row for the commodity "Red Kale" between rows 6 & 7 repeating every 23 rows down to 446 & 447. Is there a simple way to do this without manually copying and inserting every row manually?

    Thanks,

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Inserting Rows with Specific Data in Multiple Designated Locations

    Hi ExcelDummy

    This can be done with VBA Code. Do you need to do the same for DevelopingCustomers?

    Will this be a tool you'll need to use again for additional Commodities?
    Last edited by jaslake; 08-09-2014 at 01:44 PM.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Registered User
    Join Date
    01-24-2014
    Location
    Scottsdale
    MS-Off Ver
    Excel 2013
    Posts
    35

    Re: Inserting Rows with Specific Data in Multiple Designated Locations

    Jadlake,

    Thanks for the response. This will need to be done on DevelopingCustomers and yes it would be a tool I would use again when adding additional commodities.

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Inserting Rows with Specific Data in Multiple Designated Locations

    Hi ExcelDummy

    Let me play with it a bit...see what I can come up with.

  5. #5
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Inserting Rows with Specific Data in Multiple Designated Locations

    Hi ExcelDummy

    Create a Test Folder...place both of the attached Workbooks in that Test Folder. Open the TestBook v1.1.xlsm. Click the Button.

    The Code will ask you which Customer Profile Workbook you wish to modify. Select that Workbook (only one in this Test Folder...Customer Profilesv4 - Paul 7.24.14.xlsx)

    The Code will then ask you to select the Row ABOVE which you wish to Insert a New Commodity. It'll further ask you to Name the New Commodity.

    This Code is in TestBook and can be run on any Customer Profile Workbook that's of the SAME STRUCTURE.
    Option Explicit
    
    Sub Insert_Stuff()
      Dim MyPath As String, nCell As String, sAddress As String
      Dim wbSrc As Workbook
      Dim wsSrc As Variant
      Dim rng As Range, rCell As Range
      Dim LR As Long, YesNo As Long
      Dim Filter As String, Title As String
      Dim FilterIndex As Long
      Dim Filename As Variant
    
      MyPath = ThisWorkbook.Path & "\"
      ' File filters
      Filter = "Excel Files (*.xls*),*.xls*," & _
               "Text Files (*.txt),*.txt," & _
               "All Files (*.*),*.*"
      ' Default Filter to *.xls*
      FilterIndex = 1
      ' Set DialogCaption
      Title = "Select a File to Open"
      ChDir MyPath
      With Application
        ' Set File Name to selected File
        Filename = .GetOpenFilename(Filter, FilterIndex, Title)
      End With
      If Filename = False Then
        MsgBox "No file was selected."
        Exit Sub
      End If
      Workbooks.Open Filename
    
      Set wbSrc = ActiveWorkbook
      wbSrc.Activate
    
      On Error Resume Next
      Set rCell = Application.InputBox _
                  (prompt:="Please Click on the Row Where" & vbCrLf & "New Item Should be Inserted.", _
                   Title:="Add Item Here", Type:=8)
      On Error GoTo 0
      Application.DisplayAlerts = True
    
      If rCell Is Nothing Then
        Exit Sub
      End If
    
      If rCell.Address = "$B$3" Then
        MsgBox "Can't do that here!!!"
        Exit Sub
      End If
    
    
      nCell = Application.InputBox _
              (prompt:="Please Type the Name " & vbCrLf & "of the New Commodity.", _
               Title:="New Item Namee", Type:=2)
      On Error GoTo 0
      Application.DisplayAlerts = True
    
      If nCell = "" Then
        Application.ScreenUpdating = True
        Exit Sub
      End If
    
      YesNo = MsgBox(nCell & " will be inserted before every occurance " & rCell, vbYesNo + vbCritical)
    
      Application.ScreenUpdating = False
      Select Case YesNo
      Case vbYes
        With wbSrc
          For Each wsSrc In Array("CurrentCustomers", "DevelopingCustomers")
            With Sheets(wsSrc)
              .Activate
              LR = .Range("B" & .Rows.Count).End(xlUp).Row
    
              Set rng = .Range("B3:B" & LR).Find( _
                        what:=rCell, LookIn:=xlValues, lookat:=xlWhole, _
                        searchorder:=xlByRows)
              If rng Is Nothing Then
                Beep
                MsgBox prompt:="search string not found!"
                Application.ScreenUpdating = True
                Exit Sub
              End If
    
              sAddress = rng.Offset(1, 0).Address
              While .Range(rCell.Address) <> sAddress
                .Range(rng.Address).EntireRow.Insert
                .Range(rng.Address).Offset(-1, 0).Value = nCell
                LR = LR + 1
                Set rng = Cells.FindNext(After:=rng)
                If rng.Address = sAddress Then GoTo NextwsSrc
              Wend
            End With
    NextwsSrc:
          Next wsSrc
        End With
      Case vbNo
        Application.ScreenUpdating = True
        Exit Sub
      Case Else
        Application.ScreenUpdating = True
        Exit Sub
      End Select
      Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    01-24-2014
    Location
    Scottsdale
    MS-Off Ver
    Excel 2013
    Posts
    35

    Re: Inserting Rows with Specific Data in Multiple Designated Locations

    Jaslake,

    Thanks, but I keep getting a runtime error 13 - type mismatched?

  7. #7
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Inserting Rows with Specific Data in Multiple Designated Locations

    Hi ExcelDummy

    On what line of Code
    runtime error 13 - type mismatched

  8. #8
    Registered User
    Join Date
    01-24-2014
    Location
    Scottsdale
    MS-Off Ver
    Excel 2013
    Posts
    35

    Re: Inserting Rows with Specific Data in Multiple Designated Locations

    Yes, I am using Excel 2013

  9. #9
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Inserting Rows with Specific Data in Multiple Designated Locations

    Alright...I'll try to get some help from a Member that has access to 2013...

  10. #10
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Inserting Rows with Specific Data in Multiple Designated Locations

    just for test purpose change the DIM for Response to Variant and see what that does
    Ernest

    Please consider adding a * if I helped

    Nothing drives me crazy - I'm always close enough to walk....

  11. #11
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Inserting Rows with Specific Data in Multiple Designated Locations

    Hi ExcelDummy

    The Code in the attached incorporates the change suggested by Ernest. Ernest appears to have Excel 2010 and the Code performs as expected in 2010.

    Replace the old File with this File. Run the Code...let us know...
    Attached Files Attached Files

  12. #12
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Inserting Rows with Specific Data in Multiple Designated Locations

    thanks John

  13. #13
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Inserting Rows with Specific Data in Multiple Designated Locations

    @Ernest

    You're welcome...don't suppose you have access to Excel 2013???

  14. #14
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Inserting Rows with Specific Data in Multiple Designated Locations

    nope...not really sure I want too....I figured Variant will cast the data type to whatever it needs....AND you can look in the locals window and see what the system is setting the data type too....yes, my English syntax sucks....

  15. #15
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Inserting Rows with Specific Data in Multiple Designated Locations

    @Ernest

    I had previously tested Variant in 2007 and 2010 and, of course, it worked. We'll see if 2013 likes it...hopefully.

    Thanks for the input.

  16. #16
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Inserting Rows with Specific Data in Multiple Designated Locations

    Original code works in xl2013.

    @exceldummy, can you check if you have any missing references.
    With the code workbook open,
    ALT+F11 to go to VBE
    Tools > References.

    Any missing references will appear near the top of the list.
    Cheers
    Andy
    www.andypope.info

  17. #17
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Inserting Rows with Specific Data in Multiple Designated Locations

    You must select only one cell and not whole row - this will cause Type Mismatch error.
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  18. #18
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Inserting Rows with Specific Data in Multiple Designated Locations

    @Izandol

    I believe you may have discovered the issue...hope the OP hasn't given up.

+ 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. Replies: 1
    Last Post: 07-05-2014, 04:07 PM
  2. insert multiple blank rows at designated cells
    By tessawanders in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-13-2014, 06:37 AM
  3. [SOLVED] Macro for inserting specific rows with data when there is a change in the column
    By KILOJulz in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-08-2013, 04:19 AM
  4. Set autofit property for designated rows on multiple sheets
    By mandora in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-08-2013, 09:37 PM
  5. Inserting multiple rows in excel with data in consecutive rows
    By technotronic in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-20-2005, 11:05 AM

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