Results 1 to 9 of 9

How to stop error when I cancel the input file source command?

Threaded View

  1. #1
    Registered User
    Join Date
    11-07-2012
    Location
    Midwest, US
    MS-Off Ver
    Excel 2010
    Posts
    26

    How to stop error when I cancel the input file source command?

    Hello,

    I am using the following code to transfer battery testing data from .csv files into a .xlsm file.

    It gets what I need done but, if I select "Cancel" from the input file box it creates a Run-time error '1004': 'False.xlsx' could not be found.........

    What do I need to add to my code to stop this?

    Sub CommandButton1_Click()
    
    Dim batteryBook As Workbook
    Dim filter As String
    Dim caption As String
    Dim batteryFilename As String
    Dim batteryWorkbook As Workbook
    Dim targetWorkbook As Workbook
    
    Set targetWorkbook = Application.ActiveWorkbook
    
    
    filter = "Text files (*.csv),*.csv"
    caption = "Please Select an input file "
    batteryFilename = Application.GetOpenFilename(filter, , caption)
    
    Set batteryWorkbook = Application.Workbooks.Open(batteryFilename)
    
    
    Dim targetSheet As Worksheet
    Set targetSheet = targetWorkbook.Worksheets(1)
    Dim sourceSheet As Worksheet
    Set sourceSheet = batteryWorkbook.Worksheets(1)
    
    targetSheet.Range("A1").Value = sourceSheet.Range("B1").Value   'Will be adding more ranges..
    
    
    batteryWorkbook.Close
    
    End Sub
    Last edited by XxCMoneyxX; 04-15-2013 at 03:23 PM.

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