PasteSpecial only values - vba

I want to copy paste a range of cells into a destination sheet, but only their values, not the format. Unfortunately, my code pastes the format, too. I am providing the line of code for this purpose. (I know how to paste special values but I want, if it is possible, to apply this method for this method of copy paste. Normally, I use a different copy paste method, for which I know how to paste special, but for this time I wanted to keep the length of the macro as small as possible).
ws1.Range("C2:C" & lastrow2).Copy ws2.Range("A2")

Would recommend:
ws2.Range("A2:A" & lastrow2).Value = ws1.Range("C2:C" & lastrow2).Value

Try a direct value transfer.
with ws1.Range("C2:C" & lastrow2)
ws2.Range("A2").resize(.rows.count, .columns.count) = .value
end with
'alternately,
ws2.Range("A2").resize(ws1.Range("C2:C" & lastrow2).rows.count, ws1.Range("C2:C" & lastrow2).columns.count) = ws1.Range("C2:C" & lastrow2).value

Related

How do I link to another sheet in Excel with VBA?

I'm trying to generate a list of hyperlinks in excel and link them dynamically to another cell in a different sheet. Could someone explain how I need to format the reference? Currently, it looks like this:
'p2r is one sheet
'sh is another
'Subaddress is currently linking to the correct location, wrong sheet.
'(It's linking to [p2r]'s cells, not [sh]'s cells)
p2r.Hyperlinks.Add_
Anchor:=p2r.Cells(p2rIndex, 1), _
Address:="", _
SubAddress:=sh.Cells(round2, 2).Address, _
TextToDisplay:=PrevRow
Thanks for the help! I've seen others use a format like: Sheet!A1, but when I tried something like:
SubAddress:=sh & "!" & Cells(round2, 2).Address
I got no results of value.
There are two modifications you could use to get your code working:
You could use
SubAddress:="'" & Replace(sh.Name, "'", "''") & "'!" & Cells(round2, 2).Address
in order to generate something of the form SubAddress:="'Sheet name'!$A$1".
(The bits in the formula dealing with 's are to allow it to still work if there are spaces, etc, in the sheet name. If you know that won't occur, e.g. because you defined the sheet names and no-one else will be changing them, you could simplify it to SubAddress:=sh.Name & "!" & Cells(round2, 2).Address)
You could use
SubAddress:=sh.Cells(round2, 2).Address(External:=True)
which would generate something of the form SubAddress:="'[Book1.xlsx]Sheet name'!$A$1"

Different solving method for Excel Formula and WorksheetFunction formula?

I have been using an application.worksheetfunction.countifs in one of my codes but it seems to have suddenly stopped working. I typed the code into excel in the same format and it works as it should but the VBA code does not.
=COUNTIFS($D$2:$D$2582, ">0", $E$2:$E$2582, E3, $L$2:$L$2582, L3, $B$2:$B$2582, B3, $T$2:$T$2582, "<=" & T3, $U$2:$U$2582, "<=" & U3)
VBA code used in Macro:
For Each Cell In Range("R2:R" & LastRow)
If Cell.Offset(0, -3) = "" Then
Cell.Value = ""
Else: Cell.Value = Application.WorksheetFunction.CountIfs(Range("D2:D" &
LastRow), ">" & 0, Range("E2:E" & LastRow), Cell.Offset(0, -13),
Range("L2:L" & LastRow), Cell.Offset(0, -6), Range("B2:B" &
LastRow), Cell.Offset(0, -16), Range("T2:T" & LastRow), "<=" &
Cell.Offset(0, 2), Range("U2:U" & LastRow), "<=" & Cell.Offset(0,
3))
End If
Next Cell
I also tried the first search criteria as ">0" but that did not change the output at all.
I am utterly baffled on what I am doing wrong because it was working one moment, then suddenly it only counting some cells according to the formula the next minute.
And yes, I know I did not include the parameter for blank cells in the excel code, but that isn't what is causing the code problems.
Please help.
Try replacing Cell.Value = Application.WorksheetFunction... with Cell.Formula = and building a string representing the desired formula. You've already stated that the formula, when typed in, produces the correct results. Replicating that formula via code will also produce the correct result.
Application.WorksheetFunction only returns the result of the function, and without inspecting your code further, you can't see how this formula differs from the one that works.
Turns out the VBA code was having a hard time reading the amount of decimal places in the search criteria. Therefore, it was having difficulty comparing two infinitely rounded numbers against one another. I am not sure why the Excel formula worked, but the VBA did not, however, a simple Round() code before the cells in my search criteria fixed the problem. Thanks Everyone!

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

