Importing VBA code

danielalvz

New Member
Joined
Jan 29, 2022
Messages
9
Office Version
  1. 2011
Platform
  1. Windows
Hello I am trying to modify a code that i got for importing into a excel file, this is the code that i currently have
Sub Get_Data_From_File()


Dim FileToOpen As Variant
Dim OpenBook As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim destLastRow As Long, sourceLastRow As Long

Application.ScreenUpdating = False

Set ws1 = ThisWorkbook.Sheets(2)

destLastRow = ws1.Range("A" & Rows.Count).End(xlUp).Row
If destLastRow < 18 Then destLastRow = 18

FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files (*.xls*),*xls*")

If FileToOpen <> False Then

Set OpenBook = Application.Workbooks.Open(FileToOpen)
Set ws2 = OpenBook.Sheets(2)

sourceLastRow = ws2.Range("A" & Rows.Count).End(xlUp).Row
If sourceLastRow < 18 Then sourceLastRow = 18

ws2.Range("A18:D" & sourceLastRow).Copy
ws1.Range("A" & destLastRow + 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Application.CutCopyMode = False

OpenBook.Close Savechanges:=False

End If

Application.ScreenUpdating = True

End Sub

This code is working well for me but i realize that i need it to import a few different ranges not just one, i need to import from "A18:D" which is currently written and works fine but i need to modify it to import also from F18:J R18:S and V18:S
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
First off, the ranges you mentioned that you want to add need to be corrected.
Second off, you didn't mention where you want those ranges to go to.

That being said:

Can't you replace:
VBA Code:
ws2.Range("A18:D" & sourceLastRow).Copy
       ws1.Range("A" & destLastRow + 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
       Application.CutCopyMode = False

with something like:
VBA Code:
    ws2.Range("A18:D" & sourceLastRow).Copy
    ws1.Range("A" & destLastRow + 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    ws2.Range("F18:J" & sourceLastRow).Copy
    ws1.Range("F" & destLastRow + 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    ws2.Range("R18:S" & sourceLastRow).Copy
    ws1.Range("R" & destLastRow + 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    ws2.Range("V18:S" & sourceLastRow).Copy
    ws1.Range("V" & destLastRow + 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
'
    Application.CutCopyMode = False

Assuming you fix the ranges, that is.
 
Upvote 0
Solution
First off, the ranges you mentioned that you want to add need to be corrected.
Second off, you didn't mention where you want those ranges to go to.

That being said:

Can't you replace:
VBA Code:
ws2.Range("A18:D" & sourceLastRow).Copy
       ws1.Range("A" & destLastRow + 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
       Application.CutCopyMode = False

with something like:
VBA Code:
    ws2.Range("A18:D" & sourceLastRow).Copy
    ws1.Range("A" & destLastRow + 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    ws2.Range("F18:J" & sourceLastRow).Copy
    ws1.Range("F" & destLastRow + 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    ws2.Range("R18:S" & sourceLastRow).Copy
    ws1.Range("R" & destLastRow + 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    ws2.Range("V18:S" & sourceLastRow).Copy
    ws1.Range("V" & destLastRow + 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
'
    Application.CutCopyMode = False

Assuming you fix the ranges, that is.
you are a life saver man, it works like a charm many thanks
 
Upvote 0
First off, the ranges you mentioned that you want to add need to be corrected.
Second off, you didn't mention where you want those ranges to go to.

That being said:

Can't you replace:
VBA Code:
ws2.Range("A18:D" & sourceLastRow).Copy
       ws1.Range("A" & destLastRow + 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
       Application.CutCopyMode = False

with something like:
VBA Code:
    ws2.Range("A18:D" & sourceLastRow).Copy
    ws1.Range("A" & destLastRow + 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    ws2.Range("F18:J" & sourceLastRow).Copy
    ws1.Range("F" & destLastRow + 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    ws2.Range("R18:S" & sourceLastRow).Copy
    ws1.Range("R" & destLastRow + 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    ws2.Range("V18:S" & sourceLastRow).Copy
    ws1.Range("V" & destLastRow + 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
'
    Application.CutCopyMode = False

Assuming you fix the ranges, that is.
you are as life saver, this works like a charm many thanks
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,618
Members
449,092
Latest member
amyap

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