Copy a specific range from a source worksheet to a target worksheet with different path - vba

Dim path_feb As String
Dim path_mar As String
Dim wkbk_feb As Workbook
Dim wkbk_mar As Workbook
path_feb = "D:\Tranzit\2016\feb\data_feb.xlsx"
Set wkbk_feb = Workbooks.Open(path_feb)
path_mar = "D:\Tranzit\2016\mar\data_mar.xlsx"
Set wkbk_mar = Workbooks.Open(path_mar)
Worksheets("monthly").Range("A2:A1000").Value = Windows("wkbk_feb").Worksheet("impuls").Range("A2:A1000").Value
Worksheets("monthly").Range("B2:B1000").Value = Windows("wkbk_mar").Worksheet("impuls").Range("A2:A1000").Value
End Sub
I need a little help to work this code.
The issue begin here:
Worksheets("monthly").Range("A2:A1000").Value = Windows("wkbk_feb").Worksheet("impuls").Range("A2:A1000").Value
So, I have 3 files with different path:
D:\Tranzit\2016\feb\data_feb.xlsx
D:\Tranzit\2016\\mar\data_mar.xlsx
D:\Tranzit\2016\data_final.xlsm
I want to copy from file 1 the range A2:A1000 from "Sheet" Impuls to file 3 in range A2:A1000 from "Sheet" monthly.
and
copy from file 2 the range A2:A1000 from "Sheet" Impuls to file 3 in range B2:B1000 from "Sheet" monthly.

You declared wkbk_feb and wkbk_mar as workbook objects so you need to reference them directly:
wkbk_feb.Worksheets("impuls")....
instead of activating or selecting anything you should always specify the workbook or worksheet. So it should look something like
wkbk_total.Worksheets("monthly")... = wkbk_feb.Worksheets("impuls")....

Related

Vlookup worksheet function in VBA (runtime error 9)

I'm new to VBA and I have the following code for a function :
Function checktype(ByVal modele As String)
Dim srchRange As Range
Dim book1 As Workbook
'Set some Workbook variables:
Set book1 = Workbooks("C:\Users\MZ\Desktop\EPI.xlsx")
Set srchRange = book1.Sheets(1).Range("A1:D800")
'This assumes that the Book2 is Open and you are on the desired active worksheet:
checktype = Application.WorksheetFunction.VLookup(modele, srchRange, 4, False)
End Function
The aim of this code is to get a certain number from another workbook using Vlookup function and return the value to the function checktype.
The problem is that I get a runtime error 9. When I click Debug the following line gets highlighted :
Set book1 = Workbooks("C:\Users\MZ\Desktop\EPI.xlsx")
Either make sure that the workbook is opened in the same instance of excel and refer it like this:
Set book1 = Workbooks("C:\Users\MZ\Desktop\EPI.xlsx")
Set book1 = Workbooks("EPI.xlsx") 'specifying only the name is OK as well
or
Set book1 = Workbooks.Open("C:\Users\MZ\Desktop\EPI.xlsx")
An example of 2 Excel files in the same instance:

Excel vba variable not defined when using range variable

