Need Help in VBA !!

simplyeshu

New Member
Joined
Jun 25, 2011
Messages
30
Hello Frens,

I am new to VBA programming. So can you please help me.

I have a excel of 4 columns and rows can be anything from 100 to 500.

I want to create a small vba code which should insert a record when cell a2=a1 is false and then copy a2 to b2 column.

Ex:

Columns: a b c d
a1 Excel Sheet
a2 Hero Bestbuy Amazon Lifestyle
a3 Hero Bestbuy Dallas Shopping
a4 Heroine Outright Sydney Sports
a5 Villain Direct Los angels Lifestyle

I want a new row to be created after a1 as a2=a1 is false and Hero word to be copied in b2 after creating a new row. Similarly a new row after a3 and a4 as the condition is false

Can anyone help in providing me VBA code for the same?
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Try this with a copy of your sheet

Code:
Sub InsRowVal()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = LR To 2 Step -1
    If Range("A" & i).Value <> Range("A" & i - 1).Value Then Rows(i).Insert
Next i
LR = Range("A" & Rows.Count).End(xlUp).Row
With Range("B1:B" & LR)
    .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C[-1]"
    .Value = .Value
End With
End Sub
 
Upvote 0
Thank you very much but there is a small difference in output:

Present output is

<table border="0" cellpadding="0" cellspacing="0" width="256"><col style="width: 48pt;" width="64" span="4"> <tbody><tr style="height: 15pt;" height="20"> <td class="xl63" style="height: 15pt; width: 48pt;" width="64" height="20">Books</td> <td class="xl63" style="width: 48pt;" width="64">books1</td> <td class="xl63" style="width: 48pt;" width="64">books2</td> <td class="xl63" style="width: 48pt;" width="64">books3</td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl63" style="height: 15pt;" height="20">
</td> <td class="xl63">Books</td> <td class="xl63">
</td> <td class="xl63">
</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Hero</td> <td>Bestbuy</td> <td>amazon</td> <td>lifestyle</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Hero</td> <td>Bestbuy</td> <td>amazon1</td> <td>lifestyle1</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Hero</td> <td>Bestbuy</td> <td>amazon2</td> <td>lifestyle2</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">
</td> <td>Hero</td> <td>
</td> <td>
</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Rambo</td> <td>Sale</td> <td>Discount</td> <td>Shopping</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Rambo</td> <td>Sale</td> <td>Discount1</td> <td>Shopping1</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Rambo</td> <td>Sale</td> <td>Discount2</td> <td>Shopping2</td> </tr> </tbody></table>
Where as the output should be:

<table border="0" cellpadding="0" cellspacing="0" width="256"><col style="width: 48pt;" width="64" span="4"> <tbody><tr style="height: 15pt;" height="20"> <td class="xl65" style="height: 15pt; width: 48pt;" width="64" height="20">Books</td> <td class="xl65" style="width: 48pt;" width="64">books1</td> <td class="xl65" style="width: 48pt;" width="64">books2</td> <td class="xl65" style="width: 48pt;" width="64">books3</td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl65" style="height: 15pt;" height="20">
</td> <td class="xl65">Hero</td> <td class="xl65">
</td> <td class="xl65">
</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Hero</td> <td>Bestbuy</td> <td>amazon</td> <td>lifestyle</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Hero</td> <td>Bestbuy</td> <td>amazon1</td> <td>lifestyle1</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Hero</td> <td>Bestbuy</td> <td>amazon2</td> <td>lifestyle2</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">
</td> <td>Rambo</td> <td>
</td> <td>
</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Rambo
</td> <td>Sale</td> <td>Discount</td> <td>Shopping</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Rambo</td> <td>Sale</td> <td>Discount1</td> <td>Shopping1</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Rambo</td> <td>Sale</td> <td>Discount2</td> <td>Shopping2</td> </tr> </tbody></table>
 
Upvote 0
Try

