How to copy row based on cell value to another sheet - vba

I'm trying to copy 2 columns( A and B ) from sheetA to sheetB, based on each different value of column D from sheetA.
I get the week number from "release date" but now i need to create into sheetB 2 columns for each different "week number" and copy the 2 first columns on each week.
I'm getting week numbers using:
If IsDate(c.Value) Then c.Offset(, 1).Value = Format(c.Value, "ww")
But i don't know how to store each different value on "WEEK_NUM" to create the column on sheetB and then copy the row...
That's what i'm trying to obtain:

Related

Loop through column values from one sheet and paste COUNTIF value from another column into another sheet

I have two sheets in an Excel file and need to perform a COUNTIF formula from one sheet and paste the respective information in another sheet. The original sheet just has the type in 1st column with an empty 2nd column. I am trying to loop through the Type from Sheet 1, in each increment loop through the Type from Sheet 2, and past the Count of column 2 from Sheet 2 into Column 2 of sheet 1.
My current VBA code is as follows:
Sub TestOE()
'For loop to go until end of filled cells in 1st column of each sheet
a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
'Loop
For i = 2 To a
For j = 2 To b
If Worksheets("Sheet1").Cells(i, 1).Value = Worksheets("Sheet2").Cells(j, 1).Value Then
Worksheets("Sheet1").Cells(i, 2).Value = Application.WorksheetFunction.CountIf(Range("B:B"), 1)
End If
Next j
Next i
End Sub
This code is only pasting 0's in the desired outcome on Sheet 1.
Sheet to extract information from
Sheet to paste information in
Desired Outcome in destination sheet
You can simply use sumif function to sum the values based on criteria.
here is the formula
=COUNTIF(Sheet1!$A$2:$A$20,Sheet2!A2)
if you want to sum the col B then
=SUMIF(Sheet1!$A$2:$A$20,Sheet2!A2,Sheet1!$B$2:$B$20)
In a few steps, you can accomplish what you want without VBA, and just use a pivot table. Just do as follows.
Select your data set, including the header.
Click on insert tab, then PivotTable. See example for Office 365
Since you want a different worksheet, set PivotTable to be "New Worksheet" See example.
You'll need to drag the TYPE field into the rows, and binary into the values. CountIF is the same as summing binary, so you can leave as sum. See Example
And you'll have an output nearly identical to what you're looking for:

VBA macro or functon to copy cell values based on criteria to a master sheet

Let’s say I have a rental car company and I have 12 sheets with 10 columns each and unknown amount of rows. Each sheet is holding information about cars rented. Below are the column headings for each spreadsheet
- A. Date rented
- B. Customer Name
- C. Customer Address
- D. Customer Phone
- E. Customer email
- F. Car Year
- G. Car Make
- H. Car Model
- I. Car Plate number
- J. Car Vin
I have a master sheet that I want to get specific information from all sheets and copy the cellValues of those sheets into the master sheet. I’m not familiar with VBA so Here is the sudocode of the loop I want to do:
For each sheet
For each row
Copy customer name, customer phone, car plate number into next available row on master sheet
In the master sheet, the columns would be respectively how I put them in the sudocode
- A. Customer Name
- B. Customer Phone
- C. Car Plate number
Can someone show me what the VBA macro code would be for this?
Disclaimer: this is not my actual information in my spreadsheet as what I am working on is confidential so I can’t provide screenshots. This is just example information that simulates what I want to do.
I've tried =HLOOKUP(B1,'Sheet1 (51)'!1:1048576,2:2,FALSE) but getting a value error or an NA error, depending on what range or values I try in the parameters. The way I was understanding the HLookup function is this:
lookup value is the column heading I'm looking for within the source sheet: for customer name I would but B2 for the lookup value
The table array would be the whole source sheet
The row array would be the row I'm getting the cell value from in the source sheet
range lookup is either T or F or nothing as it is optional. If i use True or False, I get the NA error if I use nothing I get the value error.
The idea is that once I get this formula working for one cell then expand it to one row then expand it to the loop i have in my sudocode for all rows within the source sheet, then expand it to look or go to the next source sheet.
You can accomplish this fairly easily by looping through all the available sheets, excluding the master sheet and setting the relevant range for each sheet. Then using the find function to get the last row of data in the master sheet to be able to append the next rows.
This should produce the desired result:
Sub MasterGrab()
Dim master As Worksheet
Dim subSheet As Range
Dim i As Integer
Dim lastRow As Long
Set master = Worksheets("MasterSheet")
x = Sheets.Count
lastRow = master.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row 'find last row on master sheet
lastRow = lastRow + 1
For i = 1 To x
If Not Sheets(i).Name = "MasterSheet" Then 'capture all sheets except MasterSheet
Set subSheet = Sheets(i).Range("A1:J" & ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row) 'set each sheet range to cover required data
For Each r In subSheet.Rows
master.Cells(lastRow, 1) = r.Cells(2) 'Customer name
master.Cells(lastRow, 2) = r.Cells(4) 'Customer phone
master.Cells(lastRow, 3) = r.Cells(9) 'Car Plate number
lastRow = lastRow + 1
Next r
End If
Next i
End Sub

