columns property for pasting the row - vba

Hello I have the code for copying the particular row and paste it in particular column
the code is
Range(rng, rng.End(xlToRight)).copy
Columns(c).Offset(, 6).PasteSpecial Transpose:=True
It is working correctly
But when i wanted to start pasting it from 2 cell of particular column i,e
Range(rng, rng.End(xlToRight)).copy
Columns(c).Offset(2, 6).PasteSpecial Transpose:=True
it is giving
"object defined error"
please help me

This should work:
Cells(2, Cells(1,Columns(c).Column).offset(,6).Column).PasteSpecial Transpose:=True
In the above ling you are selecting Row 2 by Cells(2, and 6 columns to the right of whatever c is by Columns(c).Column).Offset(,6).Column)
The reason that Columns(c).Offset(2,6) does not work is because you are telling excel to Offset an Entire Column by 2 rows, which you can't since it would effectively push the data off the worksheet.
You can offset EntireColumns for a given number of columns and EntireRows for a given number of rows, but not EntireColumns by Rows and EntireRows by Columns.

You can't offset a column by row, column is always whole. You should offset a cell.
Columns(c).cells(1).Offset(1, 6).PasteSpecial Transpose:=True
or
Columns(c).cells(2).Offset(, 6).PasteSpecial Transpose:=True
or
Columns(c).cells(2, 7).PasteSpecial Transpose:=True
or
cells(2, c+6).PasteSpecial Transpose:=True

Related

Taking columns of data copying and pasting them under eachother VBA

I know this is a real simple solution, but what I want to do is take column D14:E, and paste the numerical values into another part of the worksheet, the correct code is below:
Range("D14", Cells(Rows.Count, "E")).Copy
Range("AH14", Cells(Rows.Count, "AI").End(xlUp)).PasteSpecial Paste:=xlPasteValues
My issue here is when I am trying to take a second set of columns and paste it underneath the one I just pasted. The code I am trying is below:
Range("N14", Cells(Rows.Count, "O")).Copy
Range("AH14" & Cells(Rows.Count, "AI").End(xlUp)).PasteSpecial Paste:=xlPasteValues
Note that the columns are varying sizes (meaning column D could be a different size because of previous variables that were inputted (I had a question that was answered previously if you want to know what I mean)).
Range("N14", Cells(Rows.Count, "O").End(xlUp)).Copy ' Copy columns
Range("AH14").End(xlDown).Offset(1, 0).Select ' Find the first free column, select it
Selection.PasteSpecial Paste:=xlPasteValues 'paste copied cells

Dragging Formulas into next blank column and pasting values on previous column

I'm looking to use a macro to drag formulas from the end column to the next blank column and then paste values on the previous column.
For example, column V contains the formulas at first. I want to then drag these formulas into column W (blank column) and Paste Values in column V, but I want to write the code in a way that when it comes to using the macro again the formulas that are now in column W will be dragged into column X and then have their values pasted in column X.
This is what I've got so far:
Sub RollFile()
Columns("V3:V114").Select
Selection.AutoFill Destination:=Columns("V3:W114"), Type:=xlFillDefault
Columns("V3:V114").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("W2").Select
End Sub
Any help would be great.
I would recommend using the Range.Cells property- https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-cells-property-excel
Essentially, Columns("V3:V114") in your code would be replaced by something like
Range(Cells(3, 22), Cells(114, 22))
Now you can use variables instead of the numbers to iterate using a loop.

Excel VBA PasteSpecial Transpose:=True Posts only the last value in range

I have this code which will find all the values in column B where Column A matches the declared value ThisCell. I know there are about 15 matching values. When I try to paste these values in the cells to the right of ThisCell using this code
For i = 4 To Finalrow
If Cells(i, 1) = ThisCell Then
Range(Cells(i, 2), Cells(i, 2)).Copy
ThisCell.Offset(0, 1).PasteSpecial Transpose:=True
End If
I only get the last value in the cell to the right of This Cell. I see the cell updating through all the values. When I add an resize argument like this
ThisCell.Offset(0, 1).resize(,20).PasteSpecial Transpose:=True I get 20 cells filled with the last value. Any idea how to write the ThisCell.Offset(0, 1).PasteSpecial Transpose:=True so I get all the values?
I should have thought of this first--the problem was with how I wrote the the Thiscell.offset line. Using the cells method I rewrote the code as
Cells(ThisCell.Row, Columns.Count).End(xlToLeft).Offset(, 1).PasteSpecial
_ Transpose:=True
And that worked like a charm.

Copy and paste active column without changing cells formats

This code helps me to copy and paste active column in values but the numbers are pasted with two decimals. I'm using this code because my sheet has merged cells inside and also some cells within the column have dates.
ActiveCell.Offset(0, 0).EntireColumn.Value = ActiveCell.EntireColumn.Value
I need to paste the column in values exactly in the format that the column has the format. Leave the numbers without the decimals.
Note: If you do it manually by selecting the column and then selecting paste in values from the Home bar, it pastes it exactly as it was with the formulas.
Try it this way:
Dim r As Long
c = Selection.Column
Range(Cells(4, c + 1), Cells(5, c + 1)).Merge
Columns(c).EntireColumn.Copy
Columns(c + 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
You may need to tweak some stuff, like merging certain cells, where you want the cell pasted in relation to the copied cell. Let me know how this works

Subtracting from a column based on column names instead of column indexes

I have a column named "Re-Align Midpoint" whose result is the date of another column (named "Final Execution Date") minus 100 days. Right now, the "Re-Align Midpoint" date is in column AZ while "Final Execution Date" is in column BR. These columns are subject to be moved around, so it is impractical for me to build a macro based on column indexes. What I would like to do instead is subtract one from the other based on column names. The header rows are on row 2, not row 1
This is what I have for my recorded macro:
Sub Re_Align_Midpoint_Date
Range("AZ3").Select
ActiveCell.FormulaR1C1 = "=RC[18] - 100"
Range("AZ3").Select
Selection.AutoFill Destination:=Range("AZ3:AZ142"), Type:=xlFillDefault
Range("AZ3:AZ142").Select
ActiveWindow.SmallScroll Down:=-132
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
End Sub
As long as the column headings won't change you can use an hLookup to do this:
=HLOOKUP("Final Execution Date",$A$2:$ZZ3,ROW(A2),FALSE)-100
Just paste this formula at the top of your "Midpoint" column and fill down.