Duplicated cells skip 10 rows - vba

I have an Excel spreadsheet with two sheets, Sheet 1 contains some text and formulas I wish to duplicate down to write 2,000 odd lines of C# code for my project. Extremely repetitive, so I thought I could use Excel to write it for me. Sheet 2 contains an extract from my database which I wish to use to populate that values with. My section of Excel code looks like this and is spread out over 10 rows and 5 columns:
new AccountingPeriod()
{
MonthCovered="=Sheet2!B2",
StartDate=DateTime.Parse("Sheet2!C2"),
EndDate=DateTime.Parse("Sheet2!D2"),
AccountingPeriodDescription="Sheet2!E2",
Active='=Sheet2!F2',
April='=Sheet2!G2,
TaxYear="Sheet2!H2"
},
When I highlight these 10 rows and use the Excel duplicate tool I want the cell references (for example, in my spreadsheet Sheet1!D3 = Sheet2!B2, I want Sheet1!D13 = Sheet2!B3 NOT Sheet2!B13) to increment by 1, not 10 as it's doing in my spreadsheet.
We have tried an alternative solution of writing a macro to insert 10 blank rows in Sheet2 for every populated row so that the duplicated references are correct in Sheet1 but we're currently failing miserably at getting that to work correctly.

Instead of a direct reference that is going to change on a 1-to-1 with the copy, use the INDEX function with a little maths to achieve the 10-to-1 row stagger.
In Sheet1!D3 this references Sheet2!B2.
=INDEX(Sheet2!B:B,INT(ROW(1:1)/10)+2)
In Sheet1!D13 this references Sheet2!B3.
The OFFSET function can accomplish the same thing but it is a volatile function that recalculates whenever anything in the workbook changes. INDEX provides the same functionality while remaining non-volatile and will only recalculate when something that affects its returned value changes.

Related

Excel VBA - Adding keys to duplicated couples

I'm working on a table in Excel with two columns with repeated values (text), and I need to create a new column (same sheet), where each (sorted) couple is associated with an integer.
Here a simplified example:
--> Starting point
--> Expected output
Since the number of rows is really huge (not known a priori - data are imported from external files), I need to write the code in a very efficient way!
All suggestions are warmly welcomed!
Manual method
Manually sort the columns
Insert 1 into cell C1
Insert =IF(AND(A2=A1,B2=B1),C1,C1+1) into cell C2 and copy this cell down.
VBA method (via macro recorder)
Start recording a macro
Manually do all 3 steps from above (manual method)
Stop macro recording
Now you have a basic macro that you can modify to your needs and learn from.

Workaround for either my approach or the 256 character limit in Excel VBA

