copy and paste from one sheet to another without the formulas from the first page - vba

This is simple in concept but hard for me to do in practice.
Ive got information on Sheet Separate range A6 to M65 and trying to paste all in information on Sheet Final into the first blank row without transferring all the formulas from Separate.
Moves all information to final
Worksheets("Seperate").Range("A6:K100").Copy Worksheets("Final").Range("A6")

Use PasteSpecial function. Read on MSDN details. You need to pass (probably) xlPasteValues as parameter.
Worksheets("Seperate").Range("A6:K100").Copy
Worksheets("Final").Range("A6").PasteSpecial <put_here_parameters>

This code only copy value of range.
Sub test()
Dim vDB As Variant
Dim Target As Range
Set Target = Worksheets("Final").Range("A" & Rows.Count).End(xlUp)(2)
vDB = Worksheets("Seperate").Range("A6:K100")
Target.Resize(UBound(vDB, 1), UBound(vDB, 2)) = vDB
End Sub

Related

Copy values, not formulas, on a worksheet to another

I added a macro to copy a worksheet to another worksheet, so that any changes made after that point can be compared to the original. However, my macro copies over formulas instead of just the values, so when something changes, both sheets change, and the copy serves no purpose. What I have is:
Worksheets("First Sheet").Cells.Copy _
Destination:=Worksheets("Second Sheet").Cells
Is there an easy way to fix this? Thanks!
You need to use Copy >> PasteSpecial and paste only values, this is a 2-line syntax:
Worksheets("First Sheet").Cells.Copy
Worksheets("Second Sheet").Cells.PasteSpecial xlPasteValues
After your macro, you can write this one:
With Worksheets("First Sheet")
Worksheets("Second Sheet").Range(.UsedRange.Address).Cells.Value2 = .UsedRange.Value2
End With
It takes the values of the first sheet and it puts them to the second sheet. The trick with UsedRange is needed, because Worksheets(2).Cells.Value2 = Worksheets(1).Cells.Value2 goes above the usual resources of a normal PC.
This will skip all formula cells in the first sheet:
Sub KopyKat()
Dim r As Range, addy As String
For Each r In Worksheets("First Sheet").Cells.SpecialCells(2)
addy = r.Address
r.Copy Destination:=Worksheets("Second Sheet").Range(addy)
Next r
End Sub

Copy and paste range to new destination

The function I'm trying to build into my spreadsheet is 'copy selected range and past to new range then every time I run the macro it will carry out the same copy.paste.range to new range but paste destination will follow on from previous pasted values. The function is collection stats on a weekly basis and the macro will automate the date collection.
I had the initial step working but its pasting formulas instead of just the values. I’ve added the paste special function but it’s not working at all now. Once this has been pasted in I’m looking for the next copy and paste to follow on from the data pasted into D21:J27 e.g. D28:J34 (Offset maybe?)
Sub CopyPaste_Weeklys()
Sheets("MI Build").Range("D7:J13").Copy
Sheets("MI Build").Activate
Range("D21:J27").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNoneActiveSheet.Paste
Application.CutCopyMode = False
End Sub
Any help would be much appreciated.
Thanks
Gary
If all you want to do is copy the value in D7:J13 to D21:J27, your entire sub can be replaced by:
Sub CopyPaste_Weeklys()
Sheets("MI Build").Range("D21:J27").Value = Sheets("MI Build").Range("D7:J13").Value
End Sub
In VBA, copy/paste should be reserved for situations where you need to copy things like formulas and formats. For values it is better to just use the Value property of the respective ranges.
Presumably the ranges are not always D7:J13 and D21:J27, but your question provides no information as to how the ranges are to be determined. The fixed ranges above can be replaced by range variables that can be set at run time. Something on the following lines:
Sub CopyPaste_Weeklys()
Dim i As Long
Dim target As Range
With Sheets("MI Build")
i = .Cells(.Rows.Count, "D").End(xlUp).Row + 1
Set target = Range(.Cells(i, "D"), .Cells(i + 6, "J"))
target.Value = .Range("D7:J13").Value
End With
End Sub
The variable i is determined by a standard Excel VBA trick to determine the row number of the last row in a columns which contains data.

copying data to another workbook and adding to last row used

