Excel macro find range under a specific text and then copy

Hello,
I have the following macro written by one of your colleagues. It finds the latest workbook (by date) in a datafile and copies a specific range from a specific worksheet to a range in a separate workbook.
It all works fine. Now I would like an addition.
Rather than having a set range (in the case below, Range("A1:R80")), the macro should search column B until it finds the following text, “OTC FX Options Position Breakdown”, and then it should select the range immediately below, Bx:Jx+10 and copy as before.

So, if the text appears at cell B20, the range would be B21:J31

The range in the second worksheet will remain constant.

Thanks,
CC


Public Sub UpdateMARisk()

Dim strPath As String: strPath = Environ("userprofile") & "\Dropbox\Centa\Centa FXDP\FXDP daily reports\Fixings\"

Dim DestinationFile As String:
DestinationFile = "ThisWorkbook"
Dim objFS As Object, objFolder As Object
Dim objFiles As Object, objF1 As Object
Dim strFileName As String, lngFileNumb As Long, IsFileFound As Boolean
Dim FileLen As Integer, Sourcewb As Workbook, DestWb As Workbook
IsFileFound = False

Application.ScreenUpdating = False

Set objFS = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFS.GetFolder(strPath)
Set objFiles = objFolder.Files

For Each objF1 In objFiles
    If Right(objF1.Name, 3) = "xls" Then
        If IsNumeric(Left(objF1.Name, 8)) Then
            If lngFileNumb < CLng(Left(objF1.Name, 8)) Then
                lngFileNumb = CLng(Left(objF1.Name, 8))
                strFileName = objF1.Name
                IsFileFound = True
            End If
        End If
    End If
Next

Set objF1 = Nothing
Set objFiles = Nothing
Set objFolder = Nothing
Set objFS = Nothing

If IsFileFound Then

    Set Sourcewb = Workbooks.Open(strPath & strFileName, , True)

   ' Sourcewb.Sheets("MA Risk").Range("A1:R80").Copy Workbooks(DestinationFile).Sheets("MA Risk").Range("A1:R80")
    Sourcewb.Sheets("MA Risk").Range("A1:R80").Copy Workbooks("TraderPositionSheet.xlsm").Sheets("MA Risk").Range("A1:R80")
   
    Sourcewb.Close
    Set Sourcewb = Nothing
   
    'Workbooks(DestinationFile).Save
    Workbooks("TraderPositionSheet.xlsm").Save

End If
Application.ScreenUpdating = True

End Sub
CC10Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

pls try

    Set myRange = _
       Sourcewb.Sheets("MA Risk").Range("B:B").Find("OTC FX Options Position Breakdown", , , xlWhole, , True).Offset(1).Resize(10, 9)
        
    ' Sourcewb.Sheets("MA Risk").Range("A1:R80").Copy Workbooks(DestinationFile).Sheets("MA Risk").Range("A1:R80")
    myRange.Copy Workbooks("TraderPositionSheet.xlsm").Sheets("MA Risk").Range("B1")
' Or
    myRange.Copy Workbooks("TraderPositionSheet.xlsm").Sheets("MA Risk").Range(myRange.Address)

Open in new window

EDIT Choice of destination

Regards
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Maybe something like this......

Public Sub UpdateMARisk()

Dim strPath As String: strPath = Environ("userprofile") & "\Dropbox\Centa\Centa FXDP\FXDP daily reports\Fixings\"

Dim DestinationFile As String:
DestinationFile = "ThisWorkbook"
Dim objFS As Object, objFolder As Object
Dim objFiles As Object, objF1 As Object
Dim strFileName As String, lngFileNumb As Long, IsFileFound As Boolean
Dim FileLen As Integer, Sourcewb As Workbook, DestWb As Workbook

''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim found As Range
Dim r As Long
Dim Text As String
Text = "OTC FX Options Position Breakdown"
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
IsFileFound = False

Application.ScreenUpdating = False

Set objFS = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFS.GetFolder(strPath)
Set objFiles = objFolder.Files

For Each objF1 In objFiles
    If Right(objF1.Name, 3) = "xls" Then
        If IsNumeric(Left(objF1.Name, 8)) Then
            If lngFileNumb < CLng(Left(objF1.Name, 8)) Then
                lngFileNumb = CLng(Left(objF1.Name, 8))
                strFileName = objF1.Name
                IsFileFound = True
            End If
        End If
    End If
Next

Set objF1 = Nothing
Set objFiles = Nothing
Set objFolder = Nothing
Set objFS = Nothing

If IsFileFound Then

    Set Sourcewb = Workbooks.Open(strPath & strFileName, , True)
    
    ''''''''''''''''''''''''''''''''''''''''''''''''''
    Set found = Sourcewb.Sheets("MA Risk").Range("B:B").Find(what:=Text, lookat:=xlWhole, searchdirection:=xlNext, MatchCase:=False)
    If Not found Is Nothing Then
        r = found.Row + 1
    End If
    
   ' Sourcewb.Sheets("MA Risk").Range("A1:R80").Copy Workbooks(DestinationFile).Sheets("MA Risk").Range("A1:R80")
   If r > 0 Then
        Sourcewb.Sheets("MA Risk").Range("B" & r & ":J" & r + 10).Copy Workbooks("TraderPositionSheet.xlsm").Sheets("MA Risk").Range("A1:R80")
   End If
   
   '''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Sourcewb.Close
    Set Sourcewb = Nothing
   
    'Workbooks(DestinationFile).Save
    Workbooks("TraderPositionSheet.xlsm").Save

End If
Application.ScreenUpdating = True

End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You would also need to change the destination range in the above code like this.....

Workbooks("TraderPositionSheet.xlsm").Sheets("MA Risk").Range("B" & r)

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.