Get data from excel worksheet and copy it into the active workbook - vba

I'm new to using VBA and have found the code below. It works fine but I need all the rows in the source file. How can I change the code so I'm not limited to using the row numbers as the will be differten every time.
Private Sub Import1_Click()
' Get customer workbook...
Dim customerBook As Workbook
Dim filter As String
Dim caption As String
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetWorkbook As Workbook
' make weak assumption that active workbook is the target
Set targetWorkbook = Application.ActiveWorkbook
' get the customer workbook
filter = "Text files (*.xlsx),*.xlsx"
caption = "Please Select an input file "
customerFilename = Application.GetOpenFilename(filter, , caption)
Set customerWorkbook = Application.Workbooks.Open(customerFilename)
' assume range is A1 - G10 in sheet1
' copy data from customer to target workbook
Dim targetSheet As Worksheet
Set targetSheet = targetWorkbook.Worksheets(1)
Dim sourceSheet As Worksheet
Set sourceSheet = customerWorkbook.Worksheets(1)
targetSheet.Range("A6", "G10").Value = sourceSheet.Range("A2", "G6").Value
' Close customer workbook
customerWorkbook.Close
End Sub

Assuming you want to copy the entire sheet onto a blank sheet in your current workbook and only keep values (your question doesn't specify that too well), you could replace this line:
targetSheet.Range("A6", "G10").Value = sourceSheet.Range("A2", "G6").Value
With this:
sourceSheet.UsedRange.Copy targetSheet.Range("A1")
sourceSheet.UsedRange.Value = sourceSheet.UsedRange.Value
Hope that proves to do the trick!
UPDATE
Baed upon your last comment, try replacing this line:
sourceSheet.UsedRange.Copy targetSheet.Range("A1")
With this line:
Intersect(sourceSheet.UsedRange, sourceSheet.UsedRange.Offset(1,0)).Copy targetSheet.Range("A1")
The final part Range("A1") can be updated to paste the results wherever you want them.
Hope this does the trick!!

Just the following line has to be changed like I've given below:
targetSheet.Range("A6", "G10").Value = sourceSheet.Range("A2", "G6").Value
First, using name ranges (top-left of excel where the name of the cell appears), name the start and end cell of the source sheet. Say, you call them "start" and "end".
Now, you can also name your start cell in targetsheet. End cell is not required because it will paste everything anyways. This is optional and you can stay without naming it like below.
The code will then look like...
targetSheet.Range("A6").Value = sourceSheet.Range("start", "end").Value
If you name the targetSheet start cell ("start_target" for example) as well then it will be like this :
targetSheet.Range("start_target").Value = sourceSheet.Range("start", "end").Value

Related

Get data from another excel file with not fixed worksheet name

I have a excel file that contain daily order id and I need to get some data from other excel use the order id as index. The source file contain many worksheet that means a listbox with sheet name for selection is required.
The workbook & worksheet used for data source is not fixed and will determine by user, so a listbox for user to select relevant worksheet is required
The workflow is when i call the vba at the daily excel file, a listbox with all sheet name of the source excel file will pop up for select worksheet, then the daily excel file will get data from the source excel base on the order id as index.
Now I have a vba using activeworkbook and activeworksheet to set the lookup range, but I don't think this is a good coding method. Could someone can give me some suggestion?
For the userform code if the strfile is set to an exact file the code is fine, but the source file may be change.
All source files are save in same location, the required source file name is in Range("Z1") of the daily excel file, is it possible the strfile can change base on Range("Z1")?
Please let me know if I can clarify anything for you.
Sub example()
Dim dest_wbk As Workbook
Dim dest_ws As Worksheet
Dim source_wbk As Workbook
Dim source_ws As Worksheet
Set dest_wbk = ThisWorkbook
Set dest_ws = dest_wbk.ActiveSheet
sourcefilename = Range("Z1")
UserForm1.Show
Set source_wbk = ActiveWorkbook
Set source_ws = source_wbk.ActiveSheet
sourcelastrow = source_ws.Cells(Rows.Count, 2).End(xlUp).Row
Set lookuprange = source_ws.Range("A2:E" & sourcelastrow)
dest_lastrow = dest_ws.Cells(Rows.Count, 4).End(xlUp).Row
For i = 2 To des_lastrow
ID = dest_ws.Range("D" & i)
dest_ws.Range("K" & i) = Application.VLookup(ID, lookuprange, 3, False)
dest_ws.Range("L" & i) = Application.VLookup(ID, lookuprange, 4, False)
Next i
source_wbk.Close
End Sub
'Below in the code in the userform
Private Sub ListBox1_Click()
Sheets(ListBox1.Value).Activate
Unload Me
End Sub
Private Sub UserForm_Initialize()
Dim sh As Worksheet
strfile = ("C:\Documents\" & sourcefilename)
Set wbk = Workbooks.Open(strfile, ReadOnly:=True)
For Each sh In wbk.Sheets
ListBox1.AddItem sh.Name
Next sh
End Sub
You need to change your two variables dest_wbk and dest_ws to something like
In case your destination Workbook is already open
'Change Workbook2.xls to whatever the file is (assuming it is open already)
Set dest_wbk = Workbooks("Workbook2.xls")
'Change SheetName to whatever the sheet name is inside dest_wbk
Set dest_ws = dest_wbk.Sheets("SheetName")
Otherwise, you need to open the workbook
'Change Workbook2.xls to whatever the file is
Set dest_wbk = Workbooks.Open("Workbook2.xls")
'Change SheetName to whatever the sheet name is inside dest_wbk
Set dest_ws = dest_wbk.Sheets("SheetName")
It is up to you, to get those values (Workbook name and Sheet name) from the UserForm, which I believe it shouldn't be a problem for you.

Copy cells from a Worksheet to another Worksheet (from different Workbooks and first empty cell)

I am trying to copy the content from a user opened worksheet to another worksheet from another Workbook. The code below do works, but it doesnt select the first empty cell from the WS_REL sheet, and overwrite all the data contained. So far, I have this:
Sub Importar_Dados()
Dim vTemp As Variant
Dim WB_TOA As Workbook, WB_REL As Workbook
Dim WS_TOA As Worksheet, WS_REL As Worksheet
Set WB_REL = ActiveWorkbook
Set WS_REL = WB_REL.Sheets("Planilha2")
vTemp = Application.GetOpenFilename("Excel-files,*.xlsx", _
1, "Selecione o relatório gerado pelo TOA", , False)
If TypeName(vTemp) = "Boolean" Then Exit Sub
Workbooks.Open vTemp
Set WB_TOA = Workbooks.Open(vTemp)
Set WS_TOA = WB_TOA.Sheets("Page 1")
WS_TOA.Cells.Copy WS_REL.Cells
End Sub
Thank you!
First, you dont need to open the other workbook twice. Remove the line
Workbooks.Open vTemp
Then, to append without overwriting, you need to find the last non-empty cell in your destination sheet. Try it like (for example):
WS_TOA.usedRange.Copy WS_REL.Range("A999999").End(xlUp).Offset(1)
If you dont have a column that is sure to have data in all rows, use this:
WS_TOA.usedRange.Copy WS_REL.Range("A" & WS_REL.Cells.Find("*", , , , xlByRows, xlPrevious).Row+1)
This finds you the first non-empty row.

Working with Named Ranges and Objects

I need to copy a range of cells from one workbook to another. However, unfortunately, because of the size of the two workbook I can't have them open at the same time.
So the idea was to do it in two steps:
1) Open workbook1 save the range from one workbook to a object range and close workbook1
2) Open workbook2 save the range from the object to the range in workbook
But this is not working. Could someone help with the code. Thanks. Sample code below
Dim Temp as Range
Workbooks.Open (Model1)
Workbooks(Model1).Activate
Temp = Range("First_Input").Value
Workbook(Model1).Close
Workbooks.Open(Model2)
Workbooks(Model2).Activiate
Range("Second_Input").Value = Temp.Value
A working example below:
Comments are embedded and works as is (without changes). Try this on a
workbooks first with random data.
The example works for a workbook already open, just alter it for "Opening" a workbook.
I have used the range address in the example. You can play with this depending on what you want to do.
The below works so should be easy to implement, copy and paste into excel.
Public Sub CopyData()
Dim wkb1 As Workbook
Dim wkb2 As Workbook
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Dim wkb1rng As Range
Dim wkb2rng As Range
'Point to Workbook and Sheet 1
'Set wkb1 = Workbooks.Open(Model1) ' To Open Workbook
Set wkb1 = Workbooks("Book1") ' If workbook is open already
'Sheets is the Index use String for the sheet name
Set sht1 = wkb1.Sheets(1)
' Dont need this if you moving objects directly in and out of memory.
' Workbooks(Model1).Activate
' Point to Range
Set wkb1rng = sht1.Range("First_Input")
' What is the address of the Range
Dim address As String
address = wkb1rng.Cells.address
'Point to Workbook and Sheet 2
Set wkb2 = Workbooks("Book2")
'Sheets is the Index use String for the sheet name
Set sht2 = wkb2.Sheets(1)
'I imagine Second_Input should be output?
'Use this only if the data range is exactly the same size
'Set wkb2rng = Range("Second_Output")
'else use this ...
Set wkb2rng = sht2.Range(address)
'Copy data across ...
wkb2rng.Value = wkb1rng.Value
End Sub

