VBA can't find string












1















I'm asking VBA to find a string, then an ending string, and copy all the rows inbetween to paste into another sheet. However when I run the code it can't find the string. I've tested the code in a separate file using the same two strings as a start and ending point and it works just fine.



After looking online I see that the cell format could be causing this, but I don't see how to change it or even if that is the cause in this case. Any help is appreciated



Dim findrow As Long, findrow2 As Long
On Error GoTo errhandler

findrow = Range("A:A").Find("0667 John Smith", Range("A1")).Row
findrow2 = Range("A:A").Find("TTl Hrs For Employee", Range("A" & findrow)).Row

Range("A" & findrow & ":A" & findrow2).Select
Selection.Copy
Sheets("Sheet2").Select
Range("C12").Select
ActiveSheet.Paste

errhandler:
MsgBox "No Cells containing specified text found"


Picture is example of what I'm trying to copy

If I type the exact same thing in a new document the code finds it no problem. But in the original I get an code 91 error at the first "findrow" line.










share|improve this question




















  • 1





    (1) You are accessing the data of the active sheet - is this what you want? (2) No need to use Select to copy a range, read stackoverflow.com/q/10714251/7599798 (3) The On Error Goto eats up all kind of errors, check what specific error you have (4) Your error handler is executed even if everything worked, you have to issue an Exit Sub (or Exit Function) after the Paste

    – FunThomas
    Nov 15 '18 at 16:34











  • Look at adding some more arguments to the find aswell. Look at the help for find.

    – Nathan_Sav
    Nov 15 '18 at 16:39











  • Consider checking the LookIn, LookAt and MatchCase settings, or explicitly setting them in the search. Also, comment out the On Error GoTo errhandler line temporarily in case the Error Message is suitably descriptive. Beyond that - example data would help us debug.

    – Chronocidal
    Nov 15 '18 at 16:45











  • @FunThomas, Yes I want the data from the active sheet. I removed my error checker. I'm getting code 91, but I have all my objects set or at least I think I do. I'm still new to VBA. The debugger says my problem is in the first "findrow" line but I don't see how.

    – Battle Goose
    Nov 15 '18 at 16:51






  • 1





    Try to issue a Find with Excel for exactly the string. I am almost sure that it cannot be found - maybe you have extra spaces or NewLines in the cell?

    – FunThomas
    Nov 15 '18 at 17:10
















1















I'm asking VBA to find a string, then an ending string, and copy all the rows inbetween to paste into another sheet. However when I run the code it can't find the string. I've tested the code in a separate file using the same two strings as a start and ending point and it works just fine.



After looking online I see that the cell format could be causing this, but I don't see how to change it or even if that is the cause in this case. Any help is appreciated



Dim findrow As Long, findrow2 As Long
On Error GoTo errhandler

findrow = Range("A:A").Find("0667 John Smith", Range("A1")).Row
findrow2 = Range("A:A").Find("TTl Hrs For Employee", Range("A" & findrow)).Row

Range("A" & findrow & ":A" & findrow2).Select
Selection.Copy
Sheets("Sheet2").Select
Range("C12").Select
ActiveSheet.Paste

errhandler:
MsgBox "No Cells containing specified text found"


Picture is example of what I'm trying to copy

If I type the exact same thing in a new document the code finds it no problem. But in the original I get an code 91 error at the first "findrow" line.










share|improve this question




















  • 1





    (1) You are accessing the data of the active sheet - is this what you want? (2) No need to use Select to copy a range, read stackoverflow.com/q/10714251/7599798 (3) The On Error Goto eats up all kind of errors, check what specific error you have (4) Your error handler is executed even if everything worked, you have to issue an Exit Sub (or Exit Function) after the Paste

    – FunThomas
    Nov 15 '18 at 16:34











  • Look at adding some more arguments to the find aswell. Look at the help for find.

    – Nathan_Sav
    Nov 15 '18 at 16:39











  • Consider checking the LookIn, LookAt and MatchCase settings, or explicitly setting them in the search. Also, comment out the On Error GoTo errhandler line temporarily in case the Error Message is suitably descriptive. Beyond that - example data would help us debug.

    – Chronocidal
    Nov 15 '18 at 16:45











  • @FunThomas, Yes I want the data from the active sheet. I removed my error checker. I'm getting code 91, but I have all my objects set or at least I think I do. I'm still new to VBA. The debugger says my problem is in the first "findrow" line but I don't see how.

    – Battle Goose
    Nov 15 '18 at 16:51






  • 1





    Try to issue a Find with Excel for exactly the string. I am almost sure that it cannot be found - maybe you have extra spaces or NewLines in the cell?

    – FunThomas
    Nov 15 '18 at 17:10














