• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Help to Fix my Code (VBA) in Excel sheet

abdulkharij

New Member
Hi evryone,

I have similar tables in sheet1 and sheet2. I want Rows, with new names, to be added automatically to table in sheet2 if I manually add them to sheet1. I have the following formula that helped me to do that, but the problem with it is that when I manually add a Row to sheet1 it goes automatically to the last Row in sheet2. However, I want someone to help me fix this formula so manually added Rows can be automatically added to the same spot in sheet2 when I manually add them to sheet1.
Example: If I add a row with the name "Jane" in sheet-1 under "Davis" it should automatically be added to similar table in sheet-2 under "David"

Sheet1
A B C D
Name Hours Rate Total
jhon 35 10 350
David 20 11 220
Mark 50 12 600
Hellen 40 11 440
Iren 40 13 520
Nicole 39 14 546

Sheet2
A B C D
Name Hours Rate Total
jhon 35 10 350
David 20 11 220
Mark 50 12 600
Hellen 40 11 440
Iren 40 13 520
Nicole 39 14 546

Code:

Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

Dim cell As Range

If TypeName(Target) = "Range" Then

If LenB(Target.Cells(1, 1).Value) > 0 Then

If Not Intersect(Target, Range("A:A")) Is Nothing Then

If Target.SpecialCells(xlCellTypeLastCell).Row = UsedRange.Rows.Count Then

If UsedRange.Rows.Count > Sheet2.UsedRange.Rows.Count Then

For Each cell In Target.Cells

If Not Intersect(cell, Range("A:A")) Is Nothing Then Sheet2.Cells(Sheet2.UsedRange.Rows.Count + 1, 1).Value = cell.Value

Next

End If

End If

End If

End If

End If

Set cell = Nothing

End Sub
 
Last edited by a moderator:
Why use VBA Code at all

On Sheet1 add a named formula
data: =OFFSET(Sheet1!$A$1,,,COUNTA(Sheet1!$A:$A),COUNTA(Sheet1!$1:$1))

On Sheet2 A1: =IFERROR(data,"")
Copy it as wide and as far down as you need
Allow plenty of extra cells below and besides the known area

Add/Remove/Insert data to sheet 1 as you please
 
Why use VBA Code at all

On Sheet1 add a named formula
data: =OFFSET(Sheet1!$A$1,,,COUNTA(Sheet1!$A:$A),COUNTA(Sheet1!$1:$1))

On Sheet2 A1: =IFERROR(data,"")
Copy it as wide and as far down as you need
Allow plenty of extra cells below and besides the known area

Add/Remove/Insert data to sheet 1 as you please
Thank you for your help, but where in Sheet1 and Sheet2 should I add these formulas?
 
On sheet 1 Goto the Name Manager and add a new name

On sheet 2 in cell A1
 
On sheet 1 Goto the Name Manager and add a new name

On sheet 2 in cell A1
I am so sorry, but i am still confused. I went to Sheet1 => Formulas=>Name Manager=>New. so where in the "New name" table should I past that formula?

In Sheet1 I have "Name" in CellA1 as a content. Should I just erase that and past this formula =IFERROR(Name,"")
here is how my table is presented in excel sheet

A B C D
1 Name Hours Rate Total
2 jhon 35 10 350
3 David 20 11 220
4 Mark 50 12 600
5 Hellen 40 11 440
6 Iren 40 13 520
7 Nicole 39 14 546
 
Last edited:
hallo abdulkharij,

in sheet 1 press "ctrl+F3" after it u can see name manager put name data and then in formula bar type hui's sir formula.... and after it go in sheet 2 and in a1 type again hui's sir second formula then done..
 

Hi Udprocks
Thank you for you clear explanation.
How about if I have the following order in my tables. How should I change the formulas to make them work properly

SHEET1
A B C D
1 Name Hours Rate Total
2 jhon 35 10 350
3 David 20 11 220
4 Mark 50 12 600
5 Hellen 40 11 440
6 Iren 40 13 520
7 Nicole 39 14 546

SHEET2
C D G H
5 Name Hours Rate Total
6 jhon 35 10 350
7 David 20 11 220
8 Mark 50 12 600
9 Hellen 40 11 440
10 Iren 40 13 520
11 Nicole 39 14 546


Thank you
 
hallo abdulkharij,

in sheet 1 press "ctrl+F3" after it u can see name manager put name data and then in formula bar type hui's sir formula.... and after it go in sheet 2 and in a1 type again hui's sir second formula then done..

Hi Udprocks
Thank you for you clear explanation.
How about if I have the following order in my tables. How should I change the formulas to make them work properly

SHEET1
A B C D
1 Name Hours Rate Total
2 jhon 35 10 350
3 David 20 11 220
4 Mark 50 12 600
5 Hellen 40 11 440
6 Iren 40 13 520
7 Nicole 39 14 546

SHEET2
G H I J
5 Name Hours Rate Total
6 jhon 35 10 350
7 David 20 11 220
8 Mark 50 12 600
9 Hellen 40 11 440
10 Iren 40 13 520
11 Nicole 39 14 546


Thank you
 
Last edited:
Back
Top