Copy the value from a cell in a worksheet into a range of cells

I'm intending to conduct a macro which will open up a workbook from the specified path, and loop through its worksheets which has the names "Januari, Februari, Mars" specifically, to deduct the value from C34. C34 has a value recorded there every time, so it shouldn't change. However I want to copy it to the current worksheet, where the first target should be at AA73, the second at AA74 etc. My code is
Sub Test()
Dim myHeadings
Dim i As Long
Dim path As String
path = "C:\pathtofile\file.xlsx"
Dim currentWb As Workbook
Set currentWb = ActiveWorkbook
Dim openWb As Workbook
Set openWb = Workbooks.Open(path)
Dim openWs As Worksheet
myHeadings = Array("Januari", "Februari", "Mars")
For i = 0 To UBound(myHeadings)
Set openWs = openWb.Sheets("&i")
currentWb.Sheets("Indata").Range("AA73+Application.Match(i,Array,False)").Value = openWs.Range("C34").Value
Next i
End Sub
However the compiler says that the subscript is out of range at the row with
Set openWs = openWb.Sheets("&i")
Here I've tried to do "i", i, &i among other things, but it haven't changed. Also I've tried to use "ThisWorkbook" instead of "ActiveWorkbook" but it didn't help either. Does anybody have an input as to how to achieve this in a more proper way?
EDIT: Adapting to the response from Dave, it works to import the sheets. However I get an error in:
currentWb.Sheets("Indata").Range("AA73+Application.Match(i,Array,False)").Value = openWs.Range("C34").Value
Where I get Automation Error -2147221080 (800401a8) at said code snippet.
You have already put your sheet names into an array, so you can just call the sheet name from the array as:
Set openWs = openWb.Sheets(myHeadings(i))

