I am trying to match up data in three adjacent columns with data in a fourth column.
This is how my data is currently organized:
ROW A B C
----------------------
1 Cat Car Red
2 Dog Bike Blue
3 Bird Car
4 Bear Car Blue
5 Fish Bike Red
6 Cow Car Red
(the values in column A are unique; the values in B and C are repeated values)
And this is how I need it to be organized:
ROW A B C
----------------------
1 Cat Car Red
2 Dog Bike Blue
3
4 Bird Car Blue
5 Bear Car Red
6 Fish Bike Red
Basically, I need columns A and B to see that column C is empty in row 3, and to insert blank cells in A3 and B3 so that each A and B value below that is moved down by one.
The spreadsheet has many of these needed insertions. Some of them will be individual insertions like the example above, but others might be 3, 4, 5, 10 insertions in a row.
Is there a formula or some kind of automated process I can use to make this happen?
Thanks!
From the example you posted, you want to push A and B down when C is blank?
Sub PushDown()
Dim X As Long
For X = 1 To Range("A" & Rows.Count).End(xlUp).Row
If IsEmpty(Range("C" & X)) Then Range("A" & X & ":B" & X).Insert xlDown
Next
End Sub
Sub clearContent()
Dim lastrow As Integer
lastrow = Range("D65536").End(xlUp).Row
For i = 2 To lastrow
If Cells(i, 4) = "" Then
Range(Cells(i, 2), Cells(i, 3)).ClearContents
End If
Next i
End Sub
Related
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)
This question is a different version of another question I deleted because not correct. Sorry for those who answered the preceding question.
I have an Excel spreadsheet which contains more than 6000 rows filled with values. As an example, here are the first 4 rows of the spreadsheet:
A B C D
1 1,1 1,11 7
2 1,2 1,22 6
3 1,3 1,33 8
4 1,4 1,44 2
What I need to do is to look for each of the value in column D (7, 6, 8, 2, etc.) in column A, and when one of this value is found I want Excel to replace the corresponding value in column B, with the value in column C that is in the same row of the value in column D that has just been found. So, the output I want to obtain looks like the following:
A B C D
1 1,1 1,11 7
2 1,44 1,22 6
3 1,3 1,33 8
4 1,4 1,44 2
As you can see, in this case only the value present in B2 has been replaced with the one present in C2, and this because the only value that is present both in column A and in column D is "2".
Formula in E1
=IF(COUNTIF($D$1:D4,A1)>0,VLOOKUP(A1,$A$1:C4,3,FALSE),B1)
and copy down
New formula for E2 as per your question edit.
=IFERROR(INDEX($C$2:$C$5,MATCH(A2,$D$2:$D$5,0)),B2)
You could try the below macro :
Sub Macro1()
Dim i As Integer
Dim currentValue As Integer
i = 1
Range("D1").Select
LR = Selection.End(xlDown).Row
For i = 1 To LR
currentValue = Cells(i, 4).Value
Range("A1:A" & LR).Select
Set c = Selection.Find(currentValue, LookIn:=xlValues)
If Not c Is Nothing Then
Cells(c.Row, 2).Value = Cells(c.Row, 3).Value
End If
Next i
End Sub
Looking to fill blank values in a row with the 'top' value, similar to the functionality provided by Editing>Fill>Top. The difference being that the Fill function requires you to go row-by-row rather than applying itself to a larger dataset. Example below:
Apple 1 foo
Banana 1 foo
2 foo
bar
Cherry 2 bar
3 foo
6 bar
Grape 1 foo
Would end up as the following:
Apple 1 foo
Banana 1 foo
Banana* 2 foo
Banana* 2* bar
Cherry 2 bar
Cherry* 3 foo
Cherry* 6 bar
Grape 1 foo //new values represented with *
Note that the third entry in the second column (2) was also brought down implying this could be applied to multiple columns.
The value in this would be converting a table that has the relationships Cherry>2, Cherry>3, Cherry>6 represented in the first table into a format that could be used as an associative table.
Try this short macro:
Sub FillInTheBlanks()
Dim i As Long, N As Long, j As Long
N = Cells(Rows.Count, "C").End(xlUp).Row
For i = 2 To N
For j = 1 To 2
If Cells(i, j).Value = "" Then Cells(i, j).Value = Cells(i, j).Offset(-1, 0).Value
Next j
Next i
End Sub
I thought id add my variation on this one. Works when you just want to select a range
Sub BlankCellAll()
For Each c In Worksheets("Sheet1").Range("A3:R14").Cells
If c.Value = "" Then c.FormulaR1C1 = "=R[-1]C"
c.Value = c.Value
Next
End Sub
I have an Excel file that contains some data in column B, now i wish to categories the data in A column like serial number first 1 to 5 again starts from 1 to 5 until the data ends,
for example in below format
1 A
2 B
3 C
4 D
5 E
1 F
2 G
3 H
4 I
5 J
1 K
2 L
3 M
4 N
5 O
I do not have existing code for above task please help me.
you can use the following
put 1 in the Cell A1
put =IF(OFFSET(A2,-1,0)=5,0,OFFSET(A2,-1,0))+1 in cell A2
double click in the bottom corner of cell A2, this will repeat the function for all cells in column A
hope that it will help you
Use some code
Sub DoItGood()
Dim rws As Long, rng As Range, t As Range
Columns(1).ClearContents
rws = Cells(Rows.Count, "B").End(xlUp).Row
Set rng = Range("A1:A" & rws)
x = 1
For Each t In Range("A1:A5")
t = t + x
x = x + 1
Next t
Range("A1:A5").AutoFill Destination:=rng, Type:=xlFillCopy
End Sub
You can get a repeated list of numbers from 1 to n downwards in rows with the following approach:
=MOD((ROW(A1)-1),n)+1
Take the integer remainder of the division row number (starting with 0) and n. You will get 0,1,2,...,n-1,0,1,2,...,n-1,0,1... To this add 1.
In your case n is 5:
=MOD((ROW(A1)-1),5)+1
filled downwards.