Consulting

Results 1 to 10 of 10

Thread: Solved: Run Macro on Specific Sheet

  1. #1
    VBAX Tutor
    Joined
    Sep 2008
    Posts
    213
    Location

    Solved: Run Macro on Specific Sheet

    I am trying to run a macro on a specific sheet ( regardless of the active sheet at the time) and have attempted the following, however it applies the macro to whatever the active sheet is. I am assuming that this is the incorrect way to achieve this. Any help would be appreciated.

    Sub Noukaibi()

    With Worksheets("Gold")

    If Range("B35").Value <= Range("L40").Value Then
    Range("B37").Value = Range("L39").Value
    End If
    End With

    End Sub

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Close:
    [vba]Sub Noukaibi()

    With Worksheets("Gold")

    If .Range("B35").Value <= .Range("L40").Value Then
    .Range("B37").Value = .Range("L39").Value
    End If
    End With

    End Sub[/vba]

    Note the periods before the Range calls.
    Be as you wish to seem

  3. #3
    VBAX Tutor
    Joined
    Sep 2008
    Posts
    213
    Location
    Thank you Aflatoon, all fixed!!

  4. #4
    VBAX Newbie
    Joined
    Dec 2020
    Posts
    3
    Location
    I am experiencing similar issues: I want to apply a macro to a specific sheet, but it is only being applied to the active sheet. I tried adding periods before the range calls (as mentioned above), but this resulted in errors for me. Any help is greatly appreciated!

    Sub Number_Conversion_Macro()

    With Worksheets("Sold to Breakout")



    Range("B1").Select
    Selection.Copy
    Range("B3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlMultiply, _
    SkipBlanks:=True, Transpose:=True


    Range("d1").Select
    Selection.Copy
    Range("d3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlMultiply, _
    SkipBlanks:=True, Transpose:=True


    Range("f1").Select
    Selection.Copy
    Range("f3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlMultiply, _
    SkipBlanks:=True, Transpose:=True

    Range("h1").Select
    Selection.Copy
    Range("h3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlMultiply, _
    SkipBlanks:=True, Transpose:=True
    End With

    End Sub

  5. #5
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    You are missing the period in front of all your commands
    .Range("B1").select
    If you don't fully qualify your commands, then it will be whatever your active sheet

  6. #6
    VBAX Newbie
    Joined
    Dec 2020
    Posts
    3
    Location
    Quote Originally Posted by JKwan View Post
    You are missing the period in front of all your commands
    .Range("B1").select
    If you don't fully qualify your commands, then it will be whatever your active sheet
    when I add the periods (see below), I receive the following error message: run-time error '1004' Select method of Range class failed




    Sub Number_Conversion_Macro()
    
    
    With Worksheets("Sold to Breakout")
    
    
    
        .Range("B1").Select
        Selection.Copy
        .Range("B3").Select
        .Range(Selection, Selection.End(xlDown)).Select
        .Range(Selection, Selection.End(xlDown)).Select
        Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlMultiply, _
            SkipBlanks:=True, Transpose:=True
            
     
        .Range("d1").Select
        Selection.Copy
        .Range("d3").Select
        .Range(Selection, Selection.End(xlDown)).Select
        .Range(Selection, Selection.End(xlDown)).Select
        Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlMultiply, _
            SkipBlanks:=True, Transpose:=True
          
      
        .Range("f1").Select
        Selection.Copy
        .Range("f3").Select
        .Range(Selection, Selection.End(xlDown)).Select
        .Range(Selection, Selection.End(xlDown)).Select
        Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlMultiply, _
            SkipBlanks:=True, Transpose:=True
        
        .Range("h1").Select
        Selection.Copy
        .Range("h3").Select
        .Range(Selection, Selection.End(xlDown)).Select
        .Range(Selection, Selection.End(xlDown)).Select
        Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlMultiply, _
            SkipBlanks:=True, Transpose:=True
    End With
          
    End Sub
    Last edited by Paul_Hossler; 12-07-2020 at 09:11 AM. Reason: Added CODE tags

  7. #7
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    Sorry, glanced too quickly. You have all those Select and Selection.... To quickly fix your problem add below to your code
    With Worksheets("Sold to Breakout")
    .Select

  8. #8
    VBAX Newbie
    Joined
    Dec 2020
    Posts
    3
    Location
    Quote Originally Posted by JKwan View Post
    Sorry, glanced too quickly. You have all those Select and Selection.... To quickly fix your problem add below to your code
    With Worksheets("Sold to Breakout")
    .Select
    that worked. Thank you very much!

  9. #9
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Quote Originally Posted by rb100 View Post
    I am experiencing similar issues: I want to apply a macro to a specific sheet, but it is only being applied to the active sheet. I tried adding periods before the range calls (as mentioned above), but this resulted in errors for me. Any help is greatly appreciated!
    1. It's better to start your own thread instead of replying to a 9 year old SOLVED one

    2. If you use the [#] icon to insert CODE and /CODE tags you can paste your macro between them to format it and to set it off
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  10. #10
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    1. You usually don't need to .Select objects to use them. The macro recorded captures them, but you should clean them up


    2. I'm not sure about the End(xlDown).End(xlDown) part, but without seeing the data, I have no suggestions

    Option Explicit
    
    
    Sub Number_Conversion_Macro()
        With Worksheets("Sold to Breakout")
    
            .Range("B1").Copy
            .Range("B3").End(xlDown).End(xlDown).PasteSpecial Paste:=xlPasteAll, Operation:=xlMultiply, SkipBlanks:=True, Transpose:=True
    
        End With
    End Sub
    Last edited by Paul_Hossler; 12-07-2020 at 09:32 AM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •