Consulting

Results 1 to 11 of 11

Thread: Solved: change sheet

  1. #1
    VBAX Regular
    Joined
    Feb 2008
    Posts
    6
    Location

    Solved: change sheet

    I have a workbook with two worksheets: accruals and Dec08_payments. I am positioned on the accruals worksheet and trying to execute the code below to put a value on the Dec_payments worksheet, which needs to be a variable. The value is put on the accruals worksheet not on Dec08_payments.

    What am I doing wrong?

    [VBA]Option Explicit
    Sub MatchToMonth()
    '
    Dim Sheetname As String
    Sheetname = InputBox("Enter tab name of month to compare.")
    MsgBox "tab month to be compared is " & Sheetname

    Sheets(Sheetname).Select
    ActiveSheet.Name = Sheetname

    Cells(1, 20).Value = "Post W/H tax Accrual (CCY)"
    End Sub[/VBA]

  2. #2
    VBAX Tutor nst1107's Avatar
    Joined
    Nov 2008
    Location
    Monticello
    Posts
    245
    Location
    Where do you get your error?

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Your code works for me.
    Tidied version
    [VBA]Option Explicit
    Sub MatchToMonth()
    '
    Dim Sheetname As String
    Sheetname = InputBox("Enter tab name of month to compare.")
    MsgBox "tab month to be compared is " & Sheetname

    With Sheets(Sheetname)
    .Activate
    .Cells(1, 20).Value = "Post W/H tax Accrual (CCY)"
    End With
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,199
    Location
    I am guessing that your cell range should be...

    Cells(20, 1).Value = "Post W/H tax Accrual (CCY)"

    rather than

    Cells(1, 20).Value = "Post W/H tax Accrual (CCY)"

    but only a guess. It works fine for me.
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  5. #5
    VBAX Tutor nst1107's Avatar
    Joined
    Nov 2008
    Location
    Monticello
    Posts
    245
    Location
    If your code is in a module, not a sheet, using Cells() without a worksheet object will do nothing. (Look at the change mdmackillop made.)

  6. #6
    VBAX Regular
    Joined
    Feb 2008
    Posts
    6
    Location
    Md,

    Your code works for me as does the verison below. But my original version does not work when I execute it from the other worksheet. The Sheets Select and ActiveSheet statements don't seem to be effective. Although when I execute my original version the literal value appears on the accruals tab but when the program finishes it is on the Dec08_payments tab.

    When you say my code works for you, are you executing it from another worksheet?

    Thanks.

  7. #7
    VBAX Regular
    Joined
    Feb 2008
    Posts
    6
    Location
    nst,

    My code is not in the modules section; it is in Sheet1, the payments worksheet. I can qualify every cell reference, if I have to do it. But I thought that the Sheets(Sheetname).select statement would position me on that tab (worksheet) and remove the need to continue qualifying cell references in my code.

    Is that my mistake? Is that not true?

  8. #8
    VBAX Tutor nst1107's Avatar
    Joined
    Nov 2008
    Location
    Monticello
    Posts
    245
    Location
    You're right. My bad.

  9. #9
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    If your code is in the Sheet module, without qualifying, the Range will be on that sheet. If the code is in a Standard module, the Range will be in the active sheet. When working between sheets, it is best to qualify, which allows the code to be run from any location. By qualifying both source and destination ranges, the values can be transferred without the need to activate any sheet.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  10. #10
    VBAX Regular
    Joined
    Feb 2008
    Posts
    6
    Location
    Md,

    I understand your comment that "When working between sheets, it is best to qualify, which allows the code to be run from any location. By qualifying both source and destination ranges, the values can be transferred without the need to activate any sheet."

    But I don't understand why my statement Sheets(Sheetname).Select doesn't activate that sheet in this example. Isn't that what it's supposed to do? Do you understand why it doesn't work?

  11. #11
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Because the code is in a Sheet module, Sheet1 is the default "parent" of the range. So this will write x to whatever sheet whose module contains the code.
    [VBA]Sub Test()
    Sheets(2).Activate
    Range("A1") = "x"
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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