Excel Fill blank rows with values from above - excel

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

Related

Adding line in case value is in list

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.

Find duplicates of characters

I don't know how to create a macro to find duplicates of characters.
This picture shows the expected result.
This picture is my actual result.
This is my attempted code:
Sub atrod_dublikatus()
' atrod_dublikatus Macro
Dim a, b, skaits As Integer
skaits = 0
For a = 1 To 11
For b = a + 1 To 11
If Cells(a, 1).Value = Cells(b, 1) Then
skait = skait + 1
Cells(a, 2).Value = skait
Cells(b, 2).Value = skait
End If
Next b
Next a
End Sub
You don't need VBA for this, you can do it with a formula and an expanding range (Note the position of the $ sign in only one part of the range)
=IF(COUNTIF(A$1:A1,A2)>0,VLOOKUP(A2,A:B,2,0),IF(COUNTIF(A:A,A2)>1,MAX(B$1:B1)+1,""))
Put a heading in A1 (You need this for the formula to work)
Put the formula in B2 then drag down, see below for my test data
Heading
Hello 1
World 2
Blah 3
Off
On
Blah 3
Blah 3
World 2
Blah 3
Hello 1

Excel VBA code for categories the data

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.

loop through all values from col A that are not not in col B

I need a VBA code to loop through only those rows in range1(AA:AB) whose value in cell AA is not found in Col A.
eg
A
1
2
4
AA AB
1 Jon
3 Bob
4 Frank
5 Hank
In this example I need to loop through rows containing Bob and Hank only (2 iterations)
Sub cycle()
On Error GoTo Unmatched
Set used_range = ActiveSheet.UsedRange
For Each Line In used_range.Rows
Key = Cells(Line.Row, 2).Value
If (Key <> "") Then
x = Application.WorksheetFunction.VLookup(Key, used_range, 1, False)
End If
Next Line
Exit Sub
Unmatched:
MsgBox (Cells(Line.Row, 2).Value & " " & Cells(Line.Row, 3).Value)
Resume Next
End Sub
Some notes - I used columns B and C (index 2 and 3) for my test - substitute the appropriate column index numbers for your spreadsheet
Put the thin gs you need to do in the code after Unmatched:

how to transform three columns to a matrix using macro

I need some help converting three colums into a matrix using excel macro.
Here is an example:
From this:
A A 0
A B 23
A C 3
B A 7
B B 56
B C 33
C A 31
C B 6
C C 5
to this:
A B C
A 0 23 3
B 7 56 33
C 31 6 5
Hope you can help me.
Thanks
Not quite sure what exactly you are meaning by matrix. For the code below I assumed you were looking for a way to read the data in the first two columns as Row and Column data of the output table. Assume the input data is in the Columns 1 - 3 of "Sheet1"
Sub ConvertTableOfData()
Dim testArray(1 to 3)
Dim chkROW as Integer
Dim chkCOL as Integer
Dim chkVAL as Integer
'// index the Row and Column headers
testArray(1) = "A"
testArray(2) = "B"
testArray(3) = "C"
'// Iterate through every row in the initial dataset
For i = 1 to Worksheets("Sheet1").Cells(1, 1).End(xlDown).Row
With Worksheets("Sheet1")
'// Assign the Output Row and Column values
'// based on the array indices
For j = 1 to UBound(testArray, 1)
If .Cells(i, 1) = testArray(j) Then
chkROW = j
End If
If .Cells(i, 2) = testArray(j) Then
chkCOL = j
End If
Next j
'// store the actual value
chkVAL = .Cells(i, 3)
End With
'// output table (in Sheet2)
With Worksheets("Sheet2")
.Cells(chkROW, chkCOL) = chkVAL
End With
Next i
'// Add headers to Output table
For i = 1 to 3
With Worksheets("Sheet2")
.Cells(i + 1, 1) = testArray(i)
.Cells(i, i + 1) = testArray(i)
End With
Next i
End Sub
You can also perform this without VBA.
Assume your table of data is in the range A1:C9.
Assume the first number (0) in the 3 by 3 grid of data is cell F3, with A, B, C in the row above, and A, B, C in the column to the left.
Enter the formula in cell F3 as
=INDEX($C$1:$C$9,SUMPRODUCT(--($A$1:$A$9=$E3),--($B$1:$B$9=F$2),ROW($A$1:$A$9)))
Copy this formula to all 9 cells in the 3 by 3 grid.
This generalized to any size of data.

Resources