I have been using this bit of code in my VBA tinkering for years:
Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row).Formula = _
""
Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
I now need to be able to take this function, and loop it through 1460 columns, but that 1460 will be variable.
My tries so far look like this:
Dim a As Interger
a = 3
Do While Cells(1, a) <> ""
Range(Range("R1Ca:RCa") & Range("A" & Rows.Count).End(xlUp).Row).Formula = _
"[super convoluted formula]"
Range(Range("R1Ca:RCa") & Range("A" & Rows.Count).End(xlUp).Row).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
a = a + 1
Loop
So my focused question at this point is how do I modify the "B2:B" in my starting example into a variable that can be looped to hit all of the columns?
It is probably best to calculate the "last row" once, e.g.
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
That then allows your current statement of
Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row).Formula =
to be replaced by the equivalent
Range("B2:B" & lastRow).Formula =
which can be re-expressed, using Cells instead of addresses, as
Range(Cells(2, "B"), Cells(lastRow, "B")).Formula =
This can then be easily converted to a formula which uses a variable for the column:
Range(Cells(2, c), Cells(lastRow, c)).Formula =
Note that the column parameter of the Cells object can be either expressed as a number (i.e. column 1, 2, 3, etc) or as a string (i.e. column "A", "B", "C", etc). Using a numeric value is useful when iterating over multiple columns. Using a string value is useful when you are just using a single, known, column - it makes the code more "readable".
Your rewritten code could therefore be:
Dim c As Long
Dim lastRow As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
For c = 2 To Cells(1, Columns.Count).End(xlToLeft).Column
Range(Cells(2, c), Cells(lastRow, c)).Formula = "[super convoluted formula]"
Range(Cells(2, c), Cells(lastRow, c)).Value = _
Range(Cells(2, c), Cells(lastRow, c)).Value
Next
(There is no need to do a copy/paste if you are just trying to paste values - it will be safer to just set the Value property to the Value property.)
Related
I have a code that involves a Drag Down Formula (FillDown). After the VLOOKUP is done, the FillDown method works well if there are 2 or more rows with values in it. But, I can't seem to understand why if there is only one row with values in it, the FillDown method does not work properly. It brings down the value of the first row (Header) instead and replaces the value. You may look at the picture I attached to see what I meant. After VLOOKUP, the rows should show the values as can be seen in the second picture I attached.
Workbooks("Data.xlsx").Activate
'COPY PASTE POSITIVE BARCODES
lastrow = Range("A" & Rows.Count).End(xlUp).Row
ActiveSheet.Range("A2:A" & lastrow).Copy
Workbooks("SIMKA CT VALUE FORMULA.xlsx").Activate
Sheets("Sheet3").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
'APPLICATION OF LOOKUP FORMULA
lastrow = Range("A" & Rows.Count).End(xlUp).Row
Range("B2").Select
ActiveCell.FormulaR1C1 = _
"=IFERROR(LOOKUP(2,1/('[Data.xlsx]Sheet2'!C1=RC1)/('[Data.xlsx]Sheet2'!C15=R1C),('[Data.xlsx]Sheet2'!C16)),"""")"
Selection.AutoFill Destination:=Range("B2:F2"), Type:=xlFillDefault
Range("B2:F2").Select
Range("B2:F" & lastrow).FillDown
...
Selection.AutoFill Destination:=Range("B2:F2"), Type:=xlFillDefault
Range("B2:F2").Select
'FillDown isn't required for single row as it already has formulas
If lastrow > 2 Then Range("B2:F" & lastrow).FillDown
I have following code
First it will insert two columns one by one
Then count rows in C column
Then Copy ActiveCell.FormulaR1C1 = "=R[-5]C[18]" to B7 to rows count
Similarly, Copy ActiveCell.FormulaR1C1 = "=R[-5]C[13]" to A7 to rows count
But I am getting error when the Active Row is only 01 (One), if it is more than one then it works ok.
I am struggling with this. If anyone can please help.
Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Dim LastRow As Long
LastRow = Range("C" & Rows.Count).End(xlUp).Row
Range("B7").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=R[-5]C[18]"
Range("B7").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("B7:B" & LastRow), Type:=xlFillCopy
Range("B7:B" & LastRow).Select
Range("A7").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=R[-5]C[13]"
Range("A7").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("A7:A" & LastRow), Type:=xlFillCopy
Range("A7:A" & LastRow).Select
To close the question out:
Instead of using AutoFill, maybe do Range("B7:B" & LastRow).Value = Range("B7").Value, and similarly for column A.
Or just in one line for both columns: Range("A7:B" & LastRow).Value = Range("A7:B7").Value.
First off, the Selection object is not necessarily the best option. You can insert the columns with Range("A:B").Insert xlShiftToRight.
Next, you can set the formula for an entire range at once rather than copying and pasting. Since you're putting the formulas only starting at row 7, you'll need to offset and resize the range that is defined by what is in Column C (after the insert).
The Resize method will need to reduce the row count by 6 since the formulas don't start until row 7:.Resize(LastRow - 6). Since the original range is only one column wide, the column count in the Resize method can be omitted (otherwise, it'd be .Resize(LastRow - 6, 1)).
The Offset method will need to shift down 6 rows and left 1 (or 2) columns: .Offset(6,-1) and .Offset(6,-2)).
Your code would then be greatly simplified to:
Dim LastRow As Long
ActiveSheet.Range("A:B").Insert xlShiftToRight
LastRow = Range("C" & Rows.Count).End(xlUp).Row
With Range("C:C").Resize(LastRow-6)
.Offset(6,-1).FormulaR1C1="=R[-5]C[18]"
.Offset(6,-2).FormulaR1C1="=R[-5]C[13]"
End With
Of course, even easier would be to convert your sheet to a Table ("ListObject" in VBA) and let Excel do the heavy lifting. But that's way outside what you asked.
I have a sheet with a range of A12:N112, Column A is my trigger column (1 or ) based on changing criteria). The first bit of my macro which works sorts this range to all the rows with a 1 are at the top of the range. It then opens the destination sheet as well.
The next bit of code below, needs to copy cells B:L for each row with a 1 in column A and paste that into the first empty row in the destination sheet starting at column D. This then generates a number which the then copied and pasted back into the first sheet in column M of that specific row. This then needs to loop until all of the rows with a 1 in column A have been processed.
Can anyone help, here is my code, which runs but nothing is copied or pasted.
Dim lr As Long lr = Sheets("Data Entry").Cells(Rows.Count, "A").End(xlUp).Row
For r = lr To 2 Step 1
If Range("AB" & r).Value = "1" Then
Rows(r).Copy.Range ("A" & lr2 + 1)
Windows("Serialisation Log.xlsx").Activate
Sheets("SNo Log").Select
Range("D" & Rows.Count).End(xlUp).Offset(1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Windows("Serialisation Log.xlsx").Activate
Sheets("SNo Log").Select
Range("A" & Rows.Count).End(xlUp).Offset(-1).Select
Selection.Copy
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
ActiveSheet.Paste
Range("A" & Rows.Count).End(xlUp).Offset(0).Select
Selection.Copy
Windows("Serialisation Generator rev 1.xlsm").Activate
Worksheets("Data Entry").Select
Range("N").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
If Range("AB" & r).Value = "0" Then
Range("I4").Select
ActiveCell.FormulaR1C1 = "Serial No. Issue complete for this OA"
End If
Range("F5").Select
Next r
Any help will be greatly appreciated.
I have a code that copies column of data from one sheet and to another. I would like integrate a do loop so that I don't have to repeat the same commands for each column.
Dim LastRow As Long
Sheets("PWR 2 DATA CAPTURE SHEET Shared").Select
With ActiveSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
'MsgBox LastRow
Dim x As Integer
Sheets("PWR 2 DATA CAPTURE SHEET Shared").Select
Dim Copyrange1 As String
Startrow = 3
Let Copyrange1 = "B" & Startrow & ":" & "B" & LastRow
Range(Copyrange1).Select
Selection.Copy
Sheets("PWR2 data capture sheet ").Select
Range("B3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
I want '1' after copyrange to change until '20', and 'B' to change from C, D, E,F,G,H,I,... Until 'AA' which is corresponds to the columns excel.
Any help will be appreciated. Thanks
I am attempting to pad an XLS cell via VBA & XLS Formula but I am stumbling:
Example: I want to append " - Test" to the contents of Cell AC2 via a formula, then paste that formula to all the remaining rows in Col AC:
lastRow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
strTest = " - Test"
'append
Range("AC2:AC2").Formula = "=AC2.value & value(strTest)"
'then cut and paste
Range("AC2").Select
Selection.Copy
Range("AC2:A" & lastRow).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
I am guessing I need to adjust the syntax of the formula to append. I've tried several variants without success.
Any suggestions?
Note: the following formula works, but only in the first row. Copy & Paste simply copies the VALUE from X2 into subsequent rows, not the formula:
Range("X2:X2").Formula = Range("AC2:AC2").Value & strTest
Range("X2").Select
Selection.Copy
Range("X2:X" & lastRow).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
To append strTest to the cell values, you can use:
With Range("X2:X" & lastRow)
.Value2 = .Worksheet.Evaluate("Index(" & .Address & "&""" & strTest & """,)")
End With
I think instead of this:
Range("AC2:AC2").Formula = "=AC2.value & value(strTest)"
You want this:
Range("AC2:AC2").Formula = "=AC2.value & value(" & strTest & ")"