I have the following code which I got to work, copying a range from one workbook to another, however in need help so that when I open a different workbook I can copy the same range to the last row used in the consolidation file.
Sub ValuePaste()
Workbooks("04.17.17 SHELBY.xlsx").Worksheets("Summary $500-$10K").Range("B8:F21").Copy
Workbooks("Consolidated_template_new1_WA.xlsx").Worksheets("test").Range("C4").PasteSpecial Paste:=xlPasteValues
End Sub
Sub ValuePaste()
Dim loLastRow As Long
Workbooks("04.17.17 SHELBY.xlsx").Worksheets("Summary $500-$10K").Range("B8:F21").Copy
With Workbooks("Consolidated_template_new1_WA.xlsx").Worksheets("test")
loLastRow = .Cells(Rows.Count, 3).End(xlUp).Row + 1
.Range("C" & loLastRow).PasteSpecial Paste:=xlPasteValues
End With
End Sub
The variable loLastRow will contain the last Row of the worksheet specified in the "with" command above. After that the Range object uses this Information to build the first empty cell after the last used one.

Create a range using named cells

I need to create a range using named cells in vba.
So far I have the following which is not working;
Dim pasteRange As Range
Set pasteRange = Range(firstRow, 11)
pasteRange.Value(11) = slabTemplateSheet.Range("slabTemp").Value(11)
Where firstRow is an Integer. slabTemplateSheet refers to a worksheet and slabTemp is a named range in said sheet.
I thought it would be fairly simple as my paste range is only 1 row and 11 columns (i.e 11 cells in a row) but I can't get it to work.
In your answer, presuming there is one, could you also please give me the ability to paste multiple rows and columns, so for instance if slabTemp refers to A1:F16
Edit: I didn't make it clear what I am trying to do.
I have a named range called slabTemp in the worksheet slabTemplateSheet. In another sheet I want to copy that range, including the formatting, and paste it. I heard that using the copy/paste function was slow so I found the property above that supposedly does the same thing but is faster (i haven't tested it). Source, Durgesh's answer here: fast way to copy formatting in excel
In the new sheet I need to paste it into a range which is to be created (this is what i don't know how to do)
So Range(firstRow, 11) refers to the integer saved as firstRow (a row number) and 11 is the column number. But this doesn't work.
I guess my question, is how do i create a range using names rather than say Range("A1:G6") so instead Range(firstRow1:secondRow:6)
Thanks Again!
Here's a working example.
Public Sub CopyRange()
'Define the Copy Range
Dim CopyRange As Range: Set CopyRange = Range("MyCustomRange")
'Define the range to Paste Value to
Dim PasteRange As Range: Set PasteRange = Range("MyOtherCustomRange")
'Move the Copy Range into the Paste Range
'You don't need to specify the sheet name, the Defined named holds that information
'Important: The ranges should be the same size, otherwise you may get an error
PasteRange.Value() = CopyRange.Value()
End Sub
About how to create a Named Range (worksheet scope, not for Workbook per your code), use the code below. Not sure what are you trying to achieve in this line:
pasteRange.Value(11) = slabTemplateSheet.Range("slabTemp").Value(11)
Anyway, this is the code for the Named Range:
Sub UseNamedRange()
Dim slabTemplateSheet As Worksheet
Dim pasteRange As Range
Set slabTemplateSheet = Sheets("Sheet1")
' create the Named Range "slabTemp" (Named Range for Worksheet)
' you can manualy (or with variables) modify the Range("A1:F16")
slabTemplateSheet.Names.Add "slabTemp", Sheets("Sheet1").Range("A1:F16")
Set pasteRange = slabTemplateSheet.Range("slabTemp")
End Sub
It doesn't seem like you are trying to transfer the xlRangeValueXMLSpreadsheet XlRangeValueDataType enumeration of the cells; rather it sounds like you want 11 cells in a row.
with slabTemplateSheet.Range("slabTemp")
pasteRange.Resize(1, 11) = .Cells.Resize(1, 11).Value
end with

Using Vlookup to copy and paste data into a separate worksheet using VBA

Alright I'm a beginner with VBA so I need some help. Assuming this is very basic, but here are the steps I am looking at for the code:
-Use Vlookup to find the value "Rec" in column C of Sheet1, and select that row's corresponding value in column D
-Then copy that value from column D in Sheet1 and paste it into the first blank cell in column B of another worksheet titled Sheet2
I've got a basic code that uses Vlookup to find Rec as well as it's corresponding value in column D, then display a msg. The code works fine, and is the following:
Sub BasicFindGSV()
Dim movement_type_code As Variant
Dim total_gsv As Variant
movement_type_code = "Rec"
total_gsv = Application.WorksheetFunction.VLookup(movement_type_code,Sheet1.Range("C2:H25"), 2, False)
MsgBox "GSV is :$" & total_gsv
End Sub
I also have another one that will find the next blank cell in column B Sheet2, it works as well:
Sub SelectFirstBlankCell()
Dim Sheet2 As Worksheet
Set Sheet2 = ActiveSheet
For Each cell In Sheet2.Columns(2).Cells
If IsEmpty(cell) = True Then cell.Select: Exit For
Next cell
End Sub
Not sure how to integrate the two, and I'm not sure how to make the code paste the Vlookup result in Sheet2. Any help would be greatly appreciated, thanks!
So for being a beginner you're off to a good start by designing two separate subroutines that you can confirm work and then integrating. That's the basic approach that will save you headache after headache when things get more complicated. So to answer your direct question on how to integrate the two, I'd recommend doing something like this
Sub BasicFindGSV()
Dim movement_type_code As Variant
Dim total_gsv As Variant
movement_type_code = "Rec"
total_gsv = Application.WorksheetFunction.VLookup(movement_type_code, Sheet1.Range("C2:H25"), 2, False)
AssignValueToBlankCell (total_gsv)
End Sub
Sub AssignValueToBlankCell(ByVal v As Variant)
Dim Sheet2 As Worksheet
Set Sheet2 = ActiveSheet
For Each cell In Sheet2.Columns(2).Cells
If IsEmpty(cell) = True Then cell.Value2 = v
Next cell
End Sub
That being said, as Macro Man points out, you can knock out the exact same functionality your asking for with a one liner. Keeping the operational steps separate (so actually a two liner now) would look like this.
Sub FindGSV()
AssignValueToBlankCell WorksheetFunction.VLookup("Rec", Sheet1.Range("C2:H25"), 2, False)
End Sub
Sub AssignValueToBlankCell(ByVal v As Variant)
Sheet3.Range("B" & Rows.Count).End(xlUp).Offset(1, 0).Value2 = v
End Sub
Like I said, if you plan to continue development with this, it's usually a good idea to design your code with independent operations the way you already have begun to. You can build off of this by passing worksheets, ranges, columns, or other useful parameters as arguments to a predefined task or subroutine.
Also, notice that I use Value2 instead of Value. I notice you're retrieving a currency value, so there's actually a small difference between the two. Value2 gives you the more accurate number behind a currency formatted value (although probably unnecessary) and is also faster (although probably negligible in this case). Just something to be aware of though.
Also, I noticed your use of worksheet objects kind of strange, so I thought it'd help to mentioned that you can select a worksheet object by it's object name, it's name property (with sheets() or worksheets()), index number (with sheets() or worksheets()), or the "Active" prefix. It's important to note that what you're doing in your one subroutine is reassigning the reference of the Sheet2 object to your active sheet, which means it may end up being any sheet. This demonstrates the issue:
Sub SheetSelectDemo()
Dim Sheet2 As Worksheet
Set Sheet2 = Sheets(1)
MsgBox "The sheet object named Sheet2 has a name property equal to " & Worksheets(Sheet2.Name).Name & " and has an index of " & Worksheets(Sheet2.Index).Index & "."
End Sub
You can view and change the name of a sheet object, as well as it's name property (which is different) here...
The name property is what you see and change in the worksheet tab in Excel, but once again this is not the same as the object name. You can also change these things programmatically.
Try this:
Sub MacroMan()
Range("B" & Rows.Count).End(xlUp).Offset(1, 0).Value = _
WorksheetFunction.VLookup("Rec", Sheet1.Range("C2:H25"), 2, False)
End Sub
The Range("B" & Rows.Count).End(xlUp) command is the equivalent of going to the last cell in column B and pressing Ctrl + ↑
We then use .Offset(1, 0) to get the cell after this (the next blank one) and write the value of your vlookup directly into this cell.
If Both work, then good, you have two working subs and you want to integrate them. You probably want to keep them so they might be useful for some other work later. Integrating them means invoking them in some third routine.
For many reasons, it is surely better and advised to avoid as much as possible to use (select, copy, paste) in VBA, and to use rather a direct copying method (range1.copy range2).
You need to make your routines as functions that return ranges objects, then in some third routine, invoke them
Function total_gsv() as range
Dim movement_type_code As Variant: movement_type_code = "Rec"
Set total_gsv = Application.WorksheetFunction.VLookup(movement_type_code,Sheet1.Range("C2:H25"), 2, False)
End Sub
Function FindFirstBlankCell() as Range
Dim Sheet2 As Worksheet: Set Sheet2 = ActiveSheet
For Each cell In Sheet2.Columns(2).Cells
If IsEmpty(cell) Then Set FindFirstBlankCell= cell: exit For
Next cell
End Sub
Sub FindAndMoveGsv()
total_gsv.copy FindFirstBlankCell
... 'some other work
End Sub