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.
Related
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.
Hopefully this will be a quick fix.
I've googled around and all the VBA codes are to auto-fill a column down to the last cell. However, I'm trying to auto-fill across a row.
My current code snippet is below. What I want to do is take the heading at the top which has the date in it and create a new heading that just has the month and year. This is for the rest of the macro to run. However, right now, users are having to manually do the formula auto-fill across the top.
My code is:
Rows("1:1").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("D1").Select
ActiveCell.FormulaR1C1 = "=TEXT(R[1]C,""mmm-yyyy"")"
LastCol = Range("D1").End(xlToRight).Column
Range("D1").Select
Selection.AutoFill Destination:=Range("D1:AD1"), Type:=xlFillDefault
Range("D1:AD1").Select
In the line that is:
Selection.AutoFill Destination:=Range("D1:AD1"), Type:=xlFillDefault
Is there anyway to get "AD1" to be whatever the last cell in the new row 2 is?
First avoid .Select it slows the code and can be Then you can just fill the formula without the need of Autofill.
Also get in the habit of declaring the parent worksheet of all ranges.
We find the last used column with .Cells(2,.Columns.Count).End(xltoLeft).Column).
Using that in another Cells() we get the last cell in row 1, .Cells(1,.Cells(2,.Columns.Count).End(xltoLeft).Column).
So, .Range("D1",.Cells(1,.Cells(2,.Columns.Count).End(xltoLeft).Column)) creates a range from D1 to the last used column in Row 2:
With ActiveSheet 'Should be replaced with your sheet name: Worksheets("Sheet1")
.Rows("1:1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
.Range("D1",.Cells(1,.Cells(2,.Columns.Count).End(xltoLeft).Column)).FormulaR1C1 = "=TEXT(R[1]C,""mmm-yyyy"")"
End With
a possible solution.
Rows("1:1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("D1").FormulaR1C1 = "=TEXT(R[1]C,""mmm-yyyy"")"
lastcol = Cells(2, Columns.Count).End(xlToLeft).Column
Range("D1").AutoFill Destination:=Range("D1", Cells(1, lastcol)), Type:=xlFillDefault
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.)
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
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