+ Reply to Thread
Results 1 to 5 of 5

CoVBA looks at first line of code and then last - nothing in between

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-02-2008
    Location
    Brisbane
    MS-Off Ver
    2016
    Posts
    811

    CoVBA looks at first line of code and then last - nothing in between

    Hi (again)

    Absolute newbie at code (any code, not just VBA). I have a sheet called MustDo, I have a sheet called Single, I have a named range called TemplateSelect which has a value of 1. When I run the code in step mode it just goes from the first line to the last without executing the middle. I realise this must be an absolutely basic mistake but I can't figure it.

    Any help appreciated

    Sub FieldTransfer()
        
    If TemplateSelect = 1 Then
        Sheets("MustDo").Select
        Range("AA13:AG15").Select
        Selection.Copy
        Sheets("Single").Select
        Range("D12").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Range("D16").Select
        
    End If
    
    End Sub
    Last edited by BRISBANEBOB; 09-16-2013 at 08:41 PM. Reason: Add code tags

  2. #2
    Forum Contributor
    Join Date
    01-03-2013
    Location
    Aberdeen, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    163

    Re: Nothing happens

    Hi

    Try to change this line:
    If TemplateSelect = 1 Then
    to this:
    If Sheets(" name of the sheet, where the TemplateSelect named range is here ").Range("TemplateSelect").Value=1 Then

  3. #3
    Forum Contributor
    Join Date
    12-02-2008
    Location
    Brisbane
    MS-Off Ver
    2016
    Posts
    811

    Re: Nothing happens

    That certainly got things moving - thank you. I thought a Named Range would be visible to everything including VBA. Obviously not the case.

    But the problem now is that I get a run-time error '1004' (Application-defined or opbject-defined error') at

    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    I am pasting it as a 'paste special value' as the range into which I am pasting has data validation BUT the data I am pasting is compliant with the validation. I assume that this is the problem but I do not want the data validation destroyed with a straight copy paste. Is this the problem? Is there away around it?

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: CoVBA looks at first line of code and then last - nothing in between

    Perhaps less selecting:

    
        Sheets("MustDo").Range("AA13:AG15").Copy
        Sheets("Single").Range("D12").PasteSpecial xlPasteValues
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  5. #5
    Forum Contributor
    Join Date
    12-02-2008
    Location
    Brisbane
    MS-Off Ver
    2016
    Posts
    811

    Re: CoVBA looks at first line of code and then last - nothing in between

    That did it. Thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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