I have function testFunc that takes Range as an argument, loops thorugh its cells and edits them. I have sub testSub that tests two cases: first is when I pass the current workbook's range to testFunc through: 1. variable Range 2. just as an argument testFunct(...Range("A1:A16")).
The second case is when I open another workbook and do the same - pass one of its worksheets range to the testFunc directly or via variable.
Function testFunc(aCol As Range)
Dim i As Integer
i = 0
For Each cell In aCol
MsgBox (cell.Value)
cell.Value = i
i = i + 1
Next
testFunc = i
End Function
Sub testSub()
Dim origWorkbook As Workbook
Set origWorkbook = ActiveWorkbook
Dim aRange As Range
Set aRange = ThisWorkbook.Sheets("sheet 1").Range("A1:A16")
Dim dataWorkbook As Workbook
Set dataWorkbook = Application.Workbooks.Open("Y:\vba\test_reserves\test_data\0503317-3_FO_001-2582480.XLS")
Dim incomesNamesRange As Range
Set incomesNamesRange = dataWorkbook.Worksheets("sheet 1").Range("A1:A20")
' origWorkbook.Worksheets("sheet 1").Cells(1, 5) = testFunc(dataWorkbook.Sheets("1").Range("A1:A20"))
origWorkbook.Worksheets("Ëèñò2").Cells(1, 50) = testFunc(incomesNamesRange)
' testFunc aRange
'origWorkbook.Worksheets("sheet 1").Cells(1, 5) = testFunc(aRange) '<---good
' origWorkbook.Worksheets("sheet 1").Cells(1, 5) = testFunc(ThisWorkbook.Sheets("sheet 1").Range("A1:A16"))
End Sub
The problem is, as I indicated with comments, when I open a foreign workbook and pass its range through a variable it gives an error variable not definedFor Each cell In aCol`, while all other cases (including passing range variable of the current workbook) work fine.
I need testFunc to stay a Function, because it's a simplfied example of some code from bigger program which needs to take a returned value from a function. And I need to store that Range in a variable too, to minimize time of the execution.
EDIT: As pointed out in the comments, I replaced Cells(1,5) with origWorkbook.Worksheet("shee t1").Cells(1,5) and fixed variable name, but the original mistake changed to variable not defined. I edited the title and body of the question.
The default name for the worksheet shouldn't have a space in it.
Set incomesNamesRange = dataWorkbook.Worksheets("sheet 1").Range("A1:A20")
Change this to:
Set incomesNamesRange = dataWorkbook.Worksheets("Sheet1").Range("A1:A20")
And see if that fixes it.

How to replace a sheet using VBA ?

I'd like to copy a sheet from a workbook and paste it in the 2nd sheet in my active workbook. I'm new with vba, it doesn't seem difficult but my code doesn't work. The sheet, which is copied, is opened in a new workbook and not in my active workbook.
Thanks for your help !
My code :
Sub copyPaste()
Dim classeur1 As Excel.Workbook
Dim classeur2 As Excel.Workbook
Set classeur1 = Workbooks.Open("Macintosh HD:Users:LouiseDhainaut:Documents:Stage:test_modifiable.xlsx")
Set classeur2 = ThisWorkbook
classeur1.Sheets(1).Copy
classeur1.Sheets(1).Paste Destination:=ThisWorkbook.Sheets(2).Range("A1")
classeur2.Save
classeur1.Close
End Sub
Try:
classeur1.Sheets(1).Copy Before:=ThisWorkbook.Sheets(2)
Instead of the
classeur1.Sheets(1).Copy
classeur1.Sheets(1).Paste Destination:=ThisWorkbook.Sheets(2).Range("A1")
Although note that copying an entire worksheet will copy the entire worksheet, not just the contents of it.
If you are looking to copy only the contents, you will need a different code, for example:
classeur1.Sheets(1).UsedRange.Copy ThisWorkbook.Sheets(2).Range("A1")

Export multiple ranges to a single PDF using VBA

I'm trying to write a macro to cycle through a dropdown menu. Everytime the value in the drop down changes it will change the values on the worksheet. I won't to capture a range of the worksheet for every value in the dropdown in a VBA array and then export all of these ranges to single PDF. I'm able to export them one at a time to multiple PDFs but this isn't the objective. The problem I seem to be having is storing the different ranges in an Array.
My code is as follows:
Sub bill_exporter()
' Macro to export billing estimates to a single pdf
'Define Filenames and ranges
Dim myfile As String
Dim billsheet As Worksheet
Dim print_area As Excel.Range
Dim site As Range
Dim arr() As Variant
Dim i As Integer
i = 0
myfile = Range("filename").Value
Set billsheet = ActiveWorkbook.Sheets("Mock Bill")
For Each site In Range("meters")
billsheet.Calculate
billsheet.Range("$R$10").Value = site
'Create Vertical Page Breaks
billsheet.VPageBreaks.Add Before:=Range("C3")
billsheet.VPageBreaks.Add Before:=Range("R3")
'Set Print Area
Set print_area = billsheet.Range("C3:R50")
Set arr(i) = print_area.Value
i = i + 1
Next site
Array(arr).ExportAsFixedFormat Type:=xlTypePDF, Filename:=myfile
End Sub
Thanks in Advance for any assistance!
So you can't collect them all into a single array, solution is to create multiple dummy sheets and delete them when done:

Add selection to named range using VBA?

I have a named range that is called tempPrintArea and refers to
='Label packinglist'!$A$1:$J$59
I want to use VBA to add another selection to it, so that it refers to
='Label packinglist'!$A$1:$J$59,'Label packinglist'!$A$61:$J$110
How can I do this?
I'm envisioning something like
Range("tempPrintArea").RefersTo = wks.Range("tempPrintArea").Address & wks.Range("$A$61:$J$110")
... but that doesn't work
Suppose that you've created named range like this:
Dim wks As Worksheet
Set wks = ThisWorkbook.Worksheets("Label packinglist")
wks.Names.Add Name:="tempPrintArea", RefersTo:=wks.Range("A1:J59")
next step is to add new reference to it:
wks.Names("tempPrintArea").RefersTo = Union(wks.Range("tempPrintArea"), wks.Range("A61:J110"))
and now
Dim test As String
test = wks.Range("tempPrintArea").Address ' returns $A$1:$J$59,$A$61:$J$110