+ Reply to Thread
Results 1 to 8 of 8

Activate Worksheet Using Concatenated Strings for Name

Hybrid View

Gos-C Activate Worksheet Using... 04-13-2012, 01:07 PM
111StepsAhead Re: Activate Worksheet Using... 04-13-2012, 01:13 PM
Gos-C Re: Activate Worksheet Using... 04-13-2012, 01:19 PM
JBeaucaire Re: Activate Worksheet Using... 04-13-2012, 01:54 PM
Gos-C Re: Activate Worksheet Using... 04-13-2012, 02:08 PM
Gos-C Re: Activate Worksheet Using... 04-13-2012, 07:40 PM
JBeaucaire Re: Activate Worksheet Using... 04-13-2012, 08:39 PM
Gos-C Re: Activate Worksheet Using... 04-15-2012, 01:46 PM
  1. #1
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408

    Activate Worksheet Using Concatenated Strings for Name

    Hi all,

    I am trying to create a macro to cycle through four worksheets and, if a date in a column is within a specified range, copy various cells in that record and paste them in another sheet in different workbook. For example, if column K >= 01-Jan-2012 and <= 31-Mar-2012, copy columns A, F, G, P, T and W and paste them in columns A, F, D, B, E and C of the other specified sheet.

    The four sheets are named in a ABC2011, XYZ2011, ABC2012 and XYZ2012. Each year, two the names end with the previous year and the other two end with the current year.

    I have not been able to activate the sheet (using strings to identify it).

    Here is my code, so far:

    Option Explicit
    Sub Activate_Sheet()
    'Check that the ABC Packages workbook is opened.
    Dim wBook As Workbook
    Dim wks1 As String, wks2 As Worksheet, wks3 As Worksheet, wks4 As Worksheet
    Dim sh1 As String, sh2 As String, x As Worksheet
    
    On Error Resume Next
    Set wBook = Application.Workbooks("ABC PACKAGES.xlsb")
    
    sh1 = Chr(34) & "ABC"
    sh2 = (DatePart("yyyy", Date)) & Chr(34)
    wks1 = "Worksheets(" & sh1 & sh2 & ")"
    
    x = wks1
    C:\Documents and Settings\My Name\Desktop\ABC PACKAGES.xlsm
    If wBook Is Nothing Then 'Not open
    Application.Workbooks.Open ("C:\Documents and Settings\My Name\Desktop\ABC PACKAGES.xlsm"), Password:="Password"
    
    Set wBook = Nothing
    
    On Error GoTo 0
    
    Else 'It is open
    
    Application.Workbooks(wBook).Activate
    Application.Worksheets(sh1 & sh2).Activate
    
        
    Set wBook = Nothing
    
    On Error GoTo 0
    
    End If
    End Sub
    Can someone help me fix it, please?

    Thank you,
    Gos-C
    Using Excel 2010 & Windows 10
    "It is better to be prepared for an opportunity and not have one than to have an opportunity and not be prepared."

  2. #2
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    Re: Activate Worksheet Using Concatenated Strings for Name

    edit: Read your post wrong on what you wanted.

  3. #3
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408

    Re: Activate Worksheet Using Concatenated Strings for Name

    Hi 111StepsAhead,

    Thank you very much for your quick response.

    I want to use variables for the sheets names so that I won't have to change them in the macro each year.

  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: Activate Worksheet Using Concatenated Strings for Name

    I would think you could set the wks1 variable name like so:

    Set wks1 = Sheets("ABC" & Year(Date))
    You can get last year with Year(Date)-1.
    _________________
    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
    09-19-2004
    Location
    Canada
    Posts
    408

    Re: Activate Worksheet Using Concatenated Strings for Name

    I tried that but it didn't work.

  6. #6
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408

    Re: Activate Worksheet Using Concatenated Strings for Name

    Hi all,

    I got it to work!

    I used:

    wks1 = "ABC" & Year(Date)
    Thank you,
    Gos-C

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

    Re: Activate Worksheet Using Concatenated Strings for Name

    If that takes care of your need, please click Thread Tools above your first post and mark this thread as SOLVED.

  8. #8
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408

    Re: Activate Worksheet Using Concatenated Strings for Name

    Last edited by Gos-C; 04-18-2012 at 11:39 AM. Reason: Moved follow-up request to a new thread as the title was no longer correct.

+ 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