VBA Copy Cells + Formatting without using the clipboard

I'm trying to copy cells from one sheet to another without copying stuff to the clipboard but it must copy the formatting across.
Here are the ways I've tried at the moment, any ideas how to accomplish my needs?
LastRow = ThisWorkbook.Sheets("Nas").Range("A65536").End(xlUp).Row
'Option 1, works but it's using the clipboard :/
ThisWorkbook.Sheets("Nas").Range("A" & 6 & ":F" & LastRow).Copy
ThisWorkbook.Sheets("Test").Range("A" & 6 & ":F" & LastRow).Offset(-5, 0).PasteSpecial
'Option 2, works but doesn't take formatting (ie text, general, time, date... etc)
Set Src = ThisWorkbook.Sheets("Nas").Range("A" & 6 & ":F" & LastRow)
Set Dst = ThisWorkbook.Sheets("Test").Range("A" & 6 & ":F" & LastRow).Offset(-5, 0).Resize(Src.Rows.Count, Src.Columns.Count)
Dst.Value = Src.Value
'Option 3, works but doesn't take formatting (ie text, general, time, date... etc)
ThisWorkbook.Sheets("Test").Range("A" & 6 & ":F" & LastRow).Offset(-5, 0) = ThisWorkbook.Sheets("Nas").Range("A" & 6 & ":F" & LastRow).Values
Your example code defines the formatting to be carried across as "text, general, time, date... etc". While the number formatting (a subset of the Properties of a cell) can easily be accommodated, delving further into enumerating the vast number of properties and subproperties of a Cells object is counter-productive when all relevant (non-default) properties can be carried across easily with a copy/paste operation using the clipboard.
With ThisWorkbook.Sheets("Nas")
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
ThisWorkbook.Sheets("Test").Range("A6:F" & LastRow).Offset(-5, 0) = _
.Range("A6:F" & LastRow).Value
ThisWorkbook.Sheets("Test").Range("A6:F" & LastRow).Offset(-5, 0).NumberFormat = _
.Range("A6:F" & LastRow).NumberFormat
End With
Note that transferring the value of the cell across is performed with .Value or .Value2 not .Values.
I have seen this question popup occasionally and IMHO, the source of the question is likely a) a teacher that thinks this is a cutesy way to get students to appreciate just how many properties, subproperties and internal sub-subproperties a Range.Cells object (Range Members (Excel)) contains or b) some idiot with a fresh MBA that wants to prove they are smarter than the IT department. Enumerating through every possible formatting property a cell could contain is just a fool's errand (again IMHO) when the clipboard is available.
If you do attempt this yourself, don't forget Conditional Formatting and Comments, both of which can easily be attributed as part of a cell's formatting.

Summing cells from another sheet using offset

I'm trying to get this code to work for summing cells:
Worksheets("Sheet2").Range("C3").Offset(i, j).Formula = "=Sum("
&Worksheets("Sheet1").Range("A3").Offset(2*i,j).Address & ":" &
Worksheets("Sheet1").Range("A7").Offset(2*i,j).Address & ")"
It keeps giving me the right cells but from the wrong sheet. So for the first iteration I get sum(A3:A7) in cell C3 of Sheet2 but the A3:A7 stays referenced to Sheet2 not Sheet1.
Thanks!
You need to specify the name of the sheet in the formula too. Your code will work if you write it like this:
Worksheets("Sheet2").Range("C3").Offset(i, j).Formula = "=Sum(Sheet1!" & _
Worksheets("Sheet1").Range("A3").Offset(2 * i, j).Address & ":" & _
Worksheets("Sheet1").Range("A7").Offset(2 * i, j).Address & ")"
Try this code - it uses the External:=True parameter of .Addressto retrieve the full address. While this also includes the workbook name, Excel will remove this automatically so you end up with Sheet1!A3. Also note that I used the range A3:A7 as source as .Address can handle multi-cell ranges and you don't need to take care of it manually:
Sheets("Sheet2").Range("C3").Offset(i, j).Formula = "=SUM(" & _
Sheets("Sheet1").Range("A3:A7").Offset(2 * i, j).Address(External:=True) & ")"
Be aware that hard coding references such as A3 can lead to bugs in the long run, as the user (or even the developer at some stage) might modify the sheet structure. It is best practice to use named ranges, i.e. create a named range for each cell/range you refer to and then access it in VBA with SheetX.Range("rngStartCell") or similar!