Is there a way to automatically copy and insert a line in an Excel sheet if the user selects a specific value from a drop down list?
Suppose the drop down list is column 2 (product) and takes values "Apple" & "Pear". Each time a user selects Apple, the lines should be duplicated. For instance
Name
Product
Price
A
Apple
1
B
Pear
1
C
Apple
1
If the user selects Apple, the end result should be
Name
Product
Price
A
Apple
1
A
Apple
1
B
Pear
1
C
Apple
1
C
Apple
1
If so, could anyone tell me how to do it ?
I have written this code so far but no new lines are being created
Sub Addline()
Dim Rng As Range
Dim LR as Integer
LR = Sheets("Customer").Range("A7").EndXlDown.Row
For i = 7 to LR
Set Rng = Sheets("Customer").Range("E" & i)
If Rng = "Apple" Then
Rng.Offset(Rowoffset).EntireRow.Insert Shift:= xlShiftDown
End If
Next
End sub
Then in worksheet_change()
Call Addline
If i modify line in the sheet, it ends up adding several lines instead of only one.
Here is my problem, I have those data on my Sheet1
A B C
1 Name Account Amount
2 John HSBC -20000
3 Ashley JPM 140000
4 Rose BAML 70000
5 John DB 10000
6 Rose Barclays -25000
7 Ashley JPM -3000
My goal is to use vba in order to sort lines and group them by name and amount. The result that I'm looking for in Sheet2 is :
A B C
1 Name Account Amount
2 John HSBC -20000
3 John DB 10000
4
5 Ashley JPM -3000
6 Ashley JPM 140000
7
8 Rose Barclays -25000
9 Rose BAML 70000
I already succeeded in taking the negative values first, but I still have difficulties in taking the rest and leave a blank line between groups.
Here is the code that I started :
Option Explicit
Sub sort_account()
Dim list_amount As Range, amount As Range
Dim b As Integer
Worksheets("Sheet1").Activate
Set list_amount = Worksheets("Sheet1").Range("C2", Range("C2").End(xlDown))
For Each amount In list_amount
If amount.Value < 0 Then
b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet2").Cells(b + 1, 1).Value = amount.Offset(0, -2)
Worksheets("Sheet2").Cells(b + 1, 2).Value = amount.Offset(0, -1)
Worksheets("Sheet2").Cells(b + 1, 3).Value = amount
End If
Next amount
Worksheets("Sheet2").Activate
End Sub
And this gives me as a result :
A B C
1 Name Account Amount
2 John HSBC -20000
5 Ashley JPM -3000
8 Rose Barclays -25000
I really have difficulties for the rest. Do you have any ideas ? I thank you in advance for that.
Nb : The order doesn't matter, I just want that the first values of each group is the negative amount.
Thanks!
If this were my project, I would do it in three distinct steps: Copy all the data from sheet 1 to sheet 2; Sort the data in the preferred order; Insert the blank rows, starting from the bottom.
The following code should do what you’re after.
Option Explicit
Sub sort_account()
Dim LastRow As Long, i As Long
Sheet2.Cells.ClearContents
Sheet1.UsedRange.Copy Sheet2.Range("A1")
Sheet2.Columns("A:C").Sort Key1:=Sheet2.Range("A2"), order1:=xlAscending, _
Key2:=Sheet2.Range("C2"), order2:=xlAscending, Header:=xlYes
LastRow = Sheet2.Cells(Rows.Count, "A").End(xlUp).Row
For i = LastRow To 4 Step -1
If Sheet2.Cells(i, "A") = Sheet2.Cells(i - 1, 1) Then
Sheet2.Cells(i - 1, "A").EntireRow.Insert
End If
Next i
End Sub
If your version of Excel supports Dynamic Arrays then you can do the following
This solution adds an extra row for each unique name, sorts the new data, and then blanks out the added rows.
E2 is =CHOOSE({1,2,3},UNIQUE(A2:A7),"temp",99999999). This creates an array with all the unique names in the first column, "temp" in the second column and 99999999 in the third (to be used for sorting).
F6 is =ROWS(A2:C7) the count of the rows in the data
F7 is =ROWS(E2#) the count of unique names
A10 is =IF(SEQUENCE(F6+F7)<=F6,A2:C7,INDEX(E2#,SEQUENCE(F6+F7)-F6,SEQUENCE(1,3))). This appends the original data to the new data.
E10 is =SORT(A10#,{1,3}), the appended data sorted.
I10 is =IF(F10:F18="temp","",E10#). This blanks out all the temps rows.
If your version of Excel supports LET you can do this in one cell.
=LET(data,A2:C7,
tempRows,CHOOSE({1,2,3},UNIQUE(A2:A7),"temp",99999999),
totalRows,ROWS(data),
uniqueNames,ROWS(tempRows),
outRows, SEQUENCE(totalRows+uniqueNames),
unsorted,IF(outRows<=totalRows,data,INDEX(tempRows,outRows-totalRows,{1,2,3})),
sorted,SORT(unsorted,{1,3}),
result, IF(INDEX(sorted,outRows,2)="temp","",INDEX(sorted,outRows,{1,2,3})),
result)
I need help to write macro to compare two sheets in excel, sheet1 and sheet2 on same excel book and display all the differences between both the sheets on sheet3.
My headings will always be the same between sheet1 and sheet2 but the information in both the sheets may vary.
I included the headings that will be on both the sheets:
ID Number Date of Birth Payroll Number Surname First Name Salary Member Group
I am struggling with this. So please extend your helping hands
Thank you.
Assuming 'i' is the number of rows in Sheet1 and 'm' is the number of rows in Sheet2 and 7 headings in each sheets, then this is the solution where the mismatches are registered in Sheet3
Here the Id numbers in Sheet1 is compared with Id numbers in Sheet2 and if present, then they are compared and the mismatches are reported in sheet3 in the same order of the Headings
Sub Mismatch()
Dim temp3 As Integer
temp3 = 1
Dim array1(7), array2(7), array3(7) As Variant
For i = 2 To 6
Worksheets("Sheet1").Activate
For temp = 1 To 7
array1(temp) = Cells(i, temp).Value
Next temp
Worksheets("Sheet2").Activate
For m = 2 To 6
If Cells(m, 1).Value = array1(1) Then
For n = 1 To 7
For temp2 = 1 To 7
array2(temp2) = Cells(m, temp2).Value
Next temp2
Worksheets("Sheet3").Activate
temp3 = temp3 + 1
Cells(temp3, 1).Value = array1(1)
For temp4 = 2 To 7
If array1(temp4) <> array2(temp4) Then
Cells(temp3, temp4).Value = "mismatch"
End If
Next temp4
GoTo JumpToHere
Next n
End If
Next m
JumpToHere:
Next i
End Sub
I Hope this helps
Count unique values of a column where 2 other columns meet certain criteria. Assume column A is a list of Claim Numbers that repeat because each claim has multiple bills in multiple months (month is column B and location is column C), each row represents a bill. I need to be able to count the unique claim numbers with the criteria of month=3 and location="Chicago"
-Claim # Month Location
-1234 3 Chicago
-1234 3 Chicago
-1234 3 Chicago
-1234 3 Chicago
-3215 3 Chicago
-3215 3 Chicago
-3215 3 Chicago
-1334 4 Chicago
-1334 5 Chicago
-1235 3 Philadelphia
The answer here should be 2
Give this small macro a try:
Sub dural()
Dim c As Collection
Set c = New Collection
Dim N As Long, i As Long
N = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To N
If Cells(i, 2).Value = 3 And Cells(i, 3).Value = "Chicago" Then
On Error Resume Next
c.Add Cells(i, 1), CStr(Cells(i, 1))
End If
Next
MsgBox c.Count
End Sub
I have data in a sheet which looks like
A B C D
1001 1002 1003
Phone 1 1 1
TV 1 1
Remote 1
AC 1 1 1
I want a macro to which gives Data in another sheet something like
Phone 1001;1002;1003
TV 1001;1003
Remote 1003
AC 1001;1002;1003
in 2 columns
This is Sample data, the columns and rows vary every time to large numbers say up to 1000.
So I need a macro to get the data from first row, only if corresponding cell has "1" in it.
This might help ..
Sub Init()
Dim x, y As Integer
Dim s As String
Dim xt As Worksheet
Set xt = Sheets("Sheet2")'----------> you may change this
For y = 2 To 6 '--------------------tv,remote,etc
s = ""
For x = 2 To 4 '------------------1001,1002,1003
If Cells(y, x) = 1 Then
If Len(s) > 0 Then s = s & "; "
s = s & Cells(1, x)
End If
Next
xt.Cells(y, 1) = Cells(y, 1)
xt.Cells(y, 2) = s
Next
End Sub