I'm curious about what the format of a code in VBA for...
obtaining specific rows from a specific sheet from a specific workbook within a specific subset of folders.
for example: Populating Sheet1 in Wkbk1 from the data in Sheet1 from Wkbk2 which is in a shared drive somewhere....
Any ideas?
EDIT:
I realize this may be a vague post so I am going to try to explain in further detail.
I am looking for a code which allows me to do the following.
Enable a macro from WkBk 1 to pull data from WkBk 2.
WkBk 2 is located within a shared drive and is closed.
I want to pull specific data from WkBk 2 and populate that data into specific rows in WkBk 1.
This code below can be made more exact or efficient, but it will get you started:
Option Explicit
Sub PullFromFile()
Dim wkb as Workbook, wkbFrom as Workbook
Set wkb = ThisWorkbook '-> assuming the workbook you want to copy to has code in it
Set wkbFrom = Workbooks.Open("S:\Me\letsshare\thisfile.xlsx")
Dim wks as Worksheet
Set wks = wkbFrom.Sheets("mySpecificSheet")
Dim rng as Range
Set rng = wks.Rows("1:3") '-> set your specific rows, here
rng.Copy wkb.Sheets("whichSheet").Range("A1") '-> adjust to your settings
wkbFrom.Close False
End Sub
Related
So I currently have three workbooks, I have created a folder in my Desktop, and where I am using workbook x, through a range on a cell to open up workbook called m (old) and workbook called n (new). I am then updating the old with the new.
Problem
Once I have opened up m, I am having to do a save as of the file as a newname. I am keen to know is there way of referring to it without using the workbook newname, as this workbook name would be concatenated with today's date and the name can be quite long and time consuming to type. I have produced several subroutines, one to remove protection etc,. and one for carrying out the coping actions and I am calling the subroutines, I just want to now if there is way of linking the newsaved name to some of the subroutines from referencing. Alternatively the old file could be saved at the very end. I am keen to pursue this avenue as I will be playing in certain other cases with four workbooks.
I have taken this code and amended it, but I am unable to open both spreadsheets, the strange thing when i block out the code and go through a test both work for each path they work, but not at the same time.
this is the code i amended, which i was able to find in
Dim x As Workbook, y As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Set x = Workbooks.Open("path to copying book")
Set y = Workbooks.Open("path to pasting book")
Set ws1 = x.Sheets("Sheet you want to copy from")
Set ws2 = y.Sheets("Sheet you want to copy to")
With ws1
.Cells.Copy ws2.cells
y.Close True
x.Close False
End With
End Sub
this is my code where it is not opening both workbooks
Dim wb as Workbook
Dim wb1 As Workbook, wb2 As Workbook
Dim ws as Worksheet, ws1 As Worksheet, ws2 As Worksheet
Dim Lst As Long,
Dim r1, r2 As Range
Set wb = ThisWorkbook
Set wb1 = Workbooks.Open("Sheets("x").Range("A4").Value")
Set wb2 = Workbooks.Open("Sheets("x").Range("A5").Value")
With wb2
Call Wbkunprtect()
I would be grateful for some help, please for those who are trigger happy can you hold back from pressing the down arrow some of us are not excel knowledgeable and also trying to learn and do not want to be banned from asking questions, I am trying to move data between two workbooks wb1 and wb2, through another wb i have completed my code, and this is the bit which stopping me from going forward.
I have learned a great deal from this site the reason I asked certain questions as I am reluctant to use select or activate as I have been told this is a bad habit you have to keep away from.
From what I understood, you'll need to use a WorkBook object! the WorkBook object will have the instance of the newly added workbook. It is as simple as that:
Sub example_new_workbook()
Dim wb As Workbook
Set wb = Workbooks.Add
wb.SaveAs ("path where you want to save")
End Sub
That way, "wb" will have the instance of the new workbook, and you can use freely to reference that new workbook. Remember, the WorkBooks object holds all the instances of all open workbooks, so you can get the reference for any other workbook through this object.
As to make it "Global", you can declare it outside the "Sub" scope, so the variable will be permanent as long as the module (or wherever you put your code) is open like this:
public wb as Workbook
Sub <your_routines>()
...
End Sub
I'm working on a vba macro for an excel sheet and I'm not sure how to go about doing one of my functions. I have a private sub in the macro that is used to get the path of a .csv file (say C:/files/file.csv stored as variable 'csvfile').
What I need to do at this point is automatically pull information from that csv file according to a certain formula and save it as a variable:
=COUNTIFS(F2:F10000,"=medium",Z2:Z10000,"=Open")
So in summary, in a macro in spreadsheet Main.xlsx, I need to run the above formula on the file whose path is stored in variable csvfile, and save the returned number as a variable within the macro so I can make use of that number in my macro.
I'll need to do this nine times actually with the formula slightly different each time, but once I have the single variable worked out I think I'll be able to modify it to produce all the results I need.
Thanks
Here's an example of one way to do it:
Sub OpenAndCount()
Dim sFile As String
Dim wb As Workbook
Dim ws As Worksheet
Dim cnt As Long
Dim rng1 As Range
Dim rng2 As Range
sFile = "c:\files\file.csv"
Set wb = Workbooks.Open(sFile)
Set ws = wb.Sheets(1)
Set rng1 = ws.Range("F2:F100000")
Set rng2 = ws.Range("Z2:Z100000")
cnt = Application.WorksheetFunction.CountIfs(rng1, "=medium*", rng2, "=open")
Debug.Print cnt
wb.Close
End Sub
I have the following which is working in the same workbook how do I get this to use a summary sheet in a different workbook?
Sub SummurizeSheets()
Dim ws As Worksheet
Application.ScreenUpdating = False
Sheets("Summary").Activate
For Each ws In Worksheets
If ws.Name <> "Summary" Then
ws.Range("D2:D6, D8:D15").Copy
Worksheets("Summary").Cells(Rows.Count, 4).End(xlUp).PasteSpecial (xlPasteValues)
End If
Next ws
End Sub
Make sure you place the sub-routine in a Module, and not in
ThisWorkbook. You can insert a new module by right-clicking on workbook name (from the VBA editor) and going to Insert > Module.
Make sure the workbooks you want to use/reference are open.
Make sure the workbooks are all located in the same workbook collection. This is not a problem unless you manually create an additional instance of Excel.
Reference the workbooks using the Workbooks() object just like you
do with Worksheets.
Sub test()
Dim b2 As Workbook 'We will use this variable as a reference to the external workbook that contains the "Summary" worksheet.
Set b2 = Excel.Workbooks("testbook2") 'We assign the external workbook (which I named "testbook2" for the purposes of this example) to our 'b2' variable.
Dim ws1 As Worksheet
Dim ws2 As Worksheet 'We will use these variables as references to the worksheets we're using.
Set ws1 = Excel.ActiveSheet 'We set ws1 to equal our current sheet (which presumably is where you'll be copying data from).
Set ws2 = b2.Worksheets("Summary") 'We set ws2 to equal the sheet (named "Summary") in the external workbook (named "testbook2").
ws1.Range("D2:D6").Copy 'We copy the data from the active sheet, which we reference using our 'ws1' variable.
'Note: I had issues with using multiple ranges in the same object so I removed this from your code.
ws2.Cells(Rows.Count, 4).End(xlUp).PasteSpecial xlPasteValues 'You only need to use the ws2 variable since we've already defined it as the "Summary" sheet you want.
End Sub
I'm not sure why I follow that first rule, but I seem to remember having issues when using ThisWorkbook in conjunction with external workbook references.
Update
I edited the code to show you a better example of how to do this. You almost never need to use "Activate" or "Select" commands in VBA. Just assign variables and reference the values directly.
I am currently working on an integrated set of workbooks, where I need to transfer data values between workbooks. In order to do so I need a VBA Macro that can copy a specific range (a row in a worksheet) and insert it at the bottom of an overview list in a different book.
How can I do this?
I am somewhat of a VBA novice, so specific instructions are appreciated.
Note: I'm using MS Excel 2010.
Thank you in advance!
The following example copy will copy the contents of the cells A1:A3 from a Worksheet named Source in a Workbook named workbook1.xlsx into the Worksheet named Target in a different Workbook, workbook1.xlsx. It will throw an error if either of the two Workbooks are not open, or if they do not contain the Worksheets that the Sub calls for.
Sub Example()
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set wb1 = Workbooks("workbook1.xlsx")
Set wb2 = Workbooks("workbook2.xlsx")
Set ws1 = wb1.Sheets("Source")
Set ws2 = wb2.Sheets("Target")
ws2.Range("A1:A3") = ws1.Range("A1:A3").Value
End Sub
How do I activate my Other workbook from the Current workbook? I have a current workbook with dumb.xls and The other workbook name as Tire.xls.I have opened the Tire.xls from the dumb.xls using worksbooks.open filename:= "name of the file".Its getting open but The problem is Im unable to make it work.
If I say cells(2,24).value=24 puts these value in the cell of dumb.xls but I want it to be done one Tire.xls.
activesheet.cells(2,24).value=24 puts these on Tire.xls. But how do i activate the Workbook with the name ? I need to open 3 to 4 excel workbooks And perform the operation? How do I activate the specific workbook
I have found this code on google
activeworkbook.worksheet("sheetname").activate ' but not working
windows("sheetname").activate ' people on google suggested not to use
Its not getting activated. I dont know how to make it work. Can anyone tell me How do i activate a specific workbook and a specific sheet of the other workbook ?
Example: I have niko.xls and niko_2.xls opened as workbooks from the dumb.xls workbook so totally 3 workbooks and I have to activate the 2nd sheet of niko_2.xls workbook.How do I make it? Can anyone explain me the syntax with these example? Thank you in advance
You do not need to activate the sheet (you'll take a huge performance hit for doing so, actually). Since you are declaring an object for the sheet, when you call the method starting with "wb." you are selecting that object. For example, you can jump in between workbooks without activating anything like here:
Sub Test()
Dim wb1 As Excel.Workbook
Set wb1 = Workbooks.Open("C:\Documents and Settings\xxxx\Desktop\test1.xls")
Dim wb2 As Excel.Workbook
Set wb2 = Workbooks.Open("C:\Documents and Settings\xxxx\Desktop\test2.xls")
wb1.Sheets("Sheet1").Cells(1, 1).Value = 24
wb2.Sheets("Sheet1").Cells(1, 1).Value = 24
wb1.Sheets("Sheet1").Cells(2, 1).Value = 54
End Sub
You have to set a reference to the workbook you're opening. Then you can do anything you want with that workbook by using its reference.
Dim wkb As Workbook
Set wkb = Workbooks.Open("Tire.xls") ' open workbook and set reference!
wkb.Sheets("Sheet1").Activate
wkb.Sheets("Sheet1").Cells(2, 1).Value = 123
Could even set a reference to the sheet, which will make life easier later:
Dim wkb As Workbook
Dim sht As Worksheet
Set wkb = Workbooks.Open("Tire.xls")
Set sht = wkb.Sheets("Sheet2")
sht.Activate
sht.Cells(2, 1) = 123
Others have pointed out that .Activate may be superfluous in your case. You don't strictly need to activate a sheet before editing its cells. But, if that's what you want to do, it does no harm to activate -- except for a small hit to performance which should not be noticeable as long as you do it only once or a few times. However, if you activate many times e.g. in a loop, it will slow things down significantly, so activate should be avoided.
You can try this.
Workbooks("Tire.xls").Activate
ThisWorkbook.Sheets("Sheet1").Select
Cells(2,24).value=24
The code that worked for me is:
ThisWorkbook.Sheets("sheetName").Activate
try this
Windows("name.xls").Activate
Dim Wb As Excel.Workbook
Set Wb = Workbooks.Open(file_path)
Wb.Sheets("Sheet1").Cells(2,24).Value = 24
Wb.Close
To know the sheets name to refer in Wb.Sheets("sheetname") you can use the following :
Dim sht as Worksheet
For Each sht In tempWB.Sheets
Debug.Print sht.Name
Next sht