Copy everything in a worksheet vba - vba

' Copy
wb.Sheets(wsSource.Name).Range("A1:W79").Copy
' Paste Special
wbTarget.Sheets("Sheet1").Range("A1:W79").PasteSpecial xlValues
wbTarget.Sheets("Sheet1").Range("A1:W79").PasteSpecial xlFormats
This code allows me to copy everything in a range of A1:W79. How can I modify this Range so that it selects everything that contains value in a worksheet. For example, worksheet might contain values from C7:G20 etc.

I'd go like follows:
With wb.Sheets(wsSource.Name).UsedRange
.Copy
With wbTarget.Sheets("Sheet1").Range(.Address)
.PasteSpecial xlValues
.PasteSpecial xlFormats
End With
End With

Related

Copying data and conditional formatting from one workbook to another using VBA

I am trying to copy data and conditional formatting from one workbook to another. I can get the data to transfer over to my primary workbook, but the conditional formatting is being removed. How do I copy the data and conditional formatting from one workbook to another?
This is what I have so far:
Dim Master As Workbook
Set Master = ActiveWorkbook
Dim lastRow As Long
lastRow = ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Row
Range("A2:AE2" & lastRow).Select Selection.Copy
Master.Activate
ThisWorkbook.ActiveSheet.Cells(lastRow, 1).Paste
I have tried Paste Special and it does not work either.
Use xlPasteFormats
https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.xlpastetype.aspx
ThisWorkbook.ActiveSheet.Cells(lastRow, 1).PasteSpecial xlPasteFormats
If that doesnt work, try pasting twice. First the values, then the formatting.
ThisWorkbook.ActiveSheet.Cells(lastRow, 1).PasteSpecial xlPasteValues
ThisWorkbook.ActiveSheet.Cells(lastRow, 1).PasteSpecial xlPasteFormats

VBA Code works differently in different workbooks

Hello I have a simple VBA code, which copies a column and pastes it in another column without the empty cells, which are in the original column. It works in the woorkbook, where I wrote it. But I copied it to another one, where I need it. It copies the the needed cells more than once and it fills the whole column the these values.
Range("f5:f2500").ClearContents
With Range("d5:d2500")
.Offset(, 0).SpecialCells(xlCellTypeFormulas, _
xlNumbers).Copy
.Offset(, 2).PasteSpecial skipblanks:=True, _
Paste:=xlPasteValues
End With
The Sub below will copy the formulas (with values in cells) from column D, and paste their values in column F, starting from Cell "F5" and down (without blanks).
The Sub receives the Worksheet.Name as an argument, so all the Ranges inside are fully qualified with that certain worksheet's name.
Code
Option Explicit
Sub CopyColumnWOBlanks(wsName As String)
With Worksheets(wsName)
.Range("F5:F" & .Cells(.Rows.Count, "F").End(xlUp).Row).ClearContents
.Range("D5:D" & .Cells(.Rows.Count, "D").End(xlUp).Row).SpecialCells(xlCellTypeFormulas, xlNumbers).Copy
.Range("F5").PasteSpecial Paste:=xlPasteValues, skipblanks:=True
End With
End Sub
This Main sub below, is just for testing, modify it to fit your needs.
Sub Main()
CopyColumnWOBlanks ("Sheet5") ' <-- change "Sheet5" to whatever worksheet you want the macro to run
End Sub

VBA - Copying worksheet from one workbook to another (values, not formulas)

Currently, I am copying the values and formats of a worksheet to a worksheet in another workbook as such:
workbooks("book1.xlsm").sheets(1).range(someRange).copy
with workbooks("book2.xlsm").sheets(1).range("A1")
.pasteSpecial xlPasteValues
.pasteSpecial xlPasteFormats
end with
I would like to use something like:
workbooks("book1.xlsm").sheet(1).copy after:=workbooks("book2.xlsm").sheet(1)
The problem is that some of the cells in sheet(1) of book1.xlsm have formulas. Using the second method pulls the formulas, and the resulting values are linked to the data in book2.xlsm
How can I use the second method, but have the values, not formulas, copied?
If not possible, what are some alternatives, aside from the first method, which I am currently using?
You can break the links after the copy operation:
Option Explicit
Sub copySheetValues()
With Workbooks("Book2")
Workbooks("Book1").Worksheets(1).Copy After:=.Worksheets(1)
Application.Wait Now + TimeValue("0:00:01") 'built-in replacement for "Sleep"
.BreakLink Name:=Workbooks("Book1.xlsm").FullName, Type:=xlLinkTypeExcelLinks
End With
End Sub
Note: both files need to be open in the same instance of the Excel application

How to paste value of a sheet using VBA?

