Special paste in Excel with VBA - excel

I'm working on Excel with VBA. I need to copy and paste some information from one row to and specific destination. I'm using this code:
''CUSTOM MESSAGE
Sheets("Extract").Range("AI" & sourceRow & "").Copy Destination :=
Sheets("Print").Range("H" & destRow + 7 & "")
The result of this code is:
But I need something like this:
As you can see, I need to change to the next row, before going out of the table.
Any idea?

Can you wrap the text in that cell? Something like this?
Sheets("Extract").Range("AI" & sourceRow).Copy Sheets("Print").Range("H" & destRow + 7).WrapText = True

The format of the Range() is incorrect.
Try:
Sheets("Extract").Range("AI" & sourceRow).Copy Sheets("Print").Range("H" & destRow + 7)

Related

Worksheet Function Countifs for resizable range

I have a table in which I count the records through Worksheet.Function.Countif.
It is nice because it counts the rows using .Rows.Count and so I am alwasy ensured if my table changes the size.
It looks like that (subset of the code):
endrow = .Cells(.Rows.Count, 20).End(xlUp).Row
ws1.Cells(6, 34).Formula = "=COUNTIF(" & .Range("U6:U" & endrow).Address & ",U6)"
I wish to write the the worksheet.function formula in the same way as above but for 'Countifs'. In excel, I would type it like that:
=COUNTIFS($U$6:$U$144;U6;$T$6:$T$144$;"<>"&T6)
How to write it in vba, using 'endrow' as in the first demonstarted code, i.e. without '144' as the last row but with '& endrow' ?
I was trying multiple times, but I cannot get it to work :/
I will appreciate any help.
Try this:
ws1.Cells(6, 34).Formula = "=COUNTIFS($U$6:$U$" & endrow & ",U6,$T$6:$T$" & endrow & "," & """" & "<>" & """" & "&T6" & ")"
This formula gets the last row of column A:
=IFERROR(LOOKUP(2,1/(NOT(ISBLANK(A:A))),ROW(A:A)),0)

VBA,Combine .Formula with VBA

I have the following vba code which puts a formula in a cell.
.Range("D" & lastrow + 1).Formula = "=INDEX(Spread!$C:$C, MATCH(1,INDEX((A330 = Spread!$A:$A) * (""Stack"" =Spread!$B:$B),),0))"
How can I change A330 in my formula to vba code like Worksheets("Manager").Range("C2").Value
Just concate it like a normal string using the & operator. Since you are not joining text ,but rather a VBA object, do not wrap quotes around the object.
"=INDEX(Spread!$C:$C, MATCH(1,INDEX((" & Worksheets("Manager").Range("C2").Value & "= Spread!$A:$A) * (""Stack"" =Spread!$B:$B),),0))"
You can also store the data in a variable and use it instead of A330. This way you can always use newly created variable inside your code anytime anywhere in your code
myData = Worksheets("Manager").Range("C2").Value
.Range("D" & lastrow + 1).Formula = "=INDEX(Spread!$C:$C, MATCH(1,INDEX((myData = Spread!$A:$A) * (""Stack"" =Spread!$B:$B),),0))"

get the differences between 2 Integer columns into third column

I have to calculate the difference between the values of 2 columns(firstCol and lastCol) and set those differences in a different column(NextColumn). The row count keeps changing, so I have to calculate the rowCount before calculating the difference. I'm trying to write a loop using Range so that the difference can be calculated but it doesn't seem to work.
For i = 3 To lastRow
Range(Cells(3, NextColumn), Cells(lastRow, NextColumn)).FormulaR1C1 = "=Range(Cells(i, firstCol),Cells(i,firstCol)).Value - Range(Cells(i, lastCol),Cells(i,lastCol)).Value"
Next i
Any help would be greatly appreciated!
Thank you
Nick
For any input of "i" into the formula, you need to use " & i & " when using using a formula="", such as:
"=A" & i & "+B" & i
When you change to just a formula that doesn't input a formula to the cell (math happens in VBA), you can ignore the Excel formatting and "" blocking, such as:
= Cells(i,"A").Value + Cells(i,"B").Value
Make sure to use your loop variable where appropriate, so that you would have an outcome in a loop like:
Dim i as Long
For i = 1 to 10
Cells(i,"C").Formula = Cells(i,"A").Value + Cells(i,"B").Value
Next i
Why looping ? Unless I misunderstood the question, something like this should do the trick:
Range("X3:X"& lastrow).Formula = "=C3-D3"
The formula will adjust.
vba needs to be outside the quotes and you do not need the loop:
Range(Cells(3, NextColumn), Cells(lastRow, NextColumn)).Formula = "=" & Cells(3, firstCol).Address(0, 0) & "-" & Cells(3, lastCol).Address(0, 0)
No loop needed. Excel will change the relative references as needed.

adding a dynamic cell reference in vba

I am using the following code to insert a formula into a cell using vba.
The code inserts a hyperlink with some static text leading to a file path and then at the end of my file path I want to be able to add a dynamic cell reference, for instance A and then the number of the row.
In my cell in column A I have the names of folders. I am using DestRow to define the current row number. So my question is how can I correct my formula so that when the link is clicked it opens the link to get the correct folder name of the row clicked? Thanks
ws2.Range("S" & DestRow).Formula = "=HYPERLINK(""\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\ATTACHMENTS\"" & K" & DestRow & ",""Attached"")"
Try,
ws2.Range("S" & DestRow).Formula = "=HYPERLINK(""\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\ATTACHMENTS\" & ws2.Range("K" & DestRow).Value & """,""Attached"")"
FWIW, I hate working with quoted strings as well.
Addendum: This should do for adding a static filename after the dynamic folder:
ws2.Range("S" & DestRow).Formula = "=HYPERLINK(""\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\ATTACHMENTS\" & ws2.Range("K" & DestRow).Value & "\audit.xls"",""Attached"")"
You could try including the INDIRECT() function:
ws2.Range("S" & DestRow).Formula = "=HYPERLINK(""\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\ATTACHMENTS\"" & INDIRECT(""K""&" & DestRow & ",""Attached"")"

reading clipboard contents into array in VBA

Range("C" & CStr(j) & ":C" & CStr(k)).Select
Range("C" & CStr(j) & ":C" & CStr(k)).Copy
I am reading the contents of a column into the clipboard and I want to loop through every element.
The question is how do I loop through it?
The contents of the clipboard look like this:
1234
21345234
1234512345
123452135
123451235
2345
alternatively I should probably be looping through J and K? Can you please show me how to do this thank you
You don't need to use the clipboard for this, instead:
Dim workingArray as Variant
workingArray = Range ("C" & CStr(j) & ":C" & CStr(k))
Now you can work through workingArray, note that it's treated as a two-D array.
You can loop through the range without the need for any weird syntax like this:
Dim cel as Range
For Each cel in Range(Cells(j,3), Cells(k,3))
MsgBox cel.Value
Next cel
Note that the '3' in this case means the range is in the third column (which is 'C')

Resources