Finding a value, then copying and pasting cells around it to another sheet

cboyce44

New Member
Joined
Oct 3, 2023
Messages
19
Office Version
  1. 365
Platform
  1. Windows
Hello. I am a novice with Excel VBA. I have a certificate excel workbook that contains 34 sheets. So far I have it where the user imports data from a PDF document into a sheet called "PitchGageData". What I'm trying to do is search in the "PitchGageData" tab to look for "L1". Once it finds which cell it is in (depending on the pdf file, it moves around), I then want to copy that cell. I need the "L1", then the 2 values below it. Then I need the same for "L2", "L3", "L4", "L5", and "L6". Below is a screen shot of how the pdf was imported into the sheet.

1696378737186.png


Once I have copied all the values in the cells, I want to paste them to another worksheet called "Nominal Error Calculations" into cell A60, and look like this:

1696379113695.png


Thank you for your help. I have spent days trying to figure this out.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
See if this works for you:

VBA Code:
Sub FindAndCopy()

    Dim wsSrc As Worksheet, wsDest As Worksheet
    Dim rngSrc As Range, rngDest As Range, rCell As Range
    Dim rowSrc As Long
    Dim colLast As Long
    Dim cntItem As Long
    Dim arrDest As Variant
    Dim i As Long
    
    Set wsSrc = Worksheets("PitchGageData")
    Set wsDest = Worksheets("Nominal Error Calculations")
    Set rngDest = wsDest.Range("A60")
    
    ' Try xlWhole first change to xlPart if required
    Set rngSrc = wsSrc.UsedRange.Find(What:="L1", LookIn:=xlFormulas, LookAt:=xlWhole, _
                                        SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                                        MatchCase:=False, SearchFormat:=False)
                                        
    If rngSrc Is Nothing Then Exit Sub
    
    With wsSrc
        colLast = .Cells(rngSrc.Row, Columns.Count).End(xlToLeft).Column
    End With
    
    Set rngSrc = rngSrc.Resize(1, colLast - rngSrc.Column + 1)
    cntItem = WorksheetFunction.CountA(rngSrc)
    
    ReDim arrDest(1 To 3, 1 To cntItem)
    
    For Each rCell In rngSrc
        If rCell <> "" Then
            i = i + 1
            arrDest(1, i) = rCell.Value
            arrDest(2, i) = rCell.Offset(1).Value
            arrDest(3, i) = rCell.Offset(2).Value
        End If
    Next rCell
    
    rngDest.Resize(3, i).Value = arrDest
                    
End Sub
 
Upvote 0
See if this works for you:

VBA Code:
Sub FindAndCopy()

    Dim wsSrc As Worksheet, wsDest As Worksheet
    Dim rngSrc As Range, rngDest As Range, rCell As Range
    Dim rowSrc As Long
    Dim colLast As Long
    Dim cntItem As Long
    Dim arrDest As Variant
    Dim i As Long
   
    Set wsSrc = Worksheets("PitchGageData")
    Set wsDest = Worksheets("Nominal Error Calculations")
    Set rngDest = wsDest.Range("A60")
   
    ' Try xlWhole first change to xlPart if required
    Set rngSrc = wsSrc.UsedRange.Find(What:="L1", LookIn:=xlFormulas, LookAt:=xlWhole, _
                                        SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                                        MatchCase:=False, SearchFormat:=False)
                                       
    If rngSrc Is Nothing Then Exit Sub
   
    With wsSrc
        colLast = .Cells(rngSrc.Row, Columns.Count).End(xlToLeft).Column
    End With
   
    Set rngSrc = rngSrc.Resize(1, colLast - rngSrc.Column + 1)
    cntItem = WorksheetFunction.CountA(rngSrc)
   
    ReDim arrDest(1 To 3, 1 To cntItem)
   
    For Each rCell In rngSrc
        If rCell <> "" Then
            i = i + 1
            arrDest(1, i) = rCell.Value
            arrDest(2, i) = rCell.Offset(1).Value
            arrDest(3, i) = rCell.Offset(2).Value
        End If
    Next rCell
   
    rngDest.Resize(3, i).Value = arrDest
                   
End Sub
I had to change it to xlPart for anything to work. However, it only brought in this? I tried to figure out how to import just the results I need from the pdf, but I don't know how to do that.
 

Attachments

  • Results.png
    Results.png
    109.8 KB · Views: 8
Upvote 0
I had to change it to xlPart for anything to work. However, it only brought in this? I tried to figure out how to import just the results I need from the pdf, but I don't know how to do that.
I think there must be another "L1" before the "L1" I am looking for on the page?
 
Upvote 0
I had to change it to xlPart for anything to work. However, it only brought in this? I tried to figure out how to import just the results I need from the pdf, but I don't know how to do that.
Thanks! That worked!
 
Upvote 0
See if this works for you:

VBA Code:
Sub FindAndCopy()

    Dim wsSrc As Worksheet, wsDest As Worksheet
    Dim rngSrc As Range, rngDest As Range, rCell As Range
    Dim rowSrc As Long
    Dim colLast As Long
    Dim cntItem As Long
    Dim arrDest As Variant
    Dim i As Long
   
    Set wsSrc = Worksheets("PitchGageData")
    Set wsDest = Worksheets("Nominal Error Calculations")
    Set rngDest = wsDest.Range("A60")
   
    ' Try xlWhole first change to xlPart if required
    Set rngSrc = wsSrc.UsedRange.Find(What:="L1", LookIn:=xlFormulas, LookAt:=xlWhole, _
                                        SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                                        MatchCase:=False, SearchFormat:=False)
                                       
    If rngSrc Is Nothing Then Exit Sub
   
    With wsSrc
        colLast = .Cells(rngSrc.Row, Columns.Count).End(xlToLeft).Column
    End With
   
    Set rngSrc = rngSrc.Resize(1, colLast - rngSrc.Column + 1)
    cntItem = WorksheetFunction.CountA(rngSrc)
   
    ReDim arrDest(1 To 3, 1 To cntItem)
   
    For Each rCell In rngSrc
        If rCell <> "" Then
            i = i + 1
            arrDest(1, i) = rCell.Value
            arrDest(2, i) = rCell.Offset(1).Value
            arrDest(3, i) = rCell.Offset(2).Value
        End If
    Next rCell
   
    rngDest.Resize(3, i).Value = arrDest
                   
End Sub
Alex. This worked. However sometimes when it transposes the pdf document to excel sheet, it puts "LI" instead of "L1". Is there a way that I can search for "LI" as well as "L1"? Just in case this happens?
 
Upvote 0
Sure but is L1 or Ll appearing in multiple places ? Is L2 more reliable ? Is the spacing between L1 and L2 consistent ?
 
Last edited:
Upvote 0
Alex. This worked. However sometimes when it transposes the pdf document to excel sheet, it puts "LI" instead of "L1". Is there a way that I can search for "LI" as well as "L1"? Just in case this happens?
LI is probably even more of a risk of appearing multiple times in the data but see if this helps:
VBA Code:
Sub FindAndCopy_v02()

    Dim wsSrc As Worksheet, wsDest As Worksheet
    Dim rngSrc As Range, rngDest As Range, rCell As Range
    Dim rowSrc As Long
    Dim colLast As Long
    Dim cntItem As Long
    Dim arrDest As Variant
    Dim i As Long
    
    Set wsSrc = Worksheets("PitchGageData")
    Set wsDest = Worksheets("Nominal Error Calculations")
    Set rngDest = wsDest.Range("A60")
    
    ' OP is using xlPart (xlWhole would be safer)
    ' Look for L1
    Set rngSrc = wsSrc.UsedRange.Find(What:="L1", LookIn:=xlFormulas, LookAt:=xlPart, _
                                        SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                                        MatchCase:=False, SearchFormat:=False)
                                        
    If rngSrc Is Nothing Then
        ' Try alternative LI - OCR misreading 1
        ' if LI is repeats in Data try xlWhole or go straight to trying L2, L3 etc
        Set rngSrc = wsSrc.UsedRange.Find(What:="LI", LookIn:=xlFormulas, LookAt:=xlPart, _
                                    SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                                    MatchCase:=False, SearchFormat:=False)
                                    
        If rngSrc Is Nothing Then
            ' Try L2
            Set rngSrc = wsSrc.UsedRange.Find(What:="L2", LookIn:=xlFormulas, LookAt:=xlPart, _
                            SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                            MatchCase:=False, SearchFormat:=False)
            Set rngSrc = rngSrc.Offset(, -3)
        End If
    End If

    If rngSrc Is Nothing Then Exit Sub
    
    With wsSrc
        colLast = .Cells(rngSrc.Row, Columns.Count).End(xlToLeft).Column
    End With
    
    Set rngSrc = rngSrc.Resize(1, colLast - rngSrc.Column + 1)
    cntItem = WorksheetFunction.CountA(rngSrc)
    
    ReDim arrDest(1 To 3, 1 To cntItem)
    
    For Each rCell In rngSrc
        If rCell <> "" Then
            i = i + 1
            arrDest(1, i) = rCell.Value
            arrDest(2, i) = rCell.Offset(1).Value
            arrDest(3, i) = rCell.Offset(2).Value
        End If
    Next rCell
    
    rngDest.Resize(3, i).Value = arrDest
                    
End Sub
 
Upvote 0
Yeah. that didn't really work either. What if I searched for "Pitch of pin" then copy paste the values under it? would that be easier? Because I'm looking throughout the document, and there are multiple "L2" values before the one I'm looking for. Here is what the chart looks like that I'm trying to get the values from:

1697122827973.png
 
Upvote 0
That is image is very different to the one in Post #1.
In post #1 I would have looked for something like "mutual difference of diameter".
Also the latest post looks like it has merged cells.

1) Have you tried reading it in using Power Query (Data > From File > From PDF) it may give you a cleaner result
2) If that is not an option we need a representative sample of how the pdf looks once you get it into Excel.
Either copying it here using XL2BB or putting a sample on a share service like google drive, drop box etc.
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,963
Members
449,094
Latest member
Anshu121

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