Jakson

New Member
Joined
Sep 14, 2021
Messages
21
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I've got a workbook with 48 used columns. Of those 48 used columns I need to select and copy a range covering the contiguous first 29 columns. I need to be able to select which row to start the range on by using an inputbox and a find/match loop until the given string is found and a cell address is produced. See below code for my efforts so far.

VBA Code:
Private Sub NewRow1_Click()
    
    'Establish location of data to be searched through (and length) to find the corresponding clname and EDT combo
    Set oRange = Worksheets("Jan").Range("A8:A300")
    
    Answ = InputBox("Give me the name of the person you want an extra line for, last name space first name.", "Insert New Line", vbOKCancel)
    
    'String search item = this location
    SearchString = Answ ' "Bean, Peter  Costing"
    'aCell is the first search item in oRange, the column with the data
    Set aCell = oRange.Find(What:=SearchString, LookIn:=xlValues, _
                LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False)
 
    'If aCell is not nothing
    If Not aCell Is Nothing Then
        'Move aCell location value to bCell...
        Set bCell = aCell ' bCell is nothing, aCell is nothing...
        'FoundAt can now safely take aCell's location value
        FoundAt = aCell.Address 'FoundAt is nothing...
        'While ExitLoop = False keep replacing aCell addy with oRange search addys
        Do While ExitLoop = False
            'aCell now becoems the next item in oRange
            Set aCell = oRange.FindNext(After:=aCell)
            
            'If aCell is not nothing
            If Not aCell Is Nothing Then
                'If aCell, having moved down one even, and bCell are the same then exit
                If aCell.Address = bCell.Address Then Exit Do
                'FoundAt inhereted aCell's location
                FoundAt = FoundAt & ", " & aCell.Address
            Else
            
                ExitLoop = True
            End If
        Loop
    Else
        MsgBox SearchString & " not Found" ' DEBUGGING MESSAGE BOX
         ' No name matches the information from the inputbox
        Exit Sub
    End If
 
    MsgBox "The Search String has been found at: " & FoundAt 'DEBUGGING MESSAGE BOX
    
    'String manipulation ****
    Dim NewString() As String
    Dim PlaceHolder As String
    
    'String looks like: (COL ADDR)$[A](ROW ADDR)$[11], (COL ADDR)$[B](ROW ADDR)$[12]
    PlaceHolder = Replace(FoundAt, "$", "")
    'String now looks like: (COL ADDR)[A](ROW ADDR)[11], (COL ADDR)[B](ROW ADDR)[12]
    
    NewString() = Split(PlaceHolder, ",")
    'String looks like: (COL ADDR)[A](ROW ADDR)[11] _ (COL ADDR)[B](ROW ADDR)[12]
    'NewString is now the first address stored in PlaceHolder
        
        Dim FoundItem As Range ' Range of found object
            'Break the string array out
            
            'Set FoundItem = addy 1 in PlaceHolder ( This is the location of the matched item in A col )
            Set FoundItem = Range(NewString(0)) ' the addy of the string on the workbook"
    
        MsgBox "Addy: <" & FoundItem.Row & ">"
    
    Dim r As Long
    r = FoundItem.Row
    Rows(r + 1).Insert Shift(xlDown) ' this part is giving me grief lmfao
    Range(Cells(r, "A"), Cells(r, "K")).Copy Cells(r + 1, "A")
End Sub

This above code block works - but it works too well. I need to copy not the entire row, just the first 29 columns and then paste to that same dimension downward. If a person's data were sandwiched between two unlike rows of data, I need to be able to copy the below row and move it down one row to make room for the child copy of the parent row.

A9:AC9 is the uppermost bounds of my data array that needs to be manipulated by this macro. The desired behavior is for the user to enter an existing name on the spreadsheet into an Inputbox and then a new row will be populated with that same information from the parent matched-name row. When this is put into full-scale there will be several names alphabetized going down the length of the spreadsheet. Ideally it wouldn't matter where the person's name is in the list, it gets copied and all data beneath it is appropriately shoved into a new row.

Input > Find Parent Row > Copy Parent A(rownum):AC(rownum) > Paste Child A(rownum):AC(rownum) directly underneath the parent row values.

This has to work like described or I'll need to change where some of my data operations live.
 

Attachments

  • Example.PNG
    Example.PNG
    33.8 KB · Views: 14

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Forum statistics

Threads
1,214,516
Messages
6,119,980
Members
448,934
Latest member
audette89

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top