1












1








1








I'm asking VBA to find a string, then an ending string, and copy all the rows inbetween to paste into another sheet. However when I run the code it can't find the string. I've tested the code in a separate file using the same two strings as a start and ending point and it works just fine.



After looking online I see that the cell format could be causing this, but I don't see how to change it or even if that is the cause in this case. Any help is appreciated



Dim findrow As Long, findrow2 As Long
On Error GoTo errhandler

findrow = Range("A:A").Find("0667 John Smith", Range("A1")).Row
findrow2 = Range("A:A").Find("TTl Hrs For Employee", Range("A" & findrow)).Row

Range("A" & findrow & ":A" & findrow2).Select
Selection.Copy
Sheets("Sheet2").Select
Range("C12").Select
ActiveSheet.Paste

errhandler:
MsgBox "No Cells containing specified text found"


Picture is example of what I'm trying to copy

If I type the exact same thing in a new document the code finds it no problem. But in the original I get an code 91 error at the first "findrow" line.










share|improve this question
















I'm asking VBA to find a string, then an ending string, and copy all the rows inbetween to paste into another sheet. However when I run the code it can't find the string. I've tested the code in a separate file using the same two strings as a start and ending point and it works just fine.



After looking online I see that the cell format could be causing this, but I don't see how to change it or even if that is the cause in this case. Any help is appreciated



Dim findrow As Long, findrow2 As Long
On Error GoTo errhandler

findrow = Range("A:A").Find("0667 John Smith", Range("A1")).Row
findrow2 = Range("A:A").Find("TTl Hrs For Employee", Range("A" & findrow)).Row

Range("A" & findrow & ":A" & findrow2).Select
Selection.Copy
Sheets("Sheet2").Select
Range("C12").Select
ActiveSheet.Paste

errhandler:
MsgBox "No Cells containing specified text found"


Picture is example of what I'm trying to copy

If I type the exact same thing in a new document the code finds it no problem. But in the original I get an code 91 error at the first "findrow" line.







excel vba string find






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 17:09









Chronocidal

3,0761318




3,0761318










asked Nov 15 '18 at 16:27









Battle GooseBattle Goose

62




