Copy and paste active column without changing cells formats - vba

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

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.

VBA macro copy column from one sheet to another with blanks in between

I need to copy a column (say A) from sheet ABC to another column say B in sheet DEF.
I have been using this code:
Sheets("ABC").Activate
ActiveSheet.Range("A1").Select
ActiveSheet.Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("DEF").Activate
ActiveSheet.range("B1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False'
This works fine, but it does not copy any further values if a blank value comes in between. Could you please help me out with this?
I need the entire column A in sheet ABC to be there in column B in DEF
Thanks a lot for your help!
The following code creates a variable containing a reference to the source range, then sets the destination range's Value to the source range's Value:
Dim SrcRng As Range
With Worksheets("ABC")
Set SrcRng = .Range(.Cells(1, "A"), .Cells(.Rows.Count, "A").End(xlUp))
End With
Worksheets("DEF").Range("B1").Resize(SrcRng.Rows.Count, 1).Value = SrcRng.Value
It also avoids using Select and Activate. (See How to avoid using Select in Excel VBA macros.)

columns property for pasting the row

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