VBA problem

Hacklin337

New Member
Joined
Jul 9, 2021
Messages
3
Office Version
  1. 2010
Hello,
I need help with this code:

VBA Code:
Sub OTWIERANIEXLS3()

Dim customerBook As Workbook
Dim filter As String
Dim caption As String
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetWorkbook As Workbook

Set targetWorkbook = Application.ActiveWorkbook

filter = "Text files (*.xls),*.xls"
caption = "Please select your route"
customerFilename = Application.GetOpenFilename(filter, , caption)

Set customerWorkbook = Application.Workbooks.Open(customerFilename)

Dim targetSheet As Worksheet
Set targetSheet = targetWorkbook.Worksheets(1)
Dim sourceSheet As Worksheet
Set sourceSheet = customerWorkbook.Worksheets(1)

targetSheet.Range("CA1", "CJ201").Value = sourceSheet.Range("A1", "J201").Value

customerWorkbook.Close

End Sub

I need this thing to work the same exact way but, when in the selection window i press nothing or cancel, nothing is copied (there is no selection error)
Thank you for any help
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi,
try this update to your code & see if does what you want

VBA Code:
Sub OTWIERANIEXLS3()
    
    Dim filter              As String, caption As String
    Dim customerFilename    As Variant
    Dim customerWorkbook    As Workbook, targetWorkbook As Workbook
    Dim customerBook        As Workbook
    Dim targetSheet         As Worksheet, sourceSheet As Worksheet
    
    Set targetWorkbook = Application.ActiveWorkbook
    
    filter = "Text files (*.xls),*.xls"
    caption = "Please Select your route"
    customerFilename = Application.GetOpenFilename(filter, , caption)
    'cancel pressed
    If VarType(customerFilename) = vbBoolean Then Exit Sub
    
    Set customerWorkbook = Application.Workbooks.Open(customerFilename)
    
    Set targetSheet = targetWorkbook.Worksheets(1)
    
    Set sourceSheet = customerWorkbook.Worksheets(1)
    
    targetSheet.Range("CA1", "CJ201").Value = sourceSheet.Range("A1", "J201").Value
    
    customerWorkbook.Close
    
End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,214,968
Messages
6,122,509
Members
449,089
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top