+ Reply to Thread
Results 1 to 8 of 8

Selecting worksheet when name is a variable????

  1. #1
    Registered User
    Join Date
    06-23-2014
    Location
    england
    MS-Off Ver
    2013
    Posts
    78

    Selecting worksheet when name is a variable????

    Afternoon All,

    Quick question i'm hoping you may help me with as i'm probably being a Numpty.

    I'm running an import data macro that will continously pull the same data 'solved' and run some analysis on this data.

    I know how to write the script to analyse the data too.

    I JUST CAN'T ACTIVATE THE RIGHT SHEET?

    I'm very new to macros and use this site alot as a guide :D

    Below is the full code so far, problem area is

    Worksheets(1).Activate

    Range("O2").Select
    ActiveCell.FormulaR1C1 = "INSERT INFORMATION AND SO ON..."

    This above bit doesn't work? I also dont know what the sheet name is as it will continually change by the information put into the TextBox.

    Dream choose is to choose a sheet based on the text box name somehow whatever it is chosen to be.

    If not just selecting the first worksheet is fine as this is where the new tab is added.

    Cheers as always :D


    Private Sub CommandButton1_Click()

    'Creating a new tab and naming it

    Dim ws As Worksheet

    If TextBox1 = vbNullString Then Exit Sub

    On Error Resume Next

    Application.DisplayAlerts = False
    Sheets.Add().Name = TextBox1

    ' Get customer workbook...
    Dim customerBook As Workbook
    Dim filter As String
    Dim caption As String
    Dim customerFilename As String
    Dim customerWorkbook As Workbook
    Dim targetWorkbook As Workbook

    ' make weak assumption that active workbook is the target
    Set targetWorkbook = Application.ActiveWorkbook

    ' get the customer workbook
    filter = "Text files (*.xlsx),*.xlsx"
    caption = "Please Select an input file "
    customerFilename = Application.GetOpenFilename(filter, , caption)

    Set customerWorkbook = Application.Workbooks.Open(customerFilename)

    ' Set Range
    ' copy data from customer to target workbook
    Dim targetSheet As Worksheet
    Set targetSheet = targetWorkbook.Worksheets(1)
    Dim sourceSheet As Worksheet
    Set sourceSheet = customerWorkbook.Worksheets("Table1")

    targetSheet.Range("A1", "F500").Value = sourceSheet.Range("E1", "J500").Value

    ' Close customer workbook
    customerWorkbook.Close


    Worksheets(1).Activate

    Range("O2").Select
    ActiveCell.FormulaR1C1 = "INSERT INFORMATION AND SO ON..."


    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Selecting worksheet when name is a variable????

    worksheets(sheetName).activate
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Selecting worksheet when name is a variable????

    How is the code not working?

    PS Can you add code tags when posting code?
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    06-23-2014
    Location
    england
    MS-Off Ver
    2013
    Posts
    78

    Re: Selecting worksheet when name is a variable????

    The name is determined by the information put into the TextBox - earlier part of the code therefore I can't choose the sheet by inputting the name.

    When I try to start to run analysis on the data imported, see after worksheet(1).Acvitate end of code

    In cell O2 it should have the text "INSERT INFORMATION AND SO ON...", it doesn't and I dont understand why?

    once this is solved i can contiune to add text and formula into cells to build my table.

    Does this make sense?

    Cheers

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Selecting worksheet when name is a variable????

    Which worksheet should O2 be on?

    Is it the newly created worksheet, or is it another worksheet?

    Also, if the text isn't going into O2 on the correct worksheet which worksheet is it going in?

  6. #6
    Registered User
    Join Date
    06-23-2014
    Location
    england
    MS-Off Ver
    2013
    Posts
    78

    Re: Selecting worksheet when name is a variable????

    The text needs to be going on the newly created sheet

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Selecting worksheet when name is a variable????

    Try this.
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    06-23-2014
    Location
    england
    MS-Off Ver
    2013
    Posts
    78

    Re: Selecting worksheet when name is a variable????

    Works like a dream! Thankyou very much \o/

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Selecting an entire row using a variable
    By ATLGator in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-05-2012, 01:56 PM
  2. Selecting Shapes With Variable Based On Worksheet Data
    By svAndiamo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-15-2011, 03:49 PM
  3. VBA Selecting a variable array
    By Miraun in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-03-2009, 07:00 PM
  4. [SOLVED] Selecting a variable range
    By Bill in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-16-2006, 12:45 PM
  5. selecting a variable row range
    By Dan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-05-2005, 08:06 AM

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