I am using this line to copy some sheets between two workbooks
shtSummary.Copy after:=wbNew.Sheets(wbNew.Sheets.Count)
but it copys the equations not the values so it result to empty cells in wbNew Workbook
How can I copy the values only between them?
I don't think this is possible when copying sheets.
But you could try something like this:
Dim destWs As Worksheet
Dim AddressStr As String
AddressStr = shtSummary.UsedRange.Address
With wbNew
Set destWs = .Sheets.Add(After:=.Sheets(.Sheets.Count))
End With
destWs.Name = shtSummary.Name
destWs.Range(AddressStr).Value = shtSummary.Range(AddressStr).Value
Related
I am trying to sum values from my original worksheet in specific cells in my newly created worksheet, which has a template to fill out.
When I used macro recorder, it references the worksheet name, which would not be useful as the worksheet name changes depending on which worksheet I am working in when I run the code.
So I tried changing the worksheet name to a variable "XCXX".
The first argument works so I thought everything was okay, however, on the second argument, it keeps trying to open a file, when it should simply go back to XCXX and pull the values.
Is it a problem with my activesheet changing?
Sub AddWorkbooks()
Dim ChangeOrder As Range
Dim XCXX As Worksheet
Dim CoForm As Worksheet
Set XCXX = ActiveSheet
Set CoForm = Worksheets("+CO Form+")
'Set wbNew = Workbooks.Add
CoForm.Copy After:=Sheets(ActiveSheet.Index)
With CoForm
Range("A6:D6").Select
ActiveCell.FormulaR1C1 = XCXX.Range("D2").Value
Range("AD81").Select
ActiveCell.FormulaR1C1 = "='XCXX'!R[-64]C[-24]+'XCXX'!R[-64]C[-23]"
End With
End Sub
This should be close:
Sub AddWorkbooks()
Dim ChangeOrder As Range
Dim XCXX As Worksheet, wb As Workbook
Dim CoForm As Worksheet, CoFormCopy As Worksheet
Set wb = ActiveWorkbook
Set XCXX = ActiveSheet
Set CoForm = wb.Worksheets("+CO Form+")
CoForm.Copy After:=XCXX
Set CoFormCopy = XCXX.Next 'the copy of "+CO Form+"
With CoFormCopy 'assuming you want to work with the copy?
.Range("A6:D6").Value = XCXX.Range("D2").Value
.Range("AD81").FormulaR1C1 = _
Replace("='<nm>'!R[-64]C[-24]+'<nm>'!R[-64]C[-23]", "<nm>", XCXX.Name)
End With
End Sub
Note when using With you need to use a period to link (eg) Range() with the object used in the With statement, otherwise it defaults to the active sheet.
Also generally there's no need to select a range to do something with it.
I have created a workbook that I send to other employees so they record certain items. The user will save the workbook as "STATS (their badge number)" and send it back. I copy the cells from their books (Sheet1.range("B4:B29") and .Range("F1:G2) and paste into my master workbook (STATS Total) on tabs that are named with each employee's last name. I am trying to automate opening the numerous books they send me (all in the same folder), copy the data, then paste it into the same cells (B4:B29 and F1:G2 on their named sheets, then close the "source" workbooks. To help, I put the full pathname to each "source" book in Cell("I2" on their sheet in the master book. So in the masterbook, there is a sheet named "Smith" and in I2 is the pathname to the workbook he sent me. This is the code I have tried, but I cannot get it to work. What should I be doing different?
Sub CopyAndPaste()
Dim x As Workbook, y As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Set x = Workbook(Sheet6.Range("I2").Text) 'copied book
Set y = ThisWorkbook 'pasted to this book
Set ws1 = x.Sheet1 'copied from
Set ws2 = y.Sheets("copied to")
ws1.Cells.Copy ws2.Cells
y.Close True
x.Close False
End Sub
You should use:
Sheet6.Range("I2").value
Instead of :
Sheet6.Range("I2").text
More than that, you need to open the workbook if it's not already opened (and I guess it's not the case if you have to do it for multiple workbooks). So you could have:
Dim x As Workbook, y As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim input_workbook as String
Set y = ActiveWorkbook 'I consider this as the workbook you launch the macro from
input_workbook = Sheet6.Range("I2").value
'I guess Sheet6 is in workbook y ...
'in any case, it should represent a folder path with the workbook name,
'like "C:\Documents\MyExcelFile.xlsx"
Set x = workbooks.open(filename:=input_workbook) 'The input workbook that you need to open
'... rest of the code ...
Hope this helps!
#1 EDIT
To answer your question, you can now simply use x and y as the workbooks. So you might have:
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = y.sheets("your_y_sheet_name")
Set ws2 = x.sheets("your_x_sheet_name")
You can then use these variables to transfer data:
ws1.range("B1:C10").value = ws2.range("B1:C10").value
This will make your range B1:C10 in ws1 (wb y) equal to the one in ws2 (wb x). This is absolutely faster than copy/pasting. So, if you have a fixed range to transfer from x to y, just use this like that (just use the correct ranges; they should have the same size).
Once you are done with you x workbook, do not forget to close it:
x.close
I'm using this code to copy and paste values from one excel file to another and it works great:
Sub TransferValues()
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set wb1 = Workbooks("ABC.xls")
Set ws1 = wb1.Sheets("SHEET1")
Set wb2 = Workbooks("CBA")
Set ws2 = wb2.Sheets("SHEET2")
'This line puts the value from wb1.ws1 in wb2.ws2, cells specified:
ws2.Range("O3:O33").Value = ws1.Range("I82:I112").Value
ws2.Range("O35:O65").Value = ws1.Range("J82:J112").Value
End Sub
BUT, as of right now I have to manually tell VBA that it has to grab the "ABC" file.
Is there a way to let Excel read a cell that contains the Workbook name.
So for example workbook CBA says in A1 ABC
I was thinking something like: Set wb1 = Workbooks("(A1).xls")
Thank you!
I am creating an excel workbook with multiple different sheets. I want to be able to add a row to any sheet and have it reflected on the first sheet. I am having trouble with this since I want to add them to different sheets based on a condition. Is there any easy way to do this.
Here is a template for having multiple worksheets in VBA and setting values b/w them
Sub test ()
Dim ws as worksheet
Dim ws2 as worksheet
Set ws = Worksheets(1)
Set ws2 = Worksheets(2)
ws2.Range("A") = ws.Range("A")
End Sub
I have a excel workbook A.xlsx with columns A through T, now i need to copy specific columns H,K,L to a new workbook which would be created while i run a macro.
I was able to successfully copy a range of columns from one worksheet to another, but i am not finding a way to copy specific columns to a new workbook.
Private Sub copy_sub()
Sheets("Sheet1").Columns("H:K").Copy Sheets("Sheet2").Range("A1")
End Sub
Give this a try:
Sub dural()
Dim r1 As Range, r2 As Range
Sheets("Sheet1").Select
Set r1 = Range("K:L")
Set r2 = Range("H:H")
Set wbNew = Workbooks.Add
r2.Copy Range("H1")
r1.Copy Range("K1")
End Sub
Based on the fact that after the workbook is added, the new workbook will be active and Sheet1 will be the active sheet in that book. Also assumes that cols H,K,L are to be copied, but not col I.
Try below sample code
Private Sub copy_sub()
Dim wkb As Workbook
Set wkb = Workbooks.Add ' Will add new workbook
' with column name
ThisWorkbook.Sheets("Sheet1").Columns("H").Copy wkb.Sheets("Sheet2").Range("A1")
'with column index
ThisWorkbook.Sheets("Sheet1").Columns(9).Copy wkb.Sheets("Sheet2").Range("A1")
End Sub