Assigning String a value from another workbook

I am trying to assign a value from a cell in another workbook to a string variable in my code.
So currently my code will look up column P,G,H,I in the workbook but I have created another workbook which I have input all the columns I need it to look up. So on the other workbook I have mapped it out so the Ship to site ID value is P but I cant workout how to assign the value from this workbook to the below code.
I want it so I can just change the value in my mapping workbook rather than having to come back to my coding and change the column letter everytime there is a change.
ShipToSiteID = Application.WorksheetFunction.Trim(Range("P" & counter))
AltShipTo1 = Application.WorksheetFunction.Trim(Range("G" & counter))
AltShipTo2 = Application.WorksheetFunction.Trim(Range("H" & counter))
AltShipToCity = Application.WorksheetFunction.Trim(Range("I" & counter))
When you use Range("P" & counter) this will be refering to the ActiveWorksheet
so it is the same as ActiveWorksheet.Range("P" & counter)
If you want to refer to another workbook then you need use a range from a sheet within this workbook.
Sub test()
Dim newWorkbook As Workbook
Dim newWorksheet As Worksheet
'NewWorkbook.xlsm is already open
Set newWorkbook = Workbooks("NewWorkbook.xlsm")
Set newWorksheet = newWorkBook.Worksheets("Sheet1")
MsgBox "This value is A1 in NewWorkbook - " & newWorksheet.Range("A1").Value
End Sub
You can also get an open workbook by index (although this isn't really recommended).
Set newWorkbook = Workbooks(1)
You can get and open a workbook at the same time by using Workbook.Open
Set newWorkbook = Workbooks.Open("C:/Path/To/Workbook")
Or get and add a new workbook by using Workbook.Add
Set newWorkbook = Workbooks.Add()
Also, regarding your usage of Application.WorksheetFunction.Trim
There is a built in VBA Trim() function that would suit better..
I'm not entirely sure from your question how you are trying to copy from one workbook to another, but the below code is a general approach for getting a specific cell's data out of another workbook.
Sub GetOtherValues()
Dim thisWS As Worksheet
Dim thatWS As Worksheet
Dim thatWB As Workbook
Dim ShipToSiteID As String
Dim counter As Integer
Set thisWS = ActiveSheet
Set thatWB = Workbooks.Open("C:\demo.xlsx")
Set thatWS = thatWB.Worksheets(1)
counter = 2 'initialized here for the sake of code completeness
ShipToSiteID = Trim(thatWS.Range("P" & counter))
'Do something else with the trimmed data from ShipToSiteID
thisWS.Cells(2, 2) = ShipToSiteID
thatWB.Close
End Sub
This code assumes you are running from the current workbook, and there is another worksheet/workbook that you need to get the data from. After declaring the variables, it assigns thisWB to be the current worksheet, then opens the next workbook and assigns the first worksheet to thatWS. We can now use thatWS and the counter variable to copy and trim the data, and stick it in the variable ShipToSiteID (and then do whatever else you're needing to do with it).