Results 1 to 4 of 4

Command button macro

Threaded View

majorpun Command button macro 07-11-2011, 04:11 PM
davegugg Re: Command button macro 07-11-2011, 05:26 PM
majorpun Re: Command button macro 07-11-2011, 05:43 PM
royUK Re: Command button macro 07-12-2011, 02:24 AM
  1. #1
    Registered User
    Join Date
    07-11-2011
    Location
    Oxford, Mississippi
    MS-Off Ver
    Excel 2003
    Posts
    2

    Command button macro

    I'm having trouble making this macro I wrote run with a command button.
    The macro is a demo macro I'm writing for a much larger project, and this is basically what it does:
    It allows the user to browse for a pre-formatted file. It then reads the file, stores a few values and plugs it into the designated cells in the workbook the macro is running out of.

    The macro itself works when I manually run the macro. However, when I run the macro through the command button, it will execute the first half but not the last half. More specifically, it searches for the file, opens it and makes it the active window, but does not plug in the values in the designated cells.
    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
        
            'reads & stores cell value
            Set r1Cell = Cells(1, 1)
            Set r2Cell = Cells(1, 2)
            Set r3Cell = Cells(1, 3)
           
            Set inputVariable = Cells(2, 2)
                    
            'inserts values into designated cell
            ThisWorkbook.Worksheets(2).Cells(1, 1).Value = r1Cell.Value
            ThisWorkbook.Worksheets(2).Cells(1, 2).Value = r2Cell.Value
            ThisWorkbook.Worksheets(2).Cells(1, 3).Value = r3Cell.Value
            
            ThisWorkbook.Worksheets(2).Cells(6, 2).Value = inputVariable.Value
        End If
    End Sub
    Am I not using the correct format for the command button? or is it something else?

    Thanks,
    Last edited by Paul; 07-11-2011 at 04:12 PM. Reason: Added code tags for new user. Please do so yourself in the future.

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