Copy row, transpose and paste - excel

I need a simple twist to a common vba code that copy and paste columns/rows
What I am trying to do is copy the entire row in sheet 2 , transpose and then paste them into Column A , Sheet1. The first row in sheet1 has the headings so I have to paste them into A2 that extends to the whole column
Sub transpose2()
Sheets(2).Range("A1", Cells(Columns.Count, "A").End(xlRight)).Copy
Sheets(1).Range("A2").PasteSpecial transpose:=True
Range("A1").ClearOutline
End Sub
This doesn't seem to work.
Can anyone help me with this?
Thank you!

Sub transpose2()
With Sheets(2)
.Range(.Range("A1"), .Cells(1, .Columns.Count).End(xlToLeft)).Copy
End With
Sheets(1).Range("A2").PasteSpecial transpose:=True
Range("A1").ClearOutline 'which sheet?
End Sub

Related

VBA- How to copy a column from one workbook and paste its values to another workbook? [duplicate]

I have some VBA code that copys a range from one sheet and then pastes it to another at the first blank line. What it is copying are vlookup formulas so when it pastes it pastes all 0's, how would I go about getting it to paste what it copies as values so the results are retained?
Code:
Private Sub PasteChartDataQtyCompare()
'This step pastes the range of values to the chart data tab
Sheets(1).Range("A6:J22").Copy _
Destination:=Sheets("Chart Data").Cells(Sheets("Chart Data").Rows.Count, 1).End(xlUp).Offset(1, 0)
End Sub
Transfer the values directly bypassing the clipboard.
Private Sub PasteChartDataQtyCompare()
'This step pastes the range of values to the chart data tab
with workSheets(1).Range("A6:J22")
workSheets("Chart Data").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).resize(.rows.count,.columns.count) = .value
end with
End Sub
Use Range Method "Range.PasteSpecial xlPasteValue"
Example:
Sheets("Sheet1").Columns("A").Copy
Sheets("Sheet2").Columns("B").PasteSpecial xlPasteValues
You want to use
.PasteSpecial xlPasteValues
A similar question was answered in detail here: Excel VBA Copy Paste Values only( xlPasteValues )

how to value to integer VBA

I’m trying to create VBA that allows me to copy the one of a cell and paste over until end of the row in the same column.
But I’m unable to figure out how this works, I have a total of 109 rows with 20 columns and in column “BD” that is an empty column and I want to put today date and fill up rest of the same column until end of row.
Please see the code at the bottom
Sub CopyInsert()
Range("BD2").Select
ActiveCell.FormulaR1C1 = "=TODAY()"
Range("BD2").Select
Selection.Copy
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
ActiveSheet.Paste
End Sub
This code is working fine but I’m not getting the right result what these codes do is copy and paste over pass 109 rows mean is paste over up to around 2000 rows
My question is how can I copy and paste over until the end of the row like till row 109! If we have over 200 rows, how to copy and paste until end of the row, row 200 in one column! But not using (BD2:BD), I have tried (BD2:BD) and is paste over 109 rows is not stopped at the end of row.
many thanks.
So you need to find the lastRow based on the column where the actual data is...
Try something like this:
With worksheets("mySheet")
Dim lastRow as Long
lastRow = .Cells(.Rows.Count,1).End(xlUp).Row 'this uses column A for example
.Range("BD2:BD" & lastRow).Formula = "=TODAY()"
End With

VBA, empty cell is at last row