62








  • 1





    (1) You are accessing the data of the active sheet - is this what you want? (2) No need to use Select to copy a range, read stackoverflow.com/q/10714251/7599798 (3) The On Error Goto eats up all kind of errors, check what specific error you have (4) Your error handler is executed even if everything worked, you have to issue an Exit Sub (or Exit Function) after the Paste

    – FunThomas
    Nov 15 '18 at 16:34











  • Look at adding some more arguments to the find aswell. Look at the help for find.

    – Nathan_Sav
    Nov 15 '18 at 16:39











  • Consider checking the LookIn, LookAt and MatchCase settings, or explicitly setting them in the search. Also, comment out the On Error GoTo errhandler line temporarily in case the Error Message is suitably descriptive. Beyond that - example data would help us debug.

    – Chronocidal
    Nov 15 '18 at 16:45











  • @FunThomas, Yes I want the data from the active sheet. I removed my error checker. I'm getting code 91, but I have all my objects set or at least I think I do. I'm still new to VBA. The debugger says my problem is in the first "findrow" line but I don't see how.

    – Battle Goose
    Nov 15 '18 at 16:51






  • 1





    Try to issue a Find with Excel for exactly the string. I am almost sure that it cannot be found - maybe you have extra spaces or NewLines in the cell?

    – FunThomas
    Nov 15 '18 at 17:10














  • 1





    (1) You are accessing the data of the active sheet - is this what you want? (2) No need to use Select to copy a range, read stackoverflow.com/q/10714251/7599798 (3) The On Error Goto eats up all kind of errors, check what specific error you have (4) Your error handler is executed even if everything worked, you have to issue an Exit Sub (or Exit Function) after the Paste

    – FunThomas
    Nov 15 '18 at 16:34











  • Look at adding some more arguments to the find aswell. Look at the help for find.

    – Nathan_Sav
    Nov 15 '18 at 16:39











  • Consider checking the LookIn, LookAt and MatchCase settings, or explicitly setting them in the search. Also, comment out the On Error GoTo errhandler line temporarily in case the Error Message is suitably descriptive. Beyond that - example data would help us debug.

    – Chronocidal
    Nov 15 '18 at 16:45











  • @FunThomas, Yes I want the data from the active sheet. I removed my error checker. I'm getting code 91, but I have all my objects set or at least I think I do. I'm still new to VBA. The debugger says my problem is in the first "findrow" line but I don't see how.

    – Battle Goose
    Nov 15 '18 at 16:51






  • 1





    Try to issue a Find with Excel for exactly the string. I am almost sure that it cannot be found - maybe you have extra spaces or NewLines in the cell?

    – FunThomas
    Nov 15 '18 at 17:10








1




1





(1) You are accessing the data of the active sheet - is this what you want? (2) No need to use Select to copy a range, read stackoverflow.com/q/10714251/7599798 (3) The On Error Goto eats up all kind of errors, check what specific error you have (4) Your error handler is executed even if everything worked, you have to issue an Exit Sub (or Exit Function) after the Paste

– FunThomas
Nov 15 '18 at 16:34





(1) You are accessing the data of the active sheet - is this what you want? (2) No need to use Select to copy a range, read stackoverflow.com/q/10714251/7599798 (3) The On Error Goto eats up all kind of errors, check what specific error you have (4) Your error handler is executed even if everything worked, you have to issue an Exit Sub (or Exit Function) after the Paste

– FunThomas
Nov 15 '18 at 16:34













Look at adding some more arguments to the find aswell. Look at the help for find.

– Nathan_Sav
Nov 15 '18 at 16:39





Look at adding some more arguments to the find aswell. Look at the help for find.

– Nathan_Sav
Nov 15 '18 at 16:39













Consider checking the LookIn, LookAt and MatchCase settings, or explicitly setting them in the search. Also, comment out the On Error GoTo errhandler line temporarily in case the Error Message is suitably descriptive. Beyond that - example data would help us debug.

– Chronocidal
Nov 15 '18 at 16:45





Consider checking the LookIn, LookAt and MatchCase settings, or explicitly setting them in the search. Also, comment out the On Error GoTo errhandler line temporarily in case the Error Message is suitably descriptive. Beyond that - example data would help us debug.

– Chronocidal
Nov 15 '18 at 16:45













@FunThomas, Yes I want the data from the active sheet. I removed my error checker. I'm getting code 91, but I have all my objects set or at least I think I do. I'm still new to VBA. The debugger says my problem is in the first "findrow" line but I don't see how.

– Battle Goose
Nov 15 '18 at 16:51





@FunThomas, Yes I want the data from the active sheet. I removed my error checker. I'm getting code 91, but I have all my objects set or at least I think I do. I'm still new to VBA. The debugger says my problem is in the first "findrow" line but I don't see how.

– Battle Goose
Nov 15 '18 at 16:51




1




1





Try to issue a Find with Excel for exactly the string. I am almost sure that it cannot be found - maybe you have extra spaces or NewLines in the cell?

– FunThomas
Nov 15 '18 at 17:10





Try to issue a Find with Excel for exactly the string. I am almost sure that it cannot be found - maybe you have extra spaces or NewLines in the cell?

– FunThomas
Nov 15 '18 at 17:10












3 Answers
3






active

oldest

votes


















0














Assuming the values are present, this worked just fine for me:



findrow = Range("A:A").Find("0667 John Smith", Range("A1")).Row
findrow2 = Range("A:A").Find("TTl Hrs For Employee", Range("A" & findrow)).Row
Range("A" & findrow & ":A" & findrow2).Copy Worksheets("Sheet2").Range("C12")


as long as the sheet to search was active. You may want to specify it in the code:



findrow = Worksheets("Sheet1").Range("A:A").Find("0667 John Smith", Worksheets("Sheet1").Range("A1")).Row
findrow2 = Worksheets("Sheet1").Range("A:A").Find("TTl Hrs For Employee", Worksheets("Sheet1").Range("A" & findrow)).Row
Worksheets("Sheet1").Range("A" & findrow & ":A" & findrow2).Copy Worksheets("Sheet2").Range("C12")





share|improve this answer































    0














    Update:
    Found out what was wrong with it. For some reason the Find function can't find either string in the original sheet. If I copy the whole sheet into a new sheet and then have my code search for the strings it finds them and copies them just like it should.
    I don't know what's causing it to do that and its not efficient but hey, it works. Thanks you guys for helping me trouble shoot, special thanks to @FunThomas






    share|improve this answer































      0














      use fully qualified (up to worksheet object at least) range references and always specify Find() method LookAt and LookIn parameters not to implicitly assume last method usage (even from Excel UI) ones:



      Dim firstCell As Range, lastCell As Range

      With Worksheets("MySheetName") ' reference sheet where to serach for text (change "MySheetName" to your actual sheet name)
      Set firstCell = .Range("A:A").Find("0667 John Smith", Range("A1")) ' try and find first cell
      If Not firstCell Is Nothing Then ' if first cell found
      Set lastCell = .Range("A:A").Find(what:="TTl Hrs For Employee", lookat:=xlWhole, LookIn:=xlValues, after:=firstCell) ' try find last cell
      If Not lastCell Is Nothing Then ' if last cell found
      .Range(firstCell, lastCell).Copy Sheets("Sheet2").Range("C12")
      Else
      MsgBox "No Cells containing specified 'TTl Hrs For Employee'"
      End If
      Else
      MsgBox "No Cells containing specified '0667 John Smith'"
      End If
      End With





      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323859%2fvba-cant-find-string%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        Assuming the values are present, this worked just fine for me:



        findrow = Range("A:A").Find("0667 John Smith", Range("A1")).Row
        findrow2 = Range("A:A").Find("TTl Hrs For Employee", Range("A" & findrow)).Row
        Range("A" & findrow & ":A" & findrow2).Copy Worksheets("Sheet2").Range("C12")


        as long as the sheet to search was active. You may want to specify it in the code:



        findrow = Worksheets("Sheet1").Range("A:A").Find("0667 John Smith", Worksheets("Sheet1").Range("A1")).Row
        findrow2 = Worksheets("Sheet1").Range("A:A").Find("TTl Hrs For Employee", Worksheets("Sheet1").Range("A" & findrow)).Row
        Worksheets("Sheet1").Range("A" & findrow & ":A" & findrow2).Copy Worksheets("Sheet2").Range("C12")





        share|improve this answer




























          0














          Assuming the values are present, this worked just fine for me:



          findrow = Range("A:A").Find("0667 John Smith", Range("A1")).Row
          findrow2 = Range("A:A").Find("TTl Hrs For Employee", Range("A" & findrow)).Row
          Range("A" & findrow & ":A" & findrow2).Copy Worksheets("Sheet2").Range("C12")


          as long as the sheet to search was active. You may want to specify it in the code:



          findrow = Worksheets("Sheet1").Range("A:A").Find("0667 John Smith", Worksheets("Sheet1").Range("A1")).Row
          findrow2 = Worksheets("Sheet1").Range("A:A").Find("TTl Hrs For Employee", Worksheets("Sheet1").Range("A" & findrow)).Row
          Worksheets("Sheet1").Range("A" & findrow & ":A" & findrow2).Copy Worksheets("Sheet2").Range("C12")





          share|improve this answer


























            0












            0








            0







            Assuming the values are present, this worked just fine for me:



            findrow = Range("A:A").Find("0667 John Smith", Range("A1")).Row
            findrow2 = Range("A:A").Find("TTl Hrs For Employee", Range("A" & findrow)).Row
            Range("A" & findrow & ":A" & findrow2).Copy Worksheets("Sheet2").Range("C12")


            as long as the sheet to search was active. You may want to specify it in the code:



            findrow = Worksheets("Sheet1").Range("A:A").Find("0667 John Smith", Worksheets("Sheet1").Range("A1")).Row
            findrow2 = Worksheets("Sheet1").Range("A:A").Find("TTl Hrs For Employee", Worksheets("Sheet1").Range("A" & findrow)).Row
            Worksheets("Sheet1").Range("A" & findrow & ":A" & findrow2).Copy Worksheets("Sheet2").Range("C12")





            share|improve this answer













            Assuming the values are present, this worked just fine for me:



            findrow = Range("A:A").Find("0667 John Smith", Range("A1")).Row
            findrow2 = Range("A:A").Find("TTl Hrs For Employee", Range("A" & findrow)).Row
            Range("A" & findrow & ":A" & findrow2).Copy Worksheets("Sheet2").Range("C12")


            as long as the sheet to search was active. You may want to specify it in the code:



            findrow = Worksheets("Sheet1").Range("A:A").Find("0667 John Smith", Worksheets("Sheet1").Range("A1")).Row
            findrow2 = Worksheets("Sheet1").Range("A:A").Find("TTl Hrs For Employee", Worksheets("Sheet1").Range("A" & findrow)).Row
            Worksheets("Sheet1").Range("A" & findrow & ":A" & findrow2).Copy Worksheets("Sheet2").Range("C12")






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 15 '18 at 16:39









            cybernetic.nomadcybernetic.nomad

            2,77121121




            2,77121121

























                0














                Update:
                Found out what was wrong with it. For some reason the Find function can't find either string in the original sheet. If I copy the whole sheet into a new sheet and then have my code search for the strings it finds them and copies them just like it should.
                I don't know what's causing it to do that and its not efficient but hey, it works. Thanks you guys for helping me trouble shoot, special thanks to @FunThomas






                share|improve this answer




























                  0














                  Update:
                  Found out what was wrong with it. For some reason the Find function can't find either string in the original sheet. If I copy the whole sheet into a new sheet and then have my code search for the strings it finds them and copies them just like it should.
                  I don't know what's causing it to do that and its not efficient but hey, it works. Thanks you guys for helping me trouble shoot, special thanks to @FunThomas






                  share|improve this answer


























                    0












                    0








                    0







                    Update:
                    Found out what was wrong with it. For some reason the Find function can't find either string in the original sheet. If I copy the whole sheet into a new sheet and then have my code search for the strings it finds them and copies them just like it should.
                    I don't know what's causing it to do that and its not efficient but hey, it works. Thanks you guys for helping me trouble shoot, special thanks to @FunThomas






                    share|improve this answer













                    Update:
                    Found out what was wrong with it. For some reason the Find function can't find either string in the original sheet. If I copy the whole sheet into a new sheet and then have my code search for the strings it finds them and copies them just like it should.
                    I don't know what's causing it to do that and its not efficient but hey, it works. Thanks you guys for helping me trouble shoot, special thanks to @FunThomas







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 15 '18 at 17:15









                    Battle GooseBattle Goose

                    62




                    62























                        0














                        use fully qualified (up to worksheet object at least) range references and always specify Find() method LookAt and LookIn parameters not to implicitly assume last method usage (even from Excel UI) ones:



                        Dim firstCell As Range, lastCell As Range

                        With Worksheets("MySheetName") ' reference sheet where to serach for text (change "MySheetName" to your actual sheet name)
                        Set firstCell = .Range("A:A").Find("0667 John Smith", Range("A1")) ' try and find first cell
                        If Not firstCell Is Nothing Then ' if first cell found
                        Set lastCell = .Range("A:A").Find(what:="TTl Hrs For Employee", lookat:=xlWhole, LookIn:=xlValues, after:=firstCell) ' try find last cell
                        If Not lastCell Is Nothing Then ' if last cell found
                        .Range(firstCell, lastCell).Copy Sheets("Sheet2").Range("C12")
                        Else
                        MsgBox "No Cells containing specified 'TTl Hrs For Employee'"
                        End If
                        Else
                        MsgBox "No Cells containing specified '0667 John Smith'"
                        End If
                        End With





                        share|improve this answer




























                          0














                          use fully qualified (up to worksheet object at least) range references and always specify Find() method LookAt and LookIn parameters not to implicitly assume last method usage (even from Excel UI) ones:



                          Dim firstCell As Range, lastCell As Range

                          With Worksheets("MySheetName") ' reference sheet where to serach for text (change "MySheetName" to your actual sheet name)
                          Set firstCell = .Range("A:A").Find("0667 John Smith", Range("A1")) ' try and find first cell
                          If Not firstCell Is Nothing Then ' if first cell found
                          Set lastCell = .Range("A:A").Find(what:="TTl Hrs For Employee", lookat:=xlWhole, LookIn:=xlValues, after:=firstCell) ' try find last cell
                          If Not lastCell Is Nothing Then ' if last cell found
                          .Range(firstCell, lastCell).Copy Sheets("Sheet2").Range("C12")
                          Else
                          MsgBox "No Cells containing specified 'TTl Hrs For Employee'"
                          End If
                          Else
                          MsgBox "No Cells containing specified '0667 John Smith'"
                          End If
                          End With





                          share|improve this answer


























                            0












                            0








                            0







                            use fully qualified (up to worksheet object at least) range references and always specify Find() method LookAt and LookIn parameters not to implicitly assume last method usage (even from Excel UI) ones:



                            Dim firstCell As Range, lastCell As Range

                            With Worksheets("MySheetName") ' reference sheet where to serach for text (change "MySheetName" to your actual sheet name)
                            Set firstCell = .Range("A:A").Find("0667 John Smith", Range("A1")) ' try and find first cell
                            If Not firstCell Is Nothing Then ' if first cell found
                            Set lastCell = .Range("A:A").Find(what:="TTl Hrs For Employee", lookat:=xlWhole, LookIn:=xlValues, after:=firstCell) ' try find last cell
                            If Not lastCell Is Nothing Then ' if last cell found
                            .Range(firstCell, lastCell).Copy Sheets("Sheet2").Range("C12")
                            Else
                            MsgBox "No Cells containing specified 'TTl Hrs For Employee'"
                            End If
                            Else
                            MsgBox "No Cells containing specified '0667 John Smith'"
                            End If
                            End With





                            share|improve this answer













                            use fully qualified (up to worksheet object at least) range references and always specify Find() method LookAt and LookIn parameters not to implicitly assume last method usage (even from Excel UI) ones:



                            Dim firstCell As Range, lastCell As Range

                            With Worksheets("MySheetName") ' reference sheet where to serach for text (change "MySheetName" to your actual sheet name)
                            Set firstCell = .Range("A:A").Find("0667 John Smith", Range("A1")) ' try and find first cell
                            If Not firstCell Is Nothing Then ' if first cell found
                            Set lastCell = .Range("A:A").Find(what:="TTl Hrs For Employee", lookat:=xlWhole, LookIn:=xlValues, after:=firstCell) ' try find last cell
                            If Not lastCell Is Nothing Then ' if last cell found
                            .Range(firstCell, lastCell).Copy Sheets("Sheet2").Range("C12")
                            Else
                            MsgBox "No Cells containing specified 'TTl Hrs For Employee'"
                            End If
                            Else
                            MsgBox "No Cells containing specified '0667 John Smith'"
                            End If
                            End With






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 15 '18 at 18:17









                            DisplayNameDisplayName

                            11k2720




                            11k2720






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323859%2fvba-cant-find-string%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Florida Star v. B. J. F.

                                Error while running script in elastic search , gateway timeout

                                Adding quotations to stringified JSON object values