I have an Excel workbook with two worksheets, Report and Data. I want to write a section of the Report tab out to a new workbook file (print range named "Roster") and retain the formatting, print settings, etc.
Below is the macro I have so far - it works but writes the whole Report tab to the file, not just the roster section, and it loses the print range which would be useful for the recipient of the resulting file.
Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+t
'
Dim Output As Workbook
Dim FileName As String
'This part updates the roster - grabs the next
'roster value and move it to A1, thus updating the report
Range("A1").Select
Selection.End(xlDown).Select
Selection.Copy
Range("A1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.End(xlDown).Select
Selection.ClearContents
Range("A1").Select
'Now we write the Report worksheet to a new file using
'the custom filename in cell AA1
Set Output = Workbooks.Add
Application.DisplayAlerts = False
ThisWorkbook.Worksheets("Report").Cells.Copy
Selection.PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=True, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats
FileName = Range("AA1").Value
Output.SaveAs FileName
Output.Close
End Sub
Your code is copying the whole Report tab because of the line
ThisWorkbook.Worksheets("Report").Cells.Copy
The .Cells gets all the cells of a worksheet. To get a portion you could try something like
ThisWorkbook.Worksheets("Report").Range(FirstCell, LastCell).Copy
where FirstCell and LastCell are the start and end of the range you want to copy. To duplicate the print area you could try something like
DestinationSheet.PageSetup.PrintArea = OriginalSheet.PageSetup.PrintArea
Hope that helps
Related
I have an Export to sheet button but I can't get it working correctly.
I selects the correct cells to copy but can't then transpose them on to selected sheet that appears in the drop down box in cell A1, I then also need it to paste on the next available row in that specific sheet. The problem is that I can't just list the sheets in VBA as the list in the drop down box changes. I have tried several ways to with no success. If someone could help it would be great
Sub Button2_Click()
Worksheets("Sheet1").Range("a2:x2").Copy
ActiveSheet.Paste Destination:=Worksheets("Sheet1!A1").Range("a:x")
End Sub
Here is some more code I have tried for the issue but still does not seem to work.
Sub ExportButton1()
'
' ExportButton1 Macro
' Exports Data to staff sheet from drop down box
'
' Keyboard Shortcut: Ctrl+e
'
ActiveWorkbook.Save
End Sub
Private Sub CommandButton1_Click(ByVal Target As Range)
Application.ScreenUpdating = False
Dim copySheet As Worksheet
Dim pasteSheet As Worksheet
Set copySheet = Worksheets("Data")
'On Error Resume Next
'If Not (Application.Intersect(Range("H2"), Target) Is Nothing) Then _
Set pasteSheet = Worksheets(ActiveSheet.Range("H2"))
copySheet.Range("G5:AA5").Copy
pasteSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
you need to change the sheet name in your worksheet function.
This might work.
Sub Button2_Click()
'Copying the Data
Worksheets("Sheet1").Range("a2:x2").Copy
'Pasting the data
' What we were missing was to pass the name of the tab dynamically. Now this code will pick up the name that appears in the Cell A1.
ActiveSheet.Paste Destination:=Worksheets(Worksheets("Sheet1").Range("A1").value).Range("A1")
End Sub
Also in the paste range you only need to put the first cell range to paste values.
To paste the Transposed Values check the function PasteSpecial with Transpose property set to True.
'I have found another way around the problem to copy paste cells to certain sheet then on 'staff sheet a formula in a table to tests column A value on data sheet for name and only 'transpose those rows
Sub Macro1()
Range("A2:J2").Select
Selection.Copy
Sheets("Sheet3").Select
Range("A60000").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet1").Select
End Sub
'Formula on staff sheet is -=IF(Sheet3!A:A="Column1",Sheet3!$B:$B,"")
In Excel 2007 there was no issue but when I hit the below line in Excel 2016, it now takes over a minute each time. There are only 300 rows in the column. All I want to do is cut a column and paste it next to another column.
Selection.Insert Shift:=xlToRight
Sample code is as follows, but I have 30 odd of these so it is taking half an hour.
Columns("E:E").Select
Selection.Cut
Columns("C:C").Select
Selection.Insert Shift:=xlToRight
Any ideas why?
Do you have formulas and maybe even external references? Then please try this:
ActiveWorkbook.UpdateLinks = xlUpdateLinksNever
ActiveWorkbook.UpdateRemoteReferences = False
Application.Calculation = xlManual
ActiveSheet.Columns("E:E").Cut
ActiveSheet.Columns("C:C").Insert Shift:=xlToRight
ActiveWorkbook.UpdateLinks = xlUpdateLinksAlways
ActiveWorkbook.UpdateRemoteReferences = True
Application.Calculation = xlCalculationAutomatic
' If it's faster, then uncomment following line additionally
' Application.CalculateFull
I had to deal with an Excel xlsx file generated by a Payroll system. Not sure what is causing the slowness on column insert. Inserting a column into a 88,000 row file takes about 25 seconds.
I discovered that if I copy the entire worksheet to a new sheet as values and number formats, the insert column step will run almost instantly. The copy entire worksheet portion takes just 3 seconds! There are no formulas in the file nor conditional formatting.
This is the logic I used:
' Copy source worksheet
Dim rng As Range
Set rng = Worksheets("Sheet1").Cells
rng.Copy ' note that wks.Cells.Copy is very slow
' add new worksheet
Dim newWks As Worksheet
Set newWks = Sheets.Add(After:=ActiveSheet)
newWks.Name = "Values Only"
' paste values and number format into new worksheet
newWks.Range("A1").Select
' xlPasteValues is fast too
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
' if I also include a xlPasteFormats then the insert column will become very slow once again
' Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
' now the column insert is blazingly fast!
newWks.Columns(14).Insert
newWks.Cells(1, 14).Interior.ColorIndex = 35
' etc.
I have tried to simplify the whole coding actually as nothing seemed to work. To recap:I have a folder with 51 Excel files. In each file there are specific variables between cell J4 and cell R4, included, that I want to tranfer into an inclusive excel workbook keeping the original format. The code that I am using is the following:
*** I have defined my variables ***
Sub LoopThroughDirectory()
Dim MyFile As String
Dim pp As Workbook
Dim row As Integer
row = 1
***this is the folder that has my 51 excel files***
MyFile = Dir("C:\Users\Aaa\Desktop\Analysed Data\*.xls*")
***consider all the files until the end, sheet 1, range, copy, close***
Do While MyFile <> ""
Workbook.Open ("C:\Users\Aaa\Desktop\Analysed Data\")
Worksheets("sheet1").Select
Range("J4:R4").Select
Selection.Copy
ActiveWindow.Close
***go to my workbook, sheet 1, row 1. Select A1 and paste special values and format***
Set pp = Workbook
Windows("pp.xlsx").Activate
Worksheets("sheet1").Cells(row, 1) = MyFile
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
***do it in loop for all my excel files, and copy-pastespecial the range of values each time in the following row***
row = row + 1
Loop
End Sub
it doesn't work like that either but I thought that if I get rid of the erow probably the process would be more straightforward and that if I explain what I mean with my coding, it's easier to get help. Thank you for your help. Silvia
I have this macro which works ok on the first row, but once it has completed I want it to run again on the next row down and paste the result on the next row down on the "results" sheet and continue the process through the whole document until it reaches the last record - (there are approx. 5300 records in my spreadsheet)
Sub Macro2()
' Macro2 Macro
Range("A2:BW2").Select
Selection.Copy
Sheets("Lookup").Select
Range("F3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("F3:V3").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Result").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
So basically I am copying the first row of data on sheet named "amps_job_history", it them pastes this data into a sheet called "lookup", once the data is pasted there a formula does a calculation that marries the data up with data from another worksheet. I then want to copy the original data plus the extra 3 columns that have been connected to the data with the formulas and the paste it into the sheet called "result". I then want it to go back to the first sheet "amps_job_hisotry" move down to the next row of data and repeat the process and when it pastes the data into the "result" page it need to past on the next row down and so on and so on until it reaches the last record.
I think this loop is what you are looking for.
Sub Macro2()
' Macro2 Macro
Dim rw As Long
With Worksheets("amps_job_history")
For rw = 2 To .Cells(Rows.Count, "A").End(xlUp).Row
With Intersect(.Range("A:BW"), .Rows(rw))
Worksheets("Lookup").Range("F3").Resize(.Rows.Count, .Columns.Count) = .Value
End With
With Worksheets("Lookup")
With .Range("F3:V3")
Worksheets("Result").Range("A1").Offset(rw - 1, 0).Resize(.Rows.Count, .Columns.Count) = .Value
End With
End With
Next rw
End With
End Sub
The rows from the source data in the amps_job_history and the destination Result worksheet are shifted down one more row on each loop. The transitional F3:V3 range in the Lookup worksheet remains the same through out.
I've use direct value transfer rather than copy, paste special, values and the With ... End With statement provide explicit parent worksheet referencing without the use of the Range .Select or Range .Activate methods.
Update: I was able to solve this problem. I just remembered VBA use * and ? to present the string 'If Sheet.name Like "*CopyA"'
I would like to make a copy special value with original format of the sources of all sheets contains the word: "CopyA" for example "Apple CopyA","Mango CopyA"
I can use the macro record to see how VBA create sheet and copy special like similar example below.
I am having trouble in the searching for sheet name - All examples I found either I must know exact name (same as Macro record) or I have to make a copy of all sheets (loop though all sheet and copy, no matter the name is).
Sub Macro1()
Sheets("Sheet1").Select
Sheets("Sheet1").Copy Before:=Sheets(1)
Sheets("Sheet1(2)").Select
Cells.Select
Selection.Copy
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
How can I search for sheet's name? In this case, any sheet that has "CopyA"?
Another way :) You can also use Instr(). For example
For all sheets in the workbook, use this
Option Explicit
Sub Macro1()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
If InStr(1, ws.Name, "CopyA") Then
'~~ > Your code
End If
Next
End Sub
For ActiveSheet use this
Option Explicit
Sub Macro1()
If InStr(1, ActiveSheet.Name, "CopyA") Then
'~~ > Your code
End If
End Sub