repost: copy pasting data instead of formula excel VBA - vba

ok i have to repost this as my original thread was tagged as a duplicate with "the original" thread not having anything to do with my issue and the person who tagged it as a dupe cannot provide the answer.
there's a reason why i'm using activecell in my code as it does something with interaction with outlook and validation of certain conditions from other cells.
ActiveCell.Cells(1, 37).Copy Destination:=wsTest.Range("A" & Rows.Count).End(xlUp).Offset(1)
now i tried :
ActiveCell.Cells(1, 37).Copy Destination:=wsTest.Range("A" & Rows.Count).End(xlUp).Offset(1).pastespecial
but i'm getting error 1004.
if someone will point me here: how to paste only values in excel
please tell me which of the 4 answers will actually work, because none of them is working on my end.

Reverse the procedure and pass over the value directly.
wsTest.Range("A" & Rows.Count).End(xlUp).Offset(1) = ActiveCell.Cells(1, 37).value
The .Value is the raw underlying value in a cell or the result from a formula if the cell contains a formula. .Value2 strips the value further where a date like 18-Feb-2015 becomes 42,053. If you have used number formatting in the cell (e.g. 18-Feb-2015) and wish to retain it, use .Text in place of .Value.

Related

Dynamic Array Formula using VBA

the problem I am trying to figure out is a bit complicated and detailed but I will try to simplify it with an example.
I am looking to use vba to change the array formula with a conditional statement. An example of what the code would look like is below
If (InStr(1, Range("E" & i).Value, "X")) > 0 Then
With outputSheet
Range("I" & i).FormulaArray = Sumproduct(('Sheet1'!$A$57:$A$104=$B2)*('Sheet1'!$E$56:$AZ$56=$H2),'Sheet1'!$E$57:$AZ$104)
End With
Else [Same code as above for Array formula]
End If
Initially I was using the following code to copy the Array formula down however using that I cannot use a conditional statement where the formula is dynamic according to cells.
Range("G2").AutoFill .Range("G2:G" & OutputLastRow)
If however, I can somehow make the $B2 into B&i then that would do the trick for me.
On just a highlevel, the idea was that the output cell would look at some conditions in the same row and different columns and change array formula according to the conditions. It would look at it for each cell and move down until it hits the last row
Any suggestions on how this can be tackled. I hope the problem is clear. Thanks so much!!

Runtime Error 1004 Copy Formula Array

I have the following array formula in cell B2 in my Excel spreadsheet:
{=IF(COUNT(IF(ISNUMBER(A30:A1000);IF(B30:B1000>A30:A1000-1;A30:A1000)))>=COUNT(IF(ISNUMBER(A30:A1000);COUNT(B30:B1000>A30:A1000-1;A30:A1000)));COUNT(IF(ISNUMBER(A30:A1000);COUNT(B30:B1000>A30:A1000-1;A30:A1000))))}
Now I want to use the following VBA code to copy this code into cell A2:
Sheets("Sheet1").Range("A2").FormulaArray = Sheets("Sheet1").Range("B2").Formula
However, when I use this code I get runtime error 1004.
Do you have any idea how to solve this issue?
Your array formula is too long to pass along like that as the Range.FormulaArray property.
You don't need to keep repeating all of the conditions. As you cycle through rows 30 to 1000, if the first or second condition fails, the remainder of the formula for that cycle is not processed. IFs in a formula stop processing at the first FALSE.
=IF(COUNT(IF(ISNUMBER(A30:A1000), IF(B30:B1000>A30:A1000-1, A30:A1000)))>=COUNT(B30:B1000>A30:A1000-1,A30:A1000),COUNT(B30:B1000>A30:A1000-1,A30:A1000))
Now the code works just fine.
With Worksheets("Sheet3")
.Range("A2").FormulaArray = .Range("b2").Formula
End With
Note that I could not test this using semi-colons as the system list separator; only with my own system's commas. VBA does not like anything by EN-US regional settings in a .Formula, .FormulaR1C1 or .FormulaArray property. If you still have trouble, use debug,print to see how the .Formula is being returned. If it contains semi-colons, then use,
With Worksheets("Sheet3")
.Range("A2").FormulaArray = Replace(.Range("b2").Formula, Chr(59), Chr(44))
End With

Using Excel Macro to truncate in specified cells but still allow data entry in the same cell

I am completely new at working with Excel Macro but have encountered a spreadsheet issue at work that I believe may be solved by using a Macro formula. I need to truncate a value (cannot be rounded) to the second decimal place in specified cells. However, the values to be truncated must be entered by a third party using a template spreadsheet that the Macro formula will be attached to. Is there a way to lock a formula to certain cells while allowing data entry in those cells that would be updated by the formula? I have used the Macro below to successfully truncate cell values and now just need a way to force that Macro to run every time new data is input into the specified cells.
Sub TruncateSelection()
ActiveSheet.Range("A1:A15").Select
Dim Cell As Range
With Selection.Cells
.Value = Evaluate("IF(ROW(1:" & Selection.Cells.Count & "),TRUNC(" & .Address & ",2))")
End With
End Sub
You will need to bind your script to the Worksheet_Change event in VBA
This MSDN article should shed some light on what you'll need to do.
https://msdn.microsoft.com/en-us/library/office/ff839775.aspx

Excel 2010 VBA: Add characters before Activecell.Copy pastes data to destination sheet

This is my 2nd thread in 2 days, trying to learn excel vba on my own.
Now i have a working code that pulls data from an active cell and paste it on to a another sheet. it works. but what i want to do is before it places the value on the destination sheet i want to add some characters. so basically instead of group of numbers (eg 12345) what i want the final value to show is similar to this (12345-A, 55455-B,) basically i want to add string suffixes to the final data.
any thoughts?
ActiveCell.Cells(1, 1).Copy Destination:=Sheets("test").Range("A" & Rows.Count).End(xlUp).Offset(1)
It is not clear what you realy intend to copy there, the values, the formats, the formulae... Not knowing this but only based on what's given:
Your code:
ActiveCell.Cells(1, 1).Copy Destination:=Sheets("test").Range("A" & Rows.Count).End(xlUp).Offset(1)
follow with something like this (one of the lines in the With block might be what you are looking for...):
With Sheets("test").Range("A" & Rows.Count).End(xlUp).Offset(1)
.Value = "12345-A" & .Value ' this line
.Value = .Value & "-A" ' or this line, or whatever
End with
The whole thing is ugly but should work...

Add a cell formula in Excel via vba

I’m not an Excel or VBA expert but I want to insert this current excel formula into cell’s using VBA.
Current Excel formula:
=IF(OR(ISNUM(D570)=FALSE;ISNUM(D573)=FALSE);"";IF(C573="Total";D573-D570;""))
VBA formula :
ActiveSheet.Range("a" & ActiveSheet.Rows.Count).End(xlUp).Offset(2, 12).Value = "=IF(OR(ISNUM(R[-3]C[-9])=FALSE;ISNUM(R[0]C[-9])=FALSE);'';IF(R[0]C[-10]='Total';R[0]C[-9]-R[-3]C[-9];''))"
It doesn’t work… Someone can help me please?
Try using .formula = instead of .value = in your VBA code.
Setting the .value of a cell simply copies in whatever value you specify. In this case, your formula is simply converted to a string value.
Using the .formula property, you are actually specifying the formula that gets used to compute the value, which is what you are looking for.
Can I first suggest a simplification of your formula, from:
=IF(OR(ISNUM(D570)=FALSE;ISNUM(D573)=FALSE);"";IF(C573="Total";D573-D570;""))
...to...
=IF(AND(C573="Total"; ISNUM(D570); ISNUM(D573)); D573-D570; "")
Then, I'd set a cell (the active cell in the example below) to use that formula using the VBA code:
ActiveCell.Formula = "=IF(...)"