I have always used following to make a sheet to contain only values:
Sheets("NameOfTheTab").Activate
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:= xlPasteValues
But this is not safe, if someone/event change the selection, the program runs into random behaviour. How can can I get rid of this pattern?
Well, for one thing, you can avoid selecting the cells, just directly copy them:
Sheets("NameOfTheTab").Activate
Cells.Copy
Cells(1,1).PasteSpecial Paste:= xlPasteValues
Although this might not be the fastest way, depending on your sheet/actual problem.
You should always avoid using select method, beacuse it is not reliable. The sub below is a sample to copy data from a worksheet and paste only values to another one. This sample sub assumes you are running this macro from your target workbook if not change ThisWorkbook to your target workbook.
Sub copy_paste_only_values()
'will copy all cells in your tab that contain data
ThisWorkbook.Worksheets("NameOfTheTab").Cells.Copy
'will paste only values to your target worksheet
ThisWorkbook.Worksheets("NameOfTheTargetTab").Range("A1")._
PasteSpecial Paste:=xlPasteValues
'empty the clipboard
Application.CutCopyMode = False
End Sub

Multiple data sets, input into separate workbook, output each dataset into own field

I am very new, never done programming before and never used a forum before. I have read a lot of other posts to get as far as I have done in Excel to try and get it to perform as I require.
Basically I have a number of data sets, each with 4 variables, each set needs to be copied into appropriate fields on another worksheet, then the 2 outputs from this, recorded back onto the first sheet in 2 separate columns for each data set.
I have got the macro to do nearly all of it however it pastes only the last set of data outputs in the cells not each individual set.
Unfortunately I appear not to be able to add screen shots.
Currently my macro text is:
Sub macro1()
Dim rCell As Range
Dim rRng As Range
Set rRng = Sheet1.Range("C2:C6")
For Each rCell In rRng.Cells
rCell.Copy
Sheets("Sheet2").Select
Range("C2").Select
Sheets("Sheet2").Paste
Next rCell
Dim rCell2 As Range
Dim rRng2 As Range
Set rRng2 = Sheet1.Range("D2:D6")
For Each rCell2 In rRng2.Cells
rCell2.Copy
Sheets("Sheet2").Select
Range("D2").Select
Sheets("Sheet2").Paste
Range("C8").Select
Selection.Copy
Sheets("Sheet1").Select
Range("J2:J6").PasteSpecial Paste:=xlPasteValues
Next rCell2
Dim rCell3 As Range
Dim rRng3 As Range
Set rRng3 = Sheet1.Range("E2:E6")
For Each rCell3 In rRng3.Cells
rCell3.Copy
Sheets("Sheet2").Select
Range("E2").Select
Sheets("Sheet2").Paste
Next rCell3
Dim rCell4 As Range
Dim rRng4 As Range
Set rRng4 = Sheet1.Range("F2:F6")
For Each rCell4 In rRng4.Cells
rCell4.Copy
Sheets("Sheet2").Select
Range("F2").Select
Sheets("Sheet2").Paste
Range("D8").Select
Selection.Copy
Sheets("Sheet1").Select
Range("K2:K6").PasteSpecial Paste:=xlPasteValues
Next rCell4
End Sub
Apologies for the repetition, I hope someone can help.
Also if there are any good books that people can rate to learn basic macro and programming language that would be great.
You are using Range variables which is good but you are then using Select which should be avoided.
If you want to copy from Sheets("Sheet1").Range("C2:C6") and paste into Sheets("Sheet2").Range("C2:C6"), you can do this in one line of code. You do not need to loop through the cells for this:
Sheets("Sheet1").Range("C2:C6").Copy Sheets("Sheet2").Range("C2")
Your code as posted was looping through the cells but was always pasting into the same cell.
You can copy a larger block of code and it looks like your code is trying to copy cells C2:F6. Normally you can do that with one line:
Sheets("Sheet1").Range("C2:F6").Copy Sheets("Sheet2").Range("C2")
But maybe the paste special causes the values in columns D & E to get changed?
The PasteSpecial method requires two lines of code but this seems ok in your code. These lines of code copies one value and pastes it into five cells:
Sheets("Sheet2").Range("C8").Copy
Sheets("Sheet1").Range("J2:J6").PasteSpecial Paste:=xlPasteValues
You can simplify your code to:
Sheets("Sheet1").Range("C2:C6").Copy Sheets("Sheet2").Range("C2")
Sheets("Sheet1").Range("D2:D6").Copy Sheets("Sheet2").Range("D2")
Sheets("Sheet2").Range("C8").Copy
Sheets("Sheet1").Range("J2:J6").PasteSpecial Paste:=xlPasteValues
Sheets("Sheet1").Range("E2:E6").Copy Sheets("Sheet2").Range("E2")
Sheets("Sheet1").Range("F2:F6").Copy Sheets("Sheet2").Range("F2")
Sheets("Sheet2").Range("D8").Copy
Sheets("Sheet1").Range("K2:K6").PasteSpecial Paste:=xlPasteValues
And maybe copy larger blocks of cells to reduce the number of copy operations?
Sheets("Sheet1").Range("C2:D6").Copy Sheets("Sheet2").Range("C2")
Sheets("Sheet2").Range("C8").Copy
Sheets("Sheet1").Range("J2:J6").PasteSpecial Paste:=xlPasteValues
Sheets("Sheet1").Range("E2:F6").Copy Sheets("Sheet2").Range("E2")
Sheets("Sheet2").Range("D8").Copy
Sheets("Sheet1").Range("K2:K6").PasteSpecial Paste:=xlPasteValues