VBA Help

ionelz

Board Regular
Joined
Jan 14, 2018
Messages
248
Office Version
  1. 365
Platform
  1. Windows
Hi,
I have this simple VBA to Import one Excel file into another.
I would like to add a modification here :
FileSource.Worksheets("Sheet1").Range("b1 :b500").Copy

If M2="option 1" then
FileSource.Worksheets("Sheet1").Range("b1 :b500").Copy
If M2="option 2" then
FileSource.Worksheets("Sheet1").Range("a1 :a500").Copy


Private Sub CommandButton1_Click()
Dim FileFolder As FileDialog
Dim FileName As String
Dim FileSource As Workbook
Dim desWS As Worksheet

On Error GoTo myerror
Set desWS = ActiveSheet

Range("C4:B500").ClearContents

Set FileFolder = Application.FileDialog(msoFileDialogFilePicker)
FileFolder.Title = "Please Select a folder And file."

If FileFolder.Show = -1 Then

FileName = FileFoIder.SelectedItems(1)

Set FileSource = Workbooks.Open(FileName, 0, True)

FileSource.Worksheets("Sheet1").Range("bl :b500").Copy
desWS.Range("B4").PasteSpeciaI xlPasteVaIues

Else
'cancel pressed

End If

myerror:
Application.CutCopyMode = False
If Not FileSource Is Nothing Then FileSource.Close False
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
It would make it a lot easier to read if you could put your code in brackets. You can do that with the "</>" icon at the top of the post box.

Excel Formula:
Private Sub CommandButton1_Click()
Dim FileFolder As FileDialog
Dim FileName As String
Dim FileSource As Workbook
Dim desWS As Worksheet

On Error GoTo myerror
Set desWS = ActiveSheet

Range("C4:B500").ClearContents

Set FileFolder = Application.FileDialog(msoFileDialogFilePicker)
FileFolder.Title = "Please Select a folder And file."

If FileFolder.Show = -1 Then

FileName = FileFoIder.SelectedItems(1)

Set FileSource = Workbooks.Open(FileName, 0, True)

FileSource.Worksheets("Sheet1").Range("bl :b500").Copy
desWS.Range("B4").PasteSpeciaI xlPasteVaIues

Else
'cancel pressed

End If

myerror:
Application.CutCopyMode = False
If Not FileSource Is Nothing Then FileSource.Close False


End Sub


But, my question is, what is M2 referencing? Is it a string, is it a boolean, a checkbox, a combobox?
 
Upvote 0
M2 is Data Validation, with 2 options : Option 1 and Option 2
Data is STRING (TEXT)
 
Upvote 0
Well, you will need to somehow pass this along through the code. I would assume that it has been declared as public.

Though, I would suggest that it would be either an integer (1, 2, 3, etc) or a boolean because it is just two options. The reason is because a string has to be exactly the same. "Option1" is different than "option 1" or even "Option 1". However, 1 is always 1, and True is always True. See what I mean?

BUT, making it exactly as you requested is below. You will need to have M2 declared Public and as a String somewhere outside of a subroutine and then called to be changed to your option BEFORE the button is clicked.

VBA Code:
Private Sub CommandButton1_Click()
Dim FileFolder As FileDialog
Dim FileName As String
Dim FileSource As Workbook
Dim desWS As Worksheet

On Error GoTo myerror
Set desWS = ActiveSheet

Range("C4:B500").ClearContents

Set FileFolder = Application.FileDialog(msoFileDialogFilePicker)
FileFolder.Title = "Please Select a folder And file."

If FileFolder.Show = -1 Then

FileName = FileFoIder.SelectedItems(1)

Set FileSource = Workbooks.Open(FileName, 0, True)

If M2 = "option 1" then
 FileSource.Worksheets("Sheet1").Range("b1 :b500").Copy
 elseIf M2 = "option 2" then
 FileSource.Worksheets("Sheet1").Range("a1 :a500").Copy
end if

desWS.Range("B4").PasteSpeciaI xlPasteVaIues

Else
'cancel pressed

End If

myerror:
Application.CutCopyMode = False
If Not FileSource Is Nothing Then FileSource.Close False


End Sub
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,662
Members
449,462
Latest member
Chislobog

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