Rich (BB code):
Sub InsRowVal()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = LR To 2 Step -1
    If Range("A" & i).Value <> Range("A" & i - 1).Value Then Rows(i).Insert
Next i
LR = Range("A" & Rows.Count).End(xlUp).Row
With Range("B1:B" & LR)
    .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[1]C[-1]"
    .Value = .Value
End With
End Sub
 
Upvote 0
Pefectly working my fren... Is it possible to delete first column after getting the result..

I mean something like

<table border="0" cellpadding="0" cellspacing="0" width="192"><col style="width: 48pt;" width="64" span="3"> <tbody><tr style="height: 15pt;" height="20"> <td class="xl65" style="height: 15pt; width: 48pt;" width="64" height="20">books1</td> <td class="xl65" style="width: 48pt;" width="64">books2</td> <td class="xl65" style="width: 48pt;" width="64">books3</td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl65" style="height: 15pt;" height="20">Hero</td> <td class="xl65">
</td> <td class="xl65">
</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Bestbuy</td> <td>amazon</td> <td>lifestyle</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Bestbuy</td> <td>amazon1</td> <td>lifestyle1</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Bestbuy</td> <td>amazon2</td> <td>lifestyle2</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Rambo</td> <td>
</td> <td>
</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Sale</td> <td>Discount</td> <td>Shopping</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Sale</td> <td>Discount1</td> <td>Shopping1</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Sale</td> <td>Discount2</td> <td>Shopping2</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Sale</td> <td>Discount3</td> <td>Shopping3</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Sale</td> <td>Discount4</td> <td>Shopping4</td> </tr> </tbody></table>
 
Upvote 0
Try

Rich (BB code):
Sub InsRowVal()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = LR To 2 Step -1
    If Range("A" & i).Value <> Range("A" & i - 1).Value Then Rows(i).Insert
Next i
LR = Range("A" & Rows.Count).End(xlUp).Row
With Range("B1:B" & LR)
    .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[1]C[-1]"
    .Value = .Value
End With
Columns("A").Delete
End Sub
 
Upvote 0
Its working fren but :( output is something like (two zeros are appearing)


<table border="0" cellpadding="0" cellspacing="0" width="192"><col style="width: 48pt;" width="64" span="3"> <tbody><tr style="height: 15pt;" height="20"> <td class="xl65" style="height: 15pt; width: 48pt;" width="64" height="20">books1</td> <td class="xl65" style="width: 48pt;" width="64">books2</td> <td class="xl65" style="width: 48pt;" width="64">books3</td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl65" style="height: 15pt;" align="right" height="20">0</td> <td class="xl65">
</td> <td class="xl65">
</td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl65" style="height: 15pt;" align="right" height="20">0</td> <td class="xl65">
</td> <td class="xl65">
</td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl65" style="height: 15pt;" height="20">Hero</td> <td class="xl65">
</td> <td class="xl65">
</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Bestbuy</td> <td>amazon</td> <td>lifestyle</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Bestbuy</td> <td>amazon1</td> <td>lifestyle1</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Bestbuy</td> <td>amazon2</td> <td>lifestyle2</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Bestbuy</td> <td>amazon3</td> <td>lifestyle3</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Bestbuy</td> <td>amazon4</td> <td>lifestyle4</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Rambo</td> <td>
</td> <td>
</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Sale</td> <td>Discount</td> <td>Shopping</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Sale</td> <td>Discount1</td> <td>Shopping1</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Sale</td> <td>Discount2</td> <td>Shopping2</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Sale</td> <td>Discount3</td> <td>Shopping3</td> </tr> <tr style="height: 15pt;" height="20"> <td style="height: 15pt;" height="20">Sale</td> <td>Discount4</td> <td>Shopping4</td> </tr> </tbody></table>
 
Upvote 0
I am sorry Brother...its working fine :-)

Thanks a ton :-)

You are simply genius... Can you refer me any book for dummies to learn VBA programming?
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,738
Members
452,940
Latest member
Lawrenceiow

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top