+ Reply to Thread
Results 1 to 3 of 3

VBA - Prompt User to Open a Workbook and to Select a Worksheet, But Opens an blank file

  1. #1
    Registered User
    Join Date
    01-24-2020
    Location
    Toronto
    MS-Off Ver
    Professional Plus 2016
    Posts
    4

    VBA - Prompt User to Open a Workbook and to Select a Worksheet, But Opens an blank file

    I would like the User to choose a Workbook and then select the worksheet that they need. The code works perfectly when it is Debug - Step Into. But, When the complete macro is run through the button, the file do get open and prompts to choose the sheet but no sheets or cells are visible. It's all BLANK. There is no protection to the file. Column names and Row numbers are not visible


    ans = MsgBox("Choose the file to retrive the data?", vbYesNo, "Choose Source")

    If ans = vbYes Then

    myfile = Application.GetOpenFilename(, , "Browse for Workbook")

    If myfile <> False Then

    ThisWorkbook.Sheets("Reference").Range("AA2") = myfile

    Set src_data = Workbooks.Open(myfile)

    SheetName = Application.InputBox("Choose a cell in Desired Sheet ", "Select Sheet",Type:=8).Worksheet.Name
    sht = SheetName

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA - Prompt User to Open a Workbook and to Select a Worksheet, But Opens an blank fil

    Please upload a workbook or a representative cut down copy, anonymised if necessary. It is always easier to advise if we can see your request in its context. Your code is in any case missing End If statements

    Show a before and after situation with manually calculated results, explaining which information is data and which is results, and if it's not blindingly obvious how you have arrived at your results some explanatory notes as well.

    To upload a file click the Go Advanced button at the foot of your post, look underneath the post area for the Manage Attachments section and take it from there.

    In addition would you change your location to something meaningful. This often helps if we need to consider regional/local settings.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    01-24-2020
    Location
    Toronto
    MS-Off Ver
    Professional Plus 2016
    Posts
    4

    Re: VBA - Prompt User to Open a Workbook and to Select a Worksheet, But Opens an blank fil

    The End If statements are in full code, I assumed this part of the code was giving me the issue so I uploaded just that part. My bad!

    I have attached 2 files, Destination and Source.

    When i run trhough the code in Step Into mode, it works right on point. But, when the complete macro is run - file do get open, prompts user to write the sheet name. The sheets, rows, columns everything is blank (Grey zone).
    Sub LoadData()
    Dim ws As Worksheet
    Dim desiredSheetName As String
    Dim c As String

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    ans = MsgBox("Choose the file to retrive the data?", vbYesNo, "Choose Source")
    If ans = vbYes Then
    myfile = Application.GetOpenFilename(, , "Browse for Workbook")
    If myfile <> False Then
    ThisWorkbook.Sheets("Destination").Range("AA2") = myfile
    Set src_data = Workbooks.Open(myfile)

    On Error Resume Next
    desiredSheetName = InputBox("Select any cell inside the target sheet: ",type:=8).worksheet.name ' Here i tried with .parent also
    sht = desiredSheetName
    On Error GoTo 0

    Set dest = ThisWorkbook.Worksheets("Destination")

    src_data.Activate

    'Get the Last Row Details of Source and Destination workbooks
    lastcell = src_data.Sheets(sht).Cells(Rows.Count, "C").End(xlUp).Row
    LastRowD = dest.Cells(dest.Rows.Count, "F").End(xlUp).Offset(0).Row

    src_data.Activate
    Sheets(sht).Select
    Range("A:B,D:D").Select
    Selection.Copy
    dest.Activate
    Range("F1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, skipblanks:=False, Transpose:=False

    src_data.Close False

    dest.Select

    End If
    Else
    Exit Sub
    End If

    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files
    Last edited by Praveen_k_g; 02-07-2020 at 02:11 PM.

+ 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. VBA to prompt user to select location to save file
    By Bevg in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-13-2019, 01:33 PM
  2. VBA to prompt the user to specify a workbook to open
    By Larry.LeBlanc@O in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-04-2017, 04:10 PM
  3. [SOLVED] Macro to prompt user to select a file and the selected file should saved in location
    By logesh excel in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-26-2015, 03:43 PM
  4. [SOLVED] Prompt user to select worksheet
    By darkangl640 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 02-18-2013, 01:12 PM
  5. [SOLVED] Import worksheet from user selected file into open workbook
    By AWM1966 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-14-2012, 04:30 PM
  6. Prompt user to select file to import
    By Lmsloman in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-19-2009, 07:23 PM

Tags for this Thread

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