Using CONCATENATE to populate a sum formula in excel vba

I have a spreadsheet that has numbers for a particular year and then subtotals for that year.
The number of instances in a year can vary and there may be a year that doesn't exist i.e., 2018 might be skipped. The title of the totals row is always "FYXX Totals." I have a for loop that goes through the entire column and looks for "FY" Then if it falls within one of three categories (FY1-FY2, FY3-FY7, FY3-FY9; these being variables that represent a year). What I need is for the loop to sum the number in column D, E, F...when "FY" is found. I think using CONCATENATE might be the way to go but I am not sure a) exactly how to do that or b) if that is even the best way to go about it.
Dim rng As Range
Dim SumRow As Integer
Set rng = Range("C4:C" & NextRow)
For Each cell In rng
If Left(cell.Value, 2) = "FY" Then
If 2000 + Int(Mid(cell.Value, 3, 2)) <= FY2 Then 'This is the if statement for the fisrt category
'Here would be the sum function when the if statement is triggered
End If
End If
Next cell
Thanks so much for the help.
SpreadSheet_Picture
The second if loop (right now written for the first category) would need to sum the just the zero that is in column D next to FY17 Total. Keep in mind that sometimes row for FY18 may exist so this macro would have to be able to grab that as well should it exist. But in this case what would have to go into cell "D" & NextRow would be =SUM(D5).
My best attempt at understanding what you're after is that you want to place a formula in each cell of column D where the cell in column C contains FYxx Total, where xx meets certain other criteria. That formula should contain the sum of all the cells in column D for which the cell in the corresponding row of column C contains the same value of FYxx.
The easiest way I can think of to achieve this is to use the SUMIF function:
The formula =SUMIF(B$2:B$999,LEFT(C3,4),D$2:D$999) in cell D3 calculates the sum of all cells in column D where the cell in the corresponding row of column B matches the criterion, i.e. is equal to the first four characters of cell C3.
Unless there are further instances of the same FYxx value elsewhere in column B, the ranges in the first and third arguments of the SUMIF can cover the whole table, which I've assumed here extends to row 999, so you can keep those the same for each cell you place this formula in. You only need to change the row for the cell in column C in the second argument.
In fact if you really want, you can place exactly the same formula in each Total cell in column D:
=SUMIF(B$2:B$999,LEFT(INDEX(C$2:C$999,ROW()-1),4),D$2:D$999)
Here the INDEX function looks up the appropriate cell in column C based on the row of the cell that the function is placed in.

Insert rows into table based on cell value

I am trying to have a macro add in a specific number of rows to a table based on the value of A2.
For example:
If A2=10, then when I run the macro it should insert 10 rows.
also, as a second part to the macro I want it to copy the formula that the previous cells have.
Example:
A3= "='Sheet2'!A1"
and if I add 10 rows then the following rows should be sequential:
A4= "='Sheet2'!A1"
A5= "='Sheet2'!A2"
A6= "='Sheet2'!A3"
A7= "='Sheet2'!A4"
A8= "='Sheet2'!A5"
etc...
I know that a typical drag of the table will copy the cells, I want to ensure that the formula's are copied to the next row.
This will copy and paste the formula as you showed in A3 down the number you enter in A2.
ActiveWorkbook.Sheets(1).Cells.Range("A4:A" & Rows.Count).ClearContents
For i = 1 To ActiveWorkbook.Sheets(1).Cells(2, 1).Value
ActiveWorkbook.Sheets(1).Cells(3 + i, 1).Formula = ActiveWorkbook.Sheets(1).Cells(3, 1).FormulaR1C1
Next i
Is this what you are looking for?

How do I copy specific cells from sheet 1 and paste into corresponding rows of sheet 2 , based on values of cells in sheet 2?

I have Sheet 1 with lots of columns, where column A is the list of all customer codes. In sheet 2 I have column A as some selected customer codes. Now based on the selected customer codes in sheet2 I need to extract few columns (H,I,J) from sheet1, paste it into sheet 2 and export the result to a new sheet.
Excel noob here. Hope you understood my query.
Assuming customer codes are unique in column A (i.e., the same code does not appear multiple times) you can do all of this with VLOOKUP function.
No need for VBA. In column B, Sheet 2: =VLOOKUP(A1,Sheet1!A:J,8,False) will return the value corresponding from column H (H being the eighth column of the range A:J).
Likewise do this for column I:
=VLOOKUP(A1,Sheet1!A:J,9,False)
And if you guessed also do this for column J:
=VLOOKUP(A1,Sheet1!A:J,10,False)