+ Reply to Thread
Results 1 to 4 of 4

Command button macro

Hybrid View

  1. #1
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Command button macro

    Your macro can be shortened to:

    Sub CommandButton1_Click()
    
        Dim varBook As Variant
        Dim otherWb As Workbook
        
        varBook = Application.GetOpenFilename
        If varBook <> False Then
            Workbooks.OpenText Filename:=varBook
            Set otherWb = ActiveWorkbook
                    
            'inserts values into designated cell
            ThisWorkbook.Worksheets(2).Cells(1, 1).Value = otherWb.ActiveSheet.Cells(1, 1).Value
            ThisWorkbook.Worksheets(2).Cells(1, 2).Value = otherWb.ActiveSheet.Cells(1, 2).Value
            ThisWorkbook.Worksheets(2).Cells(1, 3).Value = otherWb.ActiveSheet.Cells(1, 3).Value
            
            ThisWorkbook.Worksheets(2).Cells(6, 2).Value = otherWb.ActiveSheet.Cells(2, 2).Value
        End If
    End Sub
    No need to store the locations of the values in the other workbook that you are looking for. A couple things I'd ask you:

    Are there multiple worksheets in the workbook that is selected? Your code does not deal with sheets, so the active sheet will be where data is taken from.

    I hate to ask, but are you sure you are looking for the data in cells A1, B1, C1, and B2?
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  2. #2
    Registered User
    Join Date
    07-11-2011
    Location
    Oxford, Mississippi
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Command button macro

    ahh yes, that shortens it a lot...
    and yes, I was also experimenting with multiple worksheets to prep for the actual project.

    Also I've managed to fix it by simply doing:

    call MacroName

    and now it works... don't know why I didn't think of that before...

    Thanks for the speedy reply!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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