My problem is as follows, I have a workbook with (to begin with) 2 worksheets, the first (called WIP) acts as a form of data entry, each new occurrence of the BoM requires an insertion of 4 columns which happens to the left of the existing columns. At the same time a new worksheet is created based on a copy of the existing second worksheet (called FitOut) which pulls various bits of a data from the first worksheet based mainly upon the version of the BoM selected and the supplier referenced.
Of course adding new columns to the WIP sheet causes the functions, arrays and formulas in the sheets to automatically update, I had used a quick workaround by using some code to hold and then paste the new occurrences data into the worksheet which is created at the start of the macro, however the formulas have become slightly complex (due to the need to look for the previous 4 occurrences and return values based on specific cell locations) that the 256 character limit has been completely shot ( I think I'm over 800 on some bits).
I've very limited as to the layout of the WIP sheet, and the sheet needs to be fairly idiot proof (hence macros, buttons etc) but it needs to run well...
ANy and all suggestions/help would be much appreciated.
I have put an example of the formula I am trying to use, if it can be condensed further pleas let me know:
=IFERROR(IF($C$1='WIP'!$U$1,(INDEX('WIP'!$A$1:$X$2500,SMALL(IF('WIP'!$U$1:$U$2500=$A$1,ROW('WIP'!$U$1:$U$2500)),ROW(6:6)),COLUMN('WIP'!$C:$C))),(IF($C$1='WIP'!$X$1,(INDEX('WIP'!$A$1:$AA$2500,SMALL(IF('WIP'!$X$1:$X$2500=$A$1,ROW('WIP'!$X$1:$X$2500)),ROW(6:6)),COLUMN('WIP'!$C:$C))),(IF($C$1='WIP'!$AA$1,(INDEX('WIP'!$A$1:$AD$2500,SMALL(IF('WIP'!$AA$1:$AA$2500=$A$1,ROW('WIP'!$AA$1:$AA$2500)),ROW(6:6)),COLUMN('WIP'!$C:$C))),(INDEX('WIP'!$A$1:$AG$2500,SMALL(IF('WIP'!$AD$1:$AD$2500=$A$1,ROW('WIP'!$AD$1:$AD$2500)),ROW(7:7)),COLUMN('WIP'!$C:$C)))))))),"")

Excel - Can not compare two spreadsheets

I have gone through V-look-up guides online but I can not find one that explains what I am looking for. I am trying to avoid manual checking.
What I have:
Two sheets, Sheet 1 and Sheet 2. Both sheets have the same column names (A1:G1) with multiple rows.
Sheet 1 contains my spreadsheet where I update daily. Sheet 2 is the same spreadsheet that is imported from a application (but has hourly updates). Data can change in each row (for some columns) along with additional added/deleted rows. The data is text, dates and numbers (mixture of both too).
I want to run a formula to highlight the changes on sheet 1 (grabbing the updates from sheet 2. Once I find out the formula works correctly, I would like to know how to replace the Sheet 2 updates onto my spreadsheet (Sheet 1).
I am looking for a formula outside of creating a macro (worst case scenario).
Currently I have the following vlook up formula:
=VLOOKUP(A1,sheet2!$A:$A,1,FALSE)
When I run this in another column (lets say in H1 in Sheet 1), it will display "N/A" if that column (A1) in Sheet 2 is not the same. If it is the same, it will write out the column name.
When I use the following formula highlighting all the cells in Sheet 1, I get a values error:
=VLOOKUP(A1:G33,Sheet2!$A:$G,1,FALSE)
How could I apply that formula to the whole spreadsheet (I guess it would apply to both sheets) and have it highlight records in my spreadsheet (Sheet1). Could it also highlight rows that are missing or added?
The data in Column 1 and 2 would never change (they are ticket numbers). Only change that can apply is if ticket is closed, so when I import the updated spreadsheet that row isn't there anymore. If you think there might be a better way to tackle this down, I would like to hear.
Please let me know if I am not clear.
Here are some example screenshots:
Just in case, the formula for the totals are (adjusting the columns for each):
=SUBTOTAL(3,INDEX(C:C,2):INDEX(C:C,ROW()-1))
Sheet 2 is setup very similar. When I import it into excel, the columns are the exact same as Sheet 1, the only difference can be more/less rows (along with the updates for each row).
You can use conditional formatting.

Delete Entire Rows That Have Multiple Matching Cells

I created a macro that will copy over some information from one sheet in my workbook to another to match some criteria so I may import the info into a program. Only problem is after the macro runs, there are some blank rows and a couple duplicates. I have 12 columns of info but I would like to have the macro look at and compare entries in columns D,E,F,G and L with the row above them. So D2,E2,F2,G2 and L2 would be compared to D1,E1,F1,G1 and L1. IF all five of the entries in these cells match that of the previous row, then delete the entire row.
I've found some codes that match one cell or looks for duplicates in a certain column but nothing to look and match multiple columns and I'm so new to this that I'm having trouble even getting started.
Any and all input is welcome.
You're going to have to put in the logic of your program yourself but use something like:
worksheets("Sheet1").range("A1").offset(i, 0).resize(1, colnum).delete Shift:=xlUp
An easy way to find the commands you need is to record a macro and see what Excel uses to build that macro.

Combine Columns in multiple Excel workbooks and auto-de-dupe

I have three workbooks with IDs in column A. I want to create a fourth workbook, which should combine the IDs and de-dupe them automatically so that I can perform a vlookup on them to reference data on the other workbooks. The 3 workbooks with data in them will be constantly updated with new ID numbers added, so I need the master/summary workbook to automatically grab newly added ID numbers and perform vlookups against the other workbooks.
The goal of this is to give a summary view of each record (which corresponds to a person), letting the user know which workbooks that person exists in.
I have tried doing =max() to retreive the number of ID's in each workbook, and combining them, telling me the total # of ID's that exist, combined. Then I tried to perform this: =SMALL(IF(FREQUENCY(Test1:Test2$A$2:$A$1000, ROW($1:$28))<>0, ROW($1:$28), ""), ROW(A1))
+ CTRL + SHIFT + ENTER
But I'm 1. not sure if that'll work and 2. not sure how the syntax works with 3 separate workbooks.
I also tried the union method in VBA with no luck - again I think I'm messing up the syntax.
You can retrieve a unique list of the id numbers in [ID_First.xlsx]Sheet1!$A$2:$A$999 using the following array formula in the master worksheet's A2 (needs a row above it to avoid circular references).
=IFERROR(INDEX([ID_First.xlsx]Sheet1!$A$2:$A$999,MATCH(0, IF(LEN([ID_First.xlsx]Sheet1!$A$2:$A$999),COUNTIF(A$1:A1,[ID_First.xlsx]Sheet1!$A$2:$A$999),1),0)),"")
If you stack similar formulas consecutively, passing calculation on to them with IFERROR(), you can gain a unique list from three separate workbooks.
=IF(LEN(A1),IFERROR(INDEX([ID_First.xlsx]Sheet1!$A$2:$A$999,MATCH(0, IF(LEN([ID_First.xlsx]Sheet1!$A$2:$A$999),COUNTIF(A$1:A1,[ID_First.xlsx]Sheet1!$A$2:$A$999),1),0)),IFERROR(INDEX([ID_Second.xlsx]Sheet1!$A$2:$A$999,MATCH(0, IF(LEN([ID_Second.xlsx]Sheet1!$A$2:$A$999),COUNTIF(A$1:A1,[ID_Second.xlsx]Sheet1!$A$2:$A$999),1),0)),IFERROR(INDEX([ID_Third.xlsx]Sheet1!$A$2:$A$999,MATCH(0, IF(LEN([ID_Third.xlsx]Sheet1!$A$2:$A$999),COUNTIF(A$1:A1,[ID_Third.xlsx]Sheet1!$A$2:$A$999),1),0)),""))),"")
Array formulas require Ctrl+Shift+Enter to finalize. Once entered correctly, fill down as necessary to collect all unique IDs.
With a unique list of id numbers, you can use the same method of nested IFERROR functions to look through a series of three workbooks for additional data.
=IFERROR(VLOOKUP($A2, [ID_First.xlsx]Sheet1!$A$2:$Z$999, 2, FALSE),IFERROR(VLOOKUP($A2, [ID_Second.xlsx]Sheet1!$A$2:$Z$999, 2, FALSE),IFERROR(VLOOKUP($A2, [ID_Third.xlsx]Sheet1!$A$2:$Z$999, 2, FALSE),"")))
I'm offering this as you've mentioned a total of 50 member IDs. This method can quickly (and logrythmically) eat up calculation resources when applied to larger groups of numbers.
If you've got Excel 2016 or later, you could unpivot the data using PowerQuery, which is now built in to Excel under the Get & Transform section of the Data tab in the ribbon. Plenty of examples of how to do this if you search Google for 'unpivot' and 'Powerquery'.
If you have Excel 2010 or 2013, you can download the PowerQuery addin for free from https://www.microsoft.com/en-nz/download/details.aspx?id=39379 (assuming IT let you do so).
PowerQuery is a revolution in Excel data transformation, and the learning curve is a lot less steep than advanced formulas or VBA.