Copy and paste area including merged cells - excel

Hi I have a block of cells I need to copy and paste to a second sheet. Some of the cells in the block are merged. I have formatted the destination so the cells are also merged where they will receive merged cells.
Is their code to copy and paste as a block, the standard
Worksheets("9B").Range("C" & a & ":P " & b).Copy Worksheets("Undo").Range("C" & a & ":P" & b)
will not work with merged cells and I was hoping to avoid having to do each part individually as it is a reasonable size.
Thanks

If you have prepared cells in second sheet then do:
Sub CopyMerged()
a = 1
b = 10
'First method
Worksheets("9B").Range("C" & a & ":P " & b).Copy
Worksheets("Undo").Range("C" & a & ":P" & b).PasteSpecial
'Second method
Worksheets("Undo").Range("C" & a & ":P" & b).Value = Worksheets("9B").Range("C" & a & ":P " & b).Value
End Sub

Related

Excel 2016 Array formula

I have the following formula in excel (created by me in the past),
=IFERROR(INDEX(Tasks!$H$2:$H$65536;SMALL(IF(A2=Tasks!$A$2:$A$65536;ROW(Tasks!$A$2:$A$65536)-ROW(Tasks!$A$2)+1);ROW($1:$1)));"")
The formula is working as I wanted, but in each and every month the a columns are changing, so for example what was Column "A" here can be Column "C" next month and so the number of rows are changing. I'm trying to modify the formula that I don't have to adjust month by month.
For this first I found a VBA script online:
Function Col_Letter(lngCol As Long) As String
Dim vArr
vArr = Split(Cells(1, lngCol).Address(True, False), "$")
Col_Letter = vArr(0)
End Function
So after if I type for example:
=Col_Letter(COLUMN(Table8[[#Headers];[ID]])) it will give me Letter "B" as this header can be found in "B" Column.
So using the advantage of this VBA script I was trying to create a formula what will adjust the column letter in the original formula every time.
So this formula:
=("Tasks!" & "$" & Col_Letter(COLUMN(Table32[[#Headers];[Owned By]])) & "$" & "2" & ":" & "$" & Col_Letter(COLUMN(Table32[[#Headers];[Owned By]])) & "$" & ROW(INDEX(Table32;1;1))+ROWS(Table32)-1) is giving me this as the result Tasks!$H$2:$H$65536.
what is part of the formula so I think it's good.
This is the whole formula I was trying to re-create in this manner:
=Index((("Tasks!" & "$" & Col_Letter(COLUMN(Table32[[#Headers];[Owned By]])) & "$" & ROW() & ":" & "$" & Col_Letter(COLUMN(Table32[[#Headers];[Owned By]])) & "$" & ROW(INDEX(Table32;1;1))+ROWS(Table32)-1);SMALL(IF((((Col_Letter(COLUMN(Table8[[#Headers];[ID]])) & ROW()) & "=" & (("Tasks!" & "$" & Col_Letter(COLUMN(Table32[[#Headers];[ID]])) & "$" & "2"& ":" & "$" & Col_Letter(COLUMN(Table32[[#Headers];[ID]])) & "$" & ROW(INDEX(Table32;1;1))+ROWS(Table32)-1))));Row((("Tasks!" & "$" & Col_Letter(COLUMN(Table32[[#Headers];[ID]])) & "$" & "2"& ":" & "$" & Col_Letter(COLUMN(Table32[[#Headers];[ID]])) & "$" & ROW(INDEX(Table32;1;1))+ROWS(Table32)-1)))-ROW(("Tasks!" & "$" & Col_Letter(COLUMN(Table32[[#Headers];[ID]])) & "$" & "2"))+1);ROWS($1:$1))))
Excel Tables have the potential of resolving such questions easily. Since the Columns can be referred by their names, you do not have to worry where they are located. And likewise, since you reference the data by column name but not row number, your reference will be covering all of the rows within that Table, no matter how many rows are added or deleted.
So as a start, try converting your range to Table by selecting it and use Insert / Table. After this you should convert your formula to contain Table references rather than cell references.

Incorporate Error-Checking Formula When Copying Excel Vba

I currently have the following code for copying cells:
Set Feeder = Sheets("Projects").Range("B" & Rows.Count).End(xlUp)
With Sheets("Database")
Set Storage = .Range("C" & .Rows.Count).End(xlUp).Offset(-Masterrow + 1)
Storage.Value2 = "=" & "Projects!" & Feeder.Address
End With
Is there a way to incorporate the formula =IFERROR(B2,0) so that my copy location contains =IFERROR(Projects!B2,0) as opposed to =Projects!B2?
I want erroneous cells to return a 0 as opposed to an error code so I can just run my delete rows code easily.
The fix was straightforward after realizing I had already constructed a formula before.
Code from before:
Storage.Value2 = "=" & "Projects!" & Feeder.Address
Code after:
Storage.Value2 = "=" & "IFERROR(" & "Projects!" & Feeder.Address & ",0)"
Sometimes it really is simple!

Translate a worksheet formula in VBA

Can somebody help me to write this formula in excel VBA?
=IF(ISERROR(VLOOKUP(A3,Temp!$A$3:$A$595,1,FALSE)),A3,"0")
My code is getting stuck with :"syntax error"
Sub checkDuplitems()
Application.ScreenUpdating = False
Const top As Integer = 3
Dim bottom As Long
bottom = Sheets("Temp").Cells(Rows.Count, top).End(xlUp).row
With ThisWorkbook.Sheets("trash").Range("A" & top & ":A" & bottom)
.Formula = "=IF(ISERROR(VLOOKUP(A" & top & ",Temp!$B$" & top & ":$B$" & bottom & _
",1,FALSE)),A" & top & ", & '" 0" & ," '")"
.Value = .Value
.SortSpecial
End With
'Call something...
End Sub
You have a concatenation problem in the second line of the .Formula line.
To emulate the formula you have at the top of your question (which is wrong incidentally because you should be pointing to $B$3:$B$595 or something like that because your look up cell A3 should not be inside the VLOOKUP range).
Try this new .Formula line:-
.Formula = "=IF(ISERROR(VLOOKUP(A" & top & ",Temp!$B$" & top & ":$B$" & bottom & _
",1,FALSE)),A" & top & ", " & "0)"
Are you sure you want to use top as both the starting row in column A and the column to get the bottom row from the Temp worksheet? The important column on the Temp worksheet is column B (i.e. 2) not C (i.e. 3).
If you are putting formula(s) into Trash!A3:A595 that reference Trash!A3:A595 then these are circular references and cannot be resolved under normal conditions. I'll put the formulas into column Z.
If you are operating with Excel 2007 or newer then I would humbly propose this alternate that uses the worksheet's IFERROR function and does not attempt to make text out of the 0 returned value.
Const top As Integer = 3
Dim bottom As Long
bottom = Sheets("Temp").Cells(Rows.Count, "B").End(xlUp).Row '<~~change here
With ThisWorkbook.Sheets("trash")
With .Range("Z" & top, .Cells(Rows.Count, "A").End(xlUp).Offset(0, 25))
.Formula = "=IFERROR(VLOOKUP(A" & top & ", Temp!$B$" & top & ":$B$" & bottom & _
", 1, FALSE), 0)" '<~~ big change here
.Value = .Value
End With
End With
It is also curious as to why the number of rows of formulas in the Trash worksheet must be governed by the number of rows of data in the Temp worksheet. I would have thought that the number of values in column A of the Trash sheet should govern how many formulas go into the Trash worksheet.

Assigning cell value from another workbook to my code

Basically what I want to achieve is assigning the cell value from the other workbook into my code.
Customers send in a spreadsheet of goods they want to order and this spreadsheet is used for more than one purpose so it has many columns which are not needed for my purpose.
So I created the macro to look up and give me the value from certain columns and spit out a CSV file.
I then created a spreadsheet of my own with the column values all mapped out, I am trying to get the coding to lookup the cell value in my spreadsheet and knows which columns to be looking up on the customer spreadsheet.
I want it so I can just go in and change the values in the cells on
my spreadsheet instead of having to go into the coding and change the
columns value in my code.
ShipToSiteID = Application.WorksheetFunction.Trim(Range("Q" & counter))
AltShipTo1 = Application.WorksheetFunction.Trim(Range("G" & counter))
AltShipTo2 = Application.WorksheetFunction.Trim(Range("H" & counter))
AltShipToCity = Application.WorksheetFunction.Trim(Range("I" & counter))
'AltShipToState = Application.WorksheetFunction.Trim(Range("I" & counter))
AltShipToZip = Application.WorksheetFunction.Trim(Range("J" & counter))
AltShipToCountry = "UNITED KINGDOM"
RefNumber = counter - 1
UserId = LCase(Environ("username"))
ShipToSiteID = Replace(ShipToSiteID + AltShipToZip, " ", "")
Sheets(2).Select
Range("A" & counter - 1) = LineType & comma & RefNumber & comma & QName & comma & BlanketID _
& comma & BillToSiteID & comma & ShipToSiteID & comma & ContractID _
& comma & PONumber & comma & CaseID & comma & ShipVia & comma & RequiredDate _
& comma & Comments & comma & Priority & comma & TerminalID & comma _
& AltContactFirst & comma & AltContactLast & comma & AltPhone & comma _
& AltShipTo1 & comma & AltShipTo2 & comma & AltShipToCity & comma _
& AltShipToState & comma & AltShipToZip & comma & AltShipToCountry _
& comma & UserId & comma
A few suggestions...
To reference a value in workbook2 on workbook1, the easiest way to do this would be to copy the worksheet from workbook2 to workbook1. Then you can reference the value by typing "=" in the cell, and clicking on the cell that you want to reference.
Referencing in VBA code gives you some more flexibility. I'd suggest checking this good SO question and this one for more info. This may be more along the lines of what you want.
Unfortunately, if you reference a value from workbook2 on workbook1, there is no easy way to change the value of workbook2 from workbook1.

Find cell content and copy row onto new sheet

I want to paste a list in the "InsertList" sheet. This list will only contain the word "Correct" or "False". From then on i need a way to search for the word "Correct" or "False" in the columnS P,Q,R,S,T,U,V.
e.g. If in the column "P" on the "InsertList" sheet the word "Correct" is found, i need that entire row from A to V to be copied onto it's destination, in this case "sheet1".
If the word "Correct" is found on the column "Q" on the "InsertList" sheet, the rows from A to V need to be copied in the Sheet2. And so on..
For i = 2 to Thisworkbook.Sheets(“ÏnsertList”).Range(“A64000”).End(xlup).row
If Thisworkbook.Sheets(“ÏnsertList”).Range(“P” & i).Value = “Correct” Then
Thisworkbook.Sheets(ÏnsertList”).Range(“A” & i & ":V" & i).Copy Thisworkbook.Sheets(“Sheet1”).Range(“A” & i)
ElseIf Thisworkbook.Sheets(“ÏnsertList”).Range(“Q” & i).Value = “Correct” Then
Thisworkbook.Sheets(ÏnsertList”).Range(“A” & i & ":V" & i).Copy
Thisworkbook.Sheets(“Sheet2”).Range(“A” & i)
End If
Next
Try using the above code in one of the modules

Resources