I have two sheets:
Sheet1 contains many rows with a lot of data in them. Each row has a unique number in column A.
All these data from the rows in sheet1 is copied to sheet2 with a simple =sheet1!$A$1 ect.
Sometimes/a lot of times I need more than one copy of the row (exact copy) in sheet2 and at the moment I am manually inserting a new line in sheet2 and I am copying the above row to the newly created row.
Now, it would be very nice and very time saving for me, if I in sheet1 could insert a new column, in which I could define how many copies that would have to be created in sheet2. It would also be nice, if I could change the number of copies afterwards (in sheet1), if I by mistake had punched in the wrong number there, and then excel automaticly would delete the number of copies that where to much, starting by the last row created.
Any ideas on how this could be made?
Code I have tried:
Sub copy()
Set i = Sheets("Sheet1")
Set e = Sheets("Sheet2")
Dim d
Dim j
d = 1
j = 2
Do Until IsEmpty(i.Range("Q" & j))
If i.Range("Q" & j) = "TERM" Then
d = d + 1
e.Rows(d).Value = i.Rows(j).Value
End If
j = j + 1
Loop
End Sub
This wouldn't be very efficient, but the way you could do that would be to have TWO extra columns in Sheet1.
As you stated, column B would be the number of repetitions you'd want the value from column A to be.
You'd want column C to be a cumulative count. To do this, you'd have:
C1: =B1
C2: =C1+B2
Drag C2 down
Now, in sheet 2, you could put in the following formula AS AN ARRAY FORMULA
A1: =INDEX(Sheet1!A:A,MATCH(TRUE,Sheet1!C:C>=ROW(),0))
(To make it an array formula, once you type it in, finish with ctrl + shift + enter.)
Drag, Sheet 2, A1 down for a lot of rows and that will update as you change the values in column B of sheet 1 (the number of times to repeat the value in Column A)
Hope this does the trick!
Related
I'm trying to search on the specific column(E), and if matched with the first 4 digit, I would like to copy the number to a different column.
Column E is where i would like to paste all the random number(dynamic)
Column A/B/C is static where i would add 4 digits from time to time.
Column I/J/K is where is would like to paste the result.
PS:
I'm doing it manually and would really appreciate if someone can help me out with the automation hence no code is provided. :(
Having ExcelO365 means you may use FILTER(). Therefor try the below:
Formula in I2:
=FILTER($E:$E,ISNUMBER(MATCH(--LEFT($E:$E,4),A:A,0)))
Drag right to K2. Now, this is dynamic and will change accordingly upon data entry in column E:E, or changing values in A:C.
this is the code to execute on sheet 1, it goes through the entire column E and validates through the formula of counting if in each of the first three columns and assigns the value found in the corresponding columns.
Sub macro()
Dim Static_Data As String
Dim Sht As Worksheet
Set Sht = ThisWorkbook.Sheets("Hoja1")
Active_row = 2
Do While Sht.Range("E" & Active_row).Value <> ""
Static_Data = Sht.Range("E" & Active_row).Value
For i = 1 To 3
If Application.WorksheetFunction.CountIf(Sht.Columns(i), Mid(Static_Data, 1, 4)) > 0 Then
Sht.Cells(Sht.Cells(Rows.Count, i + 8).End(xlUp).Row + 1, i + 8).Value = Static_Data
End If
Next i
Active_row = Active_row + 1
Loop
End Sub
For Excel versions that don't support FILTER or as an alternative you can use standard formulas for this.
If you use columns F-H as helper columns (and these columns can be hidden) then the formula in F2 will be:
=IF(NOT(ISERROR(VLOOKUP(VALUE(LEFT($E2,4)),A$2:A$100,1,FALSE)))=TRUE,$E2,"")
The formula can then be copied across and down. This will find your matches.
In order to then remove the blanks from the data you can use the following formula in I2 and again copy across and down. Depending on how many numbers you want to add in, you may want to extend the range A$2:A$100 in the top formula and F$2:F$100 in the bottom formula
=IFERROR(INDEX(F$2:F$100,AGGREGATE(15,6,(ROW(F$2:F$100)-ROW(F$2)+1)/(F$2:F$100<>""),ROWS(I$2:I2))),"")
I am new to VBA Excel and wondering if you can help.
I have tried typing something similar to this =IF(C10,(ROW(A10)-ROW(A$9)),"") and it works. However, the downside to this is you have to enter this formula into every cell that you want to auto populate. I am trying to find a macro code in Excel so that cells will auto number 1,2,3,etc. whenever the adjacent column contains data?
For example, when a user enters data into B1, A1 will automatically be populated to 1. Then when users enters data into B2, A2 will automatically be populated to 2 and so on. Then when users delete data from B column, adjacent column in A will not contain a number.
Enter this into A1 based on what you said:
=if(isblank(B1),"",row())
If you want this in VBA you can use the following, but this assumes your data starts at Row 1. If it you want the numbering at Row 2 to start as 1, just add "-1" after the RngA part.
Sub Serial()
Dim RngA As Long, LastRow as Long 'Declares the variables used
LastRow = ActiveSheet.Cells(Cells.Rows.Count, "B").End(xlUp).Row 'This finds the last row in Column B where the loop will end.
For RngA = 1 To LastRow ' This loops from the first row to the last cell filled in column B
If Cells(RngA, 2) <> "" Then ' If Column B is blank, skip this row
Cells(RngA, 1).Value = RngA 'Column B was not blank, so put the row number in column A
End If
Next
End Sub
I have a column of headers in Sheet(2), Column A through Row 108. Columns B through J have data related to those headers. I need to copy Column A with Column B into Sheet(3) "X" times based on an integer in a cell in Sheet(1) into Columns B and C of Sheet(3). This needs to be repeated for each Column B-J making sure to take the header Column A, and paste it "X" times based on an integer in a cell in Sheet(1) into Column B and C of Sheet(3). I need the copy and paste to begin to repeat in Sheet(3) in the row following the last paste of data.
An additional requirement is for each time the two columns of data [meaning Column A and one of the Columns B-J of Sheet(2)] is pasted into Sheet(3), a week of the year is tagged in Column A of Sheet(3) for each row of the data pasted based on a start date in Sheet(1). The week of the year should continue to move away from the start date by one week with every paste of Sheet(2) data into Sheet(3).
Also if the VBA is run again it needs to override the data pasted not continue down the column.
I think I understand your problem. This code takes the number of times to copy/iterate from cell A1 of Sheet1, then it copies data from Sheet2 (Cell B2 to the end of the data set) and pastes it multiple times into Sheet 3:
'assumes parameter is in cell A1
num_iterations = ThisWorkbook.Worksheets("Sheet1").Range("A1")
data_last_row = Worksheets("Sheet2").Cells.Find(What:="*", SearchDirection:=xlPrevious).Row
'Copy records from data tab, assuming data starts in B2
Worksheets("Sheet2").Range("B2:B" & data_last_row).Copy
For i = 1 To num_iterations
output_last_row = Worksheets("Sheet3").Cells.Find(What:="*", SearchDirection:=xlPrevious).Row
Worksheets("Sheet3").Cells(output_last_row + 1, 1).PasteSpecial
Next i
I have two excel sheets both with matching product numbers. What I need to do is match the product numbers and then copy a column from the first sheet to the second.
My example:
Column C in the first sheet contains product numbers
Column A in the second sheet contains product numbers
I want to match C & A and then copy column B from the first to the second sheet.
Sorry if this has been answered before, my knowledge is basic but I am trying to learn
Thanks for any help.
CD
Assuming your numbers to match start in C2 (headers in row 1) and numbers to copy in AC2 on Sheet1, this should do what you want. Paste it into AC2 on Sheet2 and then drag copy to the length of column A on Sheet2.
=IF(IFNA(MATCH(A2,Sheet1!C:C,0), FALSE), INDIRECT("Sheet1!AC"&MATCH(A2, Sheet1!C:C, 0)), "Not Found")
Note this will give "Not Found" for a value on Sheet2 which is not found on Sheet1, you can change that by just replacing the string "Not Found" in the formula with whatever you want (e.g. 0).
Formula:
You can use OFFSET with IF. In B1 of sheet 2 for example you could put, drag down for as many rows as required,
=IF(A1=Sheet1!C1,OFFSET(Sheet1!A1,0,COLUMNS(Sheet1!A:AC)-1,1,1))
If you set your data up as a table in sheet 2 (Ctrl + T with an populated cell selected) then add this formula to the appropriate column it will autofilter down the formula.
Or:
With code in a standard module:
Public Sub AddFormula()
Dim rng As Range
With Worksheets("Sheet2")
If Application.WorksheetFunction.Subtotal(103, .Columns(1).Cells) > 0 Then
For Each rng In Intersect(.Columns(1), .UsedRange)
If Not IsEmpty(rng) And Not IsError(rng) Then 'assume ignore empty cells
If rng = Worksheets("Sheet1").Cells(rng.Row, "C") Then Worksheets("Sheet1").Cells(rng.Row, "AC").Copy rng.Offset(, 1) 'determine where you want value to go
End If
Next rng
End If
End With
End Sub
Note:
This is based on your comment that you are comparing sheet2 col A with sheet1 col C and copying sheet1 col AC if match.
I assume that the product numbers in both sheets are unique. If that, I suggest use the offset and match formulas to achieve what you want.
sorry for my half-baked answer due to i just typed them with phone and it is not convenient to input complicated style, here is the additional information:
the match formula is used to query the location of the target product number in the original sheet( sheet 1 in your case above);
after we located the position of the target product number, extract the information you wanted through the offset formula.
here are the specific process:
Sheet 1 represent the original information and sheet 2 represent the target one. What we required to do is copy the info in col D of sheet 1 to col D of sheet through the product:
OFFSET($D$5,MATCH(I6,$D$5:$D$16,0)-1,1,1,1)
OFFSET($D$5,MATCH(I7,$D$5:$D$16,0)-1,1,1,1)
OFFSET($D$5,MATCH(I8,$D$5:$D$16,0)-1,1,1,1)
OFFSET($D$5,MATCH(I9,$D$5:$D$16,0)-1,1,1,1)
OFFSET($D$5,MATCH(I10,$D$5:$D$16,0)-1,1,1,1)
or you can refer to the output directly:
enter image description here
for more information about these two formulas, you can refer to the help of excel of google.
I'm very new to Excel VB scripting and i'm looking for information on how to copy specific cell data to a new sheet with a table and add a new line for each time you press the button to copy. Let me try to explain better
I have 1 sheet named Values and one named Data
Excel Version 2013
Value Sheet Information
in Cells A2, H17, H19, H21, H23, H25, H27, H29, H31, H33, H35 and H37
Data Sheet
Tablename: SurveyData
Table Header names
AgeRange, B1, C1, D1,E1, F1, and so on all on the same row
H17 = B1
H19 = C1
And so on
When you click on the button it should copy all these values from these cells to a new table row in the Data Sheet
I don't know how possible this is to do within Excel but i'm hoping there will be some options on how to perform this within Excel.
Thank you for taking your time and hope this explanation of mine makes sense.
Edit: I did a huge error when expressing my request, The data in the entry sheet is spread out across multiple rows and cells. I want to take these individual cells to be pasted into a new sheet on a new row. so it will look something like this
When you click the button it copies all data from Sheet 1 to Sheet 2 each click creates a new row with data filled in on each cell that was copied from sheet 1 under their corresponding cell.
AgeRange B C D E F G
15-20 1 1 0 1 0 1
20-25 1 0 1 1 1 0
And so on.
I got the copy down but getting each individual cell to be pasted into one row in a new column and when done go to a new line for new data is the issue.
I do not know if i make any sense here
Regards
Johan
Sub CopySheet1PasteSheet2 ()
count=Worksheets("Sheet1").Cells(Worksheets("Sheet2").Rows.Count, "A").End(xlUp).Row
Worksheets("Sheet2").Cells(count+1,1) =Worksheets("Sheet1").Cells(1,2) 'for A2
Worksheets("Sheet2").Cells(count+1,2) = Worksheets("Sheet1").Cells(2,5) 'for B5
End Sub
In order to add next row you need to count rows in Data Sheet:
Dim count as integer;
count=Worksheets("Data").Cells(Worksheets("Data").Rows.Count, "A").End(xlUp).Row
To copy values from worksheets from A2 to V1
Worksheets("Data").Cells(count+1,1) = Worksheets("Values").Cells(1,2)
And then next cells accordingly