Pastespecial pasting only the first cell value across entire range - excel

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

Related

Copy or paste a filtered rows to a variable

Good day all
I want to use VBA to avoid the slowness in Excel when moving from one sheet to another. I replace formulas with VBA code. In brief I want to filter a column (A) that has hundreds of words based on the last characters and then use vba code to textjoin them and paste them in one cell. I managed by VBA code to filter them and paste them in another sheet but I want to edit the code to filter the list, textjoin them and paste them to a cell without using formulas. This is the code I used.
Thank you
`
Sub FilteringByLastCharacter()
Dim FLCretera As String
FLCretera = ThisWorkbook.Worksheets("Searching").Range("A2")
Sheets("WordsList").Select
Range("A1").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$B12169").AutoFilter Field:=1, Criteria1:="*" & FLCretera
Columns("A:A").Select
Selection.Copy
Sheets("Searching").Select
Range("S1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("WordsList").Select
ActiveSheet.Range("$A$1:$B$12169").AutoFilter Field:=1
Sheets("Searching").Select
Range("A1").Select
Application.CutCopyMode = False
Range("A2").Select
End Sub`
application.displayalerts=false
application.screenupdating=false
FLCretera = ThisWorkbook.Worksheets("Searching").Range("A2")
Sheets("WordsList").Select
Range("A1").Select
''''clear filters
On error resume next
activesheet.clearallfilters
''''''find the las cells with inf
uf=ActiveSheet.Columns("A").Find("*", _
searchorder:=xlByRows, searchdirection:=xlPrevious).Row
''''create filter
ActiveSheet.Range("$A$1:$B" & uf).AutoFilter Field:=1, Criteria1:="*" & FLCretera
range("A1:B" & uf).copy
'''''
Sheets("Searching").Select
Range("S1").Select
Selection.PasteSpecial Paste:=xlPasteValues
for flag = 1 to uf
'''range("C" & flag) where paste the words for Cells A and B
range("C" & flag)=range("A" & flag)&range("C" & flag)
next flag
Sheets("WordsList").Select
Application.CutCopyMode = False
application.displayalerts=true
application.screenupdating=true
End Sub
sorry, my english is short, but i try to help.
take care

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 ".", "/"

How can i replace VBA manual change to automate(or input box)?

I want to replace !R46C181 to !R46C182, etc. Everytime i launch my macros, since it will take new column(month)
Right now , every month i manually change via find/replace (181 to 182) to move it to the next month before launching my macros.
Is there a way to put some - Input box? Like i where i just place 182, and it will update everything to it
Here is the part of that code
Range("BD31").Select
ActiveCell.FormulaR1C1 = _
"=SUM('[sales.xlsm]Market Share'!R46C181)"
Range("BD32").Select
ActiveCell.FormulaR1C1 = _
"=SUM('[sales.xlsm]Market Share'!R47C181:R49C181)"
Range("BD33").Select
And here is full code:
Sub Auto_ship()
'
' Auto_ship Macro
'
' Keyboard Shortcut: Ctrl+l
'
Range("BD31").Select
ActiveCell.FormulaR1C1 = _
"=SUM('[sales.xlsm]Market Share'!R46C181)"
Range("BD32").Select
ActiveCell.FormulaR1C1 = _
"=SUM('[sales.xlsm]Market Share'!R47C181:R49C181)"
Range("BD33").Select
ActiveCell.FormulaR1C1 = _
"=SUM('[sales.xlsm]Market Share'!R50C181)"
Range("BD34").Select
ActiveCell.FormulaR1C1 = _
"=SUM('[sales.xlsm]Market Share'!R51C181:R52C181)"
Range("BC31").Select
Selection.Copy
Range("BD31:BD35").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("BD31:BD34").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("BG25").Select
Application.CutCopyMode = False
Range("BC30").Select
Selection.AutoFill Destination:=Range("BC30:BD30"), Type:=xlFillDefault
Range("BC30:BD30").Select
Range("BB3:BC3").Select
Selection.AutoFill Destination:=Range("BB3:BD3"), Type:=xlFillDefault
Range("BB3:BD3").Select
Range("BD3").Select
ActiveCell.FormulaR1C1 = "'Sep 2020"
Range("BE3").Select
Columns("BC:BC").EntireColumn.AutoFit
ActiveWindow.SmallScroll Down:=0
End Sub
You can use a
Dim month As String
month =InputBox("My Month")
save it to a string and then concat with & eg
"=SUM('[sales.xlsm]Market Share'!R46C"& month
of cause better ways to do this,error check etc, just a quick one here
The solution below features and InputBox where you can enter a column either by number (like 182) or by its alphabetic ID (like "FZ"). The specified column will then be used to create the formulas your code needs.
Option Explicit
Sub Auto_ship()
' 102
' Keyboard Shortcut: Ctrl+l
' include apostrophes and exclamation point in the string:-
Const Source As String = "'[sales.xlsm]Market Share'!"
Dim C As Variant ' target column
C = InputBox("Enter a column ID (Number or letter):", _
"Target column", "FA")
If Len(C) = 0 Then Exit Sub ' blank to exit
If Not IsNumeric(C) Then C = Columns(C).Column
With Application
.DisplayAlerts = False
.AskToUpdateLinks = False
End With
Cells(31, "BD").Formula = "=SUM(" & Source & RangeName(C, 46) & ")"
Cells(32, "BD").Formula = "=SUM(" & Source & RangeName(C, 47, 49) & ")"
Cells(33, "BD").Formula = "=SUM(" & Source & RangeName(C, 50) & ")"
Cells(34, "BD").Formula = "=SUM(" & Source & RangeName(C, 51, 52) & ")"
Cells(31, "BD").Copy ' copy the formats from BD31 to Bd32:Bd35
Range("BD32:BD35").PasteSpecial Paste:=xlPasteFormats
' if you want the formatsd to be copied to the current column use this line instead:-
' Range(Cells(32, C), Cells(35, C)).PasteSpecial Paste:=xlPasteFormats
' why would you copy the values from BD31:BD34 to that same address ?????
' Range("BD31:BD34").Copy ' copy the values
' Range("BD31:BD34").PasteSpecial Paste:=xlPasteValues
' Range("BG25").Select ' what's the purpose of this serlection?
With Application
.DisplayAlerts = True
.AskToUpdateLinks = True
.CutCopyMode = False
End With
End Sub
Private Function RangeName(ByVal C As Long, _
ByVal Rstart As Long, _
Optional ByVal Rend As Long)
' 102
Dim Rng As Range
Set Rng = Range(Cells(Rstart, C), Cells(IIf(Rend, Rend, Rstart), C))
RangeName = Rng.Address
End Function
The exact range address is created by the function RangeName which takes 2 or 3 arguments: the column, the start row and the end row. The latter can be omitted if you want to specify a single cell. I use A1 notations instead of R1C1. That's a matter of preference in this case.
My code looks radically different from yours because I removing all Select statements. They serve no useful purpose. Excel knows perfectly well where its cells are once you tell it the coordinates.
The second half of your code didn't make sense to me. I couldn't fathom why you would want to copy BD31:BD34 to Bd31:Bd34 every month. I gave one example how you might use the column you enter to address different cells each month directly. For the rest of the code I urge you to continue removing all Select statements and just address each range directly as you instruct what is to be done with it.

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.

EXCEL VBA Formula - Pad a cell

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

Resources