In a VBA Excel macro,
I copy rows from another sheet, select a different sheet, and try to find the next open cell before pasting them.
Range("A1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues
The page has a first row of column titles, and find then passes over these to the very last row???
If dummy data
is put in the second row, or if there is data already present,
it works.
WHY is it doing this?
Range.End(xlDown) is equivalent to pressing Ctrl + ↓.
Test it on your own - if you only have a header in Row 1, then you'll jump to the last row in the Worksheet. If you have data below the header, you'll move to that row.
Use Range.End(xlUp) to find the next available row by moving up from the last row on the Worksheet - something like this:
With Sheet1
.Cells(.Rows.Count, 1).End(xlUp).Offset(1).PasteSpecial xlPasteValues
End With
standing your data layout, you can use:
Cells(WorksheetFunction.CountA(Columns(1)) + 1, 1).PasteSpecial Paste:=xlPasteValues
or, using explicit worksheet references (recommended):
With Worksheets("mySheetName")
.Cells(WorksheetFunction.CountA(.Columns(1)) + 1, 1).PasteSpecial Paste:=xlPasteValues
End With

VBA paste as values - how to

I have some VBA code that copys a range from one sheet and then pastes it to another at the first blank line. What it is copying are vlookup formulas so when it pastes it pastes all 0's, how would I go about getting it to paste what it copies as values so the results are retained?
Code:
Private Sub PasteChartDataQtyCompare()
'This step pastes the range of values to the chart data tab
Sheets(1).Range("A6:J22").Copy _
Destination:=Sheets("Chart Data").Cells(Sheets("Chart Data").Rows.Count, 1).End(xlUp).Offset(1, 0)
End Sub
Transfer the values directly bypassing the clipboard.
Private Sub PasteChartDataQtyCompare()
'This step pastes the range of values to the chart data tab
with workSheets(1).Range("A6:J22")
workSheets("Chart Data").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).resize(.rows.count,.columns.count) = .value
end with
End Sub
Use Range Method "Range.PasteSpecial xlPasteValue"
Example:
Sheets("Sheet1").Columns("A").Copy
Sheets("Sheet2").Columns("B").PasteSpecial xlPasteValues
You want to use
.PasteSpecial xlPasteValues
A similar question was answered in detail here: Excel VBA Copy Paste Values only( xlPasteValues )

Copy certain excel columns based on ones criteria

First thing I did was create a button that would copy certain cells using this code:
Worksheets("Sheet1").Range("A:A,B:B,D:D").Copy _
and it worked fine.
Second, I found the code that would copy all details in a row based on the criteria of one, in this case if there was an "A" in the "Location" column.
Private Sub ENTIREROW_Click()
'Sub copyrows()
Dim i As Range, Cell As Object
Set i = Range("D:D") 'Substitute with the range which includes your True/False values
For Each Cell In i
If IsEmpty(Cell) Then
Exit Sub
End If
If Cell.Value = "A" Then
Cell.ENTIREROW.Copy
Sheet2.Select 'Substitute with your sheet
ActiveSheet.Range("A65536").End(xlUp).Select
Selection.Offset(1, 0).Select
ActiveSheet.Paste
End If
Next
End Sub
My question is, how do I copy all information in the specified columns (A,B,D) where there is an "A" in "Location" in one button.
Furthermore, this is my example data, the sheet I will actually use this on has 34 columns to copy. Is there a more efficient way of setting a range when you don't want an entire sequence, everything but the data in column C?
Thanks in advance and apologies for my explanation skills.
One way maybe to:
filter your source
hide column C
copy the result using .PasteSpecial xlPasteValues into the destination
Unhide column C on the source sheet
remove the autofilter
Using xlPasteValues only pastes the visible cells from the source - so no column C
The code then looks like this: .
Sub CopyRows()
With Sheets(1).Range([A2], [A2].SpecialCells(xlLastCell))
[A1].AutoFilter
.AutoFilter Field:=4, Criteria1:="A"
[C:C].EntireColumn.Hidden = True
.Copy
[C:C].EntireColumn.Hidden = False
End With
With Sheets(2)
If .Cells(Sheets(2).Rows.Count, 1).End(xlUp) = "" Then 'it's a clean sheet
.Cells(Sheets(2).Rows.Count, 1).End(xlUp).PasteSpecial Paste:=xlPasteValues
Else
.Cells(Sheets(2).Rows.Count, 1).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
End If
End With
Application.CutCopyMode = False
Sheet1.[A1].AutoFilter
End Sub

Resources