EXCEL VBA Formula - Pad a cell - excel

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 & ")"

Related

Excel is interpreting date wrong

I have a problem. I have data in this format:
13.3.19 00:23:01
I use a macro to import it to one tab, copy it to another tab, and replace the "." with "/" so it in the correct format. But excel said no and interprets most of the data as text, which is not a problem, I just use datevalue on that. But when it comes to this date in particular:
12.3.19 23:52:41
Excel is interpreting it as a date in the US format and instead of leaving it as march the 12th, it makes December the 3rd out of it. This renders the datevalue useless in just a part of my data set.
Any thoughts?
Code of the macro here:
Sub import_data()
Path = Worksheets("Macro").Cells(6, 4).Value
Analysis = ThisWorkbook.Name
Rfrom = Sheets("Macro").Cells(8, 4)
Rto = Sheets("Macro").Cells(9, 4)
Application.DisplayAlerts = False
For Data_Range = Rfrom To Rto
Fname = Sheets("Macro").Cells(Data_Range, 3)
Segment_name = Sheets("Macro").Cells(Data_Range, 4)
'selecting workbook
Workbooks.Open Filename:= _
Path & "\" & Fname _
Sheets(Segment_name).Select
Range("A2:W14000").Select
Selection.Copy
Windows(Analysis).Activate
Sheets("Raw_data_import").Select
Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(Fname).Activate
ActiveWindow.Close
Windows(Analysis).Activate
Next Data_Range
Windows(ThisWorkbook.Name).Activate
Sheets("Raw_data_import").Activate
Range("E:G").Select
Selection.Copy
Sheets("Priprava_dat").Select
Range("A:C").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Priprava_dat").Range("A:B").Replace ".", "/"
Dim lastRow As Long
lastRow = Range("C" & Rows.Count).End(xlUp).Row
Range("D2").AutoFill Destination:=Range("D2:D" & lastRow)
Range("E2").AutoFill Destination:=Range("E2:E" & lastRow)
Range("F2").AutoFill Destination:=Range("F2:F" & lastRow)
Range("G2").AutoFill Destination:=Range("G2:G" & lastRow)
Range("H2").AutoFill Destination:=Range("H2:H" & lastRow)
Range("I2").AutoFill Destination:=Range("I2:I" & lastRow)
Range("J2").AutoFill Destination:=Range("J2:J" & lastRow)
Windows(ThisWorkbook.Name).Activate
Sheets("Macro").Activate
End Sub
you can set the number format of the cells using
Sheets("Priprava_dat").Range("A:B").NumberFormat = "dd/mm/yy hh:mm:ss"
just before changing the '.' to '/' using
Sheets("Priprava_dat").Range("A:B").Replace ".", "/"

FillDown Method

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

Pastespecial pasting only the first cell value across entire range

I'm trying to paste the formula results from one column to the next. I need to perform this through a macro without using a loop as it slows down the tool due to large number of records. The following methods are not working -
a) range("G5:G10").value = range("H5:H10").value
b) Selection.PasteSpecial paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
The issue is that excel copies the formula result of the first cell ("G5") to all the cells at the destination ("H5:H10"). The formula in the source is an array formula. Could anyone please share how to resolve the issue.
Edit :
Sub paste()
'
' paste Macro
'
'
Dim ColName As String, sheetname1 As String, lookup_col As String, lastrow_range As Long
ColName = "A"
sheetname1 = "Sheet1"
lookup_col = "C"
lastrow_range = 8
Worksheets("Sheet1").Range("F5").Select
ActiveCell.FormulaArray = _
"=IF(ISERROR(MATCH(TRUE,EXACT($" & ColName & ActiveCell.Row & "," & sheetname1 & "!$" & lookup_col & "$5:$" & lookup_col & "$" & lastrow_range & "),0)) , ""-" & ColName & """&"" is Invalid. This should match with any one of the values in column "" &""" & lookup_col & """&"" of "" & """ & sheetname1 & """&"". Please refer ""&""" & sheet_name & """ ,""It Matches"")"
Worksheets("Sheet1").Range("F5").Select
Selection.AutoFill Destination:=Range("F5:F8")
Range("F5:F8").Select
Selection.Copy
Range("G5").Select
Selection.PasteSpecial paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
End Sub
Copy and paste special values of range
Range("H5:H10").Copy
Range("G5:G10").PasteSpecial (xlPasteValues)
Application.CutCopyMode = False
Copy and paste range
Range("H5:H10").Copy
Range("G5:G10").PasteSpecial (xlPasteAll)
Application.CutCopyMode = False
Or copy and paste the entire column
Range("H:H").Copy
Range("G:G").PasteSpecial (xlPasteAll)
Application.CutCopyMode = False
Thanks for the input.
I found that the calculation method had to be changed to 'Automatic' from 'Manual' :
Application.Calculation = xlAutomatic

cut copy paste macro across 2 sheets with dynamic range

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.

Retrofit VBA code so it can loop through columns

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.)

Resources