i have a little problem with VBA. I want to paste data from one sheet and specific range to another sheet and last empty cell. I need to paste Value, Number format and theme. Is there any macro to select last paste range and paste into this range theme? Or is there any macro which can paste all data without formulas?
My code:
Dim xScreenUpdating As Boolean
Dim xPasteSht As Worksheet
Dim xRg As Range
Dim xTxt As String
On Error Resume Next
Worksheets("KOPÍROVAT ASISTENT").Range("C1:G54").Copy
Set xPasteSht = Worksheets("Archiv")
xRg.Copy
xPasteSht.Cells(Rows.Count, 1).End(xlUp).Offset(0, 0).PasteSpecial xlPasteValuesAndNumberFormats, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub
I'm not sure what .Offset(0,0) achieves - .Offset(1,0) is what you actually need to find the last empty cell. That aside, the following code snippet should give you what you want. Let me know how you go with it.
Sheets("KOPÍROVAT ASISTENT").Range("C1:G54").Copy
With Sheets("Archiv").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With
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,"")
I'm trying to take a range from one sheet and copy it to the next empty row in another sheet (basically, paste into the range A12:D12 for next empty row in the other sheet). The range will never change. I've seen a lot of questions like this, with people saying the answers work great, but I can't get it to work.
Very new to VBA. Here is the code I'm using:
Private Sub CommandButton1_Click()
Dim NextRow As Range
Set NextRow = Range("A" & Sheets("Sheet3").UsedRange.Rows.Count + 1)
Sheet1.Range("A12:D12").Copy
Sheet3.Activate
NextRow.PasteSpecial Paste:=xlValues, Transpose:=False
Application.CutCopyMode = False
Set NextRow = Nothing
End Sub
This runs but it doesn't actually paste any values into Sheet3. Is there something I'm missing? Is there a better way to do this?
Thanks!
You just had an issue in the second line defining NextRow.
Is there a better way to do this? It depends on your needs; personally I do not like to activate/select other cells during a VBA macro so e.g. I would get rid of the Sheet3.Activate. I would also copy the stuff 'manually' without using the clipboard to avoid changing the user's clipboard contents.
Private Sub CommandButton1_Click()
Dim NextRow As Range
Set NextRow = Sheet3.Range("A" & Sheet3.Rows.Count).End(xlUp).Offset(1, 0)
Sheet1.Range("A12:D12").Copy
Sheet3.Activate
NextRow.PasteSpecial Paste:=xlValues, Transpose:=False
Application.CutCopyMode = False
Set NextRow = Nothing
End Sub
I have a range of data A1:M55 that changes often and I would like to take snap shots for historcial purpsoes of the range and paste as a picture to the right in the worksheet. Each time the macro is ran it would paste the picture one row paste the last pasted picture, leaving an empty row between each pasted event. Also would like the first paste to occure in cell AA:1.
A version of the below is what I attempted but based on the results I am using the incorrect approach.
Sub CopyHistory2()
Range("A1:M55").Select
Selection.Copy
ActiveSheet.Range(ActiveSheet.UsedRange.Columns.Count + 1).Select
Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub
Was able to combine solutions from multiple questions to get the output I desired.
Dim source As Worksheet
Dim destination As Worksheet
Dim emptyColumn As Long
Set source = Sheets("Sheet1")
Set destination = Sheets("Sheet1")
'find empty Column (actually cell in Row 1)'
emptyColumn = destination.Cells(1, destination.Columns.Count).End(xlToLeft).Column
If emptyColumn > 1 Then
emptyColumn = emptyColumn + 1
End If
source.Range("A1:M55").Copy
destination.Cells(1, emptyColumn).PasteSpecial xlPasteValuesAndNumberFormats
destination.Cells(1, emptyColumn).PasteSpecial xlPasteFormats
Application.CutCopyMode = False
I'm trying to run a macro where it should get the last active line, copy all data to a new sheet, apply a filter (numbers on K row >15,9), copy and paste the results in a new sheet.
However, after aplying the filter, nothing is pasted in the new sheet.
Any ideas why?
Thank you!
Sub Macro1()
'Select and paste all data, couldn't work on a "last active line" in here..
Cells.Select
Selection.Copy
Sheets("Plan2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1:O1").Select
Application.CutCopyMode = False
'Aplying the filter
Selection.AutoFilter
ActiveSheet.Range("$A$1:$O$1056").AutoFilter Field:=11, Criteria1:=">15,9" _
, Operator:=xlAnd
'Here I'm trying to past the filtered data in the new sheet, but the result appears in blank
Cells.Select
Selection.Copy
Sheets("Plan3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Here i came back and turned the autofilter off, but it was useless
Sheets("Plan2").Select
Application.CutCopyMode = False
ActiveSheet.Range("$A$1:$O$1056").AutoFilter Field:=11
End Sub
You have a few issues in the code:
Using Cells can be very tricky, especially when you don't define them well
Using Select is never a good practice. Just stick with working directly with objects (Sheets, Ranges, Workbooks, etc.).
I don't know why you need to copy the entire data set to a new sheet, then filter it to copy onto a 3rd sheet. It's possible to just filter the original data set and copy to the final sheet. I did not adjust the code for this, because there may be a reason you need to do this, but so you know, you can just work with the original data without the middle step of copying to another sheet to filter.
You may need to filter on 15.9 instead of 15,9, even if the comma is your decimal separator. (This may not be true, but I am adding it in in case it is (I don't have experience working on European systems in Excel.) Also, see David Zemens comment above.
In the below code, I have qualified all sheets and ranges, found the last row and provided comments where I made some assumptions. Modify it to fit your exact structure, and let me know if it works.
Sub Macro1()
'Select and paste all data, couldn't work on a "last active line" in here..
Dim wsCopy As Worksheet, wsPlan2 As Worksheet, wsPlan3 As Worksheet
Set wsCopy = Sheets("mySheet") ' replace with correct sheet name
Set wsPlan2 = Sheets("Plan2")
Set wsPlan3 = Sheets("Plan3")
'this will copy only cells with data in (*note -> this could copy more than that, but I will not go into it, for now, it's sufficient to use this)
With wsCopy
'find last row
Dim lRow As Long
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("A1:O" & lRow).Copy 'assume the data goes to column O, change if need be
End With
With wsPlan2
'paste to sheet Plan 2
.Range("A1").PasteSpecial xlPasteValues
'find last row
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
With .Range("A1:O" & lRow)
'Aplying the filter
.AutoFilter 11, ">15,9" 'you may need to use 15.9 here if you indeed mean 15 and 9/10ths. even if the comma separator is what you use to show decimals
.Copy
End With
wsPlan3.Range("A1").PasteSpecial xlPasteValues 'change range reference if you need it
.AutoFilterMode = False
End With
End Sub
Right now, I'm doing this to copy a row from one sheet to another sheet and insert it at the end:
Sheets("Monthly").Range("A1").EntireRow.Copy Destination:= _
Sheets(Name).Range("A" & rows.Count).End(xlUp).Offset(1)
But this doesn't copy any formatting/cell spacing. Is that possible to do in vba?
Copy does copy the formatting. Maybe your formatting isn't what you think. I don't know what cell spacing means, but if it's column width, then Copy won't copy that. You'll have to set that explicitly, like
For i = 1 to rSrce.Cells.Count
rDest.Cells(i).EntireColumn.ColumnWidth = rSrce.Cells(1).EntireColumn.ColumnWidth
Next i
Using the Macro Recorder will give you the syntax for other formats.
Range("A1").NumberFormat = "General"
Or
.PasteSpecial Paste:=xlPasteFormats
.PasteSpecial Paste:=xlPasteValues
Ex. of a way to solve it
Sub Copy()
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
ws.Range("A1").EntireColumn.Copy
With ws.Range("G2").EntireColumn
.PasteSpecial Paste:=xlPasteFormats
.PasteSpecial Paste:=xlPasteValues
End With
End Sub
There are a million ways to do this try
https://www.google.com/search?q=how+to+copy+formatting+in+excel+with+vba&oq=how+to+co&aqs=chrome.0.69i59l2j69i57j69i59j69i60j0.2240j0j7&sourceid=chrome&espv=210&es_sm=93&ie=UTF-8