How to edit this code to only paste the value using VBA? - excel

I have this code in VBA to copy and paste into the next empty cell in column DK, but given that the original cell is a function, the pasted value shows up as 0. Is there any way to paste only the value of the original cell?
Sub CopyToDK()
Range("CN59").Copy Range("DK" & Rows.Count).End(xlUp)
End Sub

If you only want to copy the value, you can do this...
Sub CopyToDK()
Range("DK" & Rows.Count).End(xlUp) = Range("CN59")
End Sub

Range("CN59").Copy
Range("DK" & Rows.Count).End(xlUp).PasteSpecial xlPasteValues

Related

Using VBA to insert a formula using existing cell reference

Okay, I have a basic understanding of how to record macros in VBA.
I have a bunch of cells, each referencing cells on other sheets. (Ex: =R!$A$1). These cells don't match the destination cells (for example, D3 on the primary sheet contains =R!$A$1, and the next entry is F3 referencing =R!$A$4).
Using VBA, I tried replacing the first instance with =IF(R!$A$1="", "TBA", R!$A$1). This recorded and worked for the cell in question, but when I moved to F3 and ran the macro, it replaced the cell with the exact same formula. How do I get VBA to take the existing text in the cell as replacement text, instead of blindly copying the first example?
My first thought was ActiveCell.R1C1 = "=IF( ActiveCell.R1C1 = "", "TBA", ActiveCell.R1C1) but that failed to compile.
Thanks!
Try something like this (if I understand what it is you're trying to do...)
Sub Test()
Dim frm As String
If ActiveCell.HasFormula Then 'formula in cell?
frm = ActiveCell.Formula 'read the existing formula
frm = Right(frm, Len(frm) - 1) 'remove the `=`
ActiveCell.Formula = _
"=IF(" & frm & "="""",""TBA""," & frm & ")" 'set new formula
End If
End Sub

Copying and pasting a cell into the next empty row in a column

I am copying and pasting some data into an excel sheet. I have three different cells that I need copied and pasted into their corresponding three new cells. I want to be able to paste them into the sheet, and run a macro that copies and pastes them automatically into the empty row and column that I specified. I'm going to need to copy and paste them into the next subsequent empty row out of the same column the next time, so I need to update it somehow or have a condition for it to parse down the column to the next empty cell. I have run this code shown and it gives me a "Subscript Out of Range Error" or a "Can't Jump to Sheet" error. I'm not quite sure what to do.
Sub CopyStuff()
Range("A2:C2").Copy
Sheets("Sheet11").Range("A" & Rows.Count).End(xlUp).Offset(1,0).PasteSpecial xlPasteValues
End Sub
Often times easier to use either name or codename depending on situation.
Either
Thisworkbook.sheet1.range("A1").value 'Name
' -or-
Thisworkbook.sheets(1).range("A1").value 'Index
' -or-
Thisworkbook.sheets("Sheet2").range("A1").value 'Codename
Anway... double check whether you're trying to reference sheets(11), sheet11 or sheets("Sheet11")
Sub CopyStuff()
Sheet11.Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Resize(1, 3) = _
ActiveSheet.Range("A2").Resize(1, 3).Value
End Sub

How can I insert a formula into another sheet referring to a cell in the Active Sheet?

I have a sheet called Line Item Summary with cell E7 that has the formula ='Costing Sheet'!I75. Instead of having the formula reference Costing Sheet I would like to replace it with the sheet that is being worked on (so the active sheet) .
The active sheet name may change as copies are made but the cell I75 where there data is referencing will always be the same.
I have tried the following code but I get "Run-time error '9':Subscript out of range".
Sub Connect()
Sheets("Line Item Summary").Range("E7").Formula = "='ActiveSheet'!I75"
End Sub
I tried to explain this as best I can but please let me know if it needs clarification. Thanks!
Remove the activesheet from the quotes otherwise it is treated as a literal string.
Sub Connect()
Sheets("Line Item Summary").Range("E7").Formula = "='" & ActiveSheet.name & "'!I75"
End Sub
But your formula will change - is that what you intend?

Simple VBA code with Range("A1:B3").Sort does not work with cells including formulas

I have a simple macro:
Private Sub Worksheet_Change(ByVal Target As Range)
Range("A1:B3").Sort Key1:=Range("A1:A3"), Order1:=Descending, Header:=x1No
End Sub
However, cells A1 to A3 have formulas (eg. A1 =C1 etc.), and this does not seems to work with my macro. I am not very familiar with coding and have tried to find a simple solution, but is there? I found an example which sets a targetRange and then specified this range to cells with formulas, but could not get it to work in my code.
My simple suggestion would be to copy and pastespecial to a display sheet and sort your data on the display sheet. Or pastespecial to a new column and sort that.
example of pastespecial to another sheet in same workbook:
put the copy and paste on separate lines
wsSource.Range("B" & i & ":" & "C" & i).Copy 'Copy cells Bi and Ci
wsReceiver.Range("A" & j & ":" & "B" & j).PasteSpecial xlPasteValues
wsReciever is a variable that can be replaced with a know sheet name, the same with the letter j in the range.

Trim string starting at comma - Excel

I have an csv sheet with data of Employee as Last Name, First Name.
Below is an example. (These values are in a single cell)
Flynn, Jeremy
Early, Allison
Epstein, David
Newman, Joanna
Biord, Brooke
I need to left trim the data so that I only need the first name without any trailing space or comma.
OutPut Sample should be:
Jeremy
Allison
David
Joanna
Brooke
How can I write a formula or macro that will process the entire sheet of more that 5000 records.
Formula, in an empty column put:
=TRIM(MID(A1,Find(",",A1)+1,LEN(A1)))
Where A1 is the first cell of your list. Then copy down the entire list.
If you want vba this will do it nearly instantly:
Sub firstName()
Dim rng As Range
With ActiveWorkbook.ActiveSheet
'Change the Second Criterion in Each .Cells() to the column number of your data.
Set rng = .Range(.Cells(1, 1), Cells(.Rows.Count, 1).End(xlUp))
'Change the Offset value to the number of columns to offset the answer.
rng.Offset(, 1).Value = .Evaluate("Index(TRIM(MID(" & rng.Address & ",Find("",""," & rng.Address & ")+1,LEN(" & rng.Address & "))),)")
End With
End Sub
It assumes you data is in column A and it will put it in column B
As a formula in an used column to the right using the REPLACE function,
=REPLACE(A1, IFERROR(FIND(",", A1), LEN(A1)), LEN(A1), TEXT(,))
As a VBA sub procedure using Range.Replace method,
Sub firstOnly_bySel()
With Selection
.Replace what:=",*", replacement:=vbNullString, lookat:=xlPart
'selection.replace(
End With
End Sub
Select one or more cells and run the sub from the Macros dialog ([alt]+[F8])
Just do simple find and replace. Find ",*" and Leave Replace blank.

Resources