Please help really need this to work

G

Guest

have a spread sheet with column A and B being static but Column C1 has a
value 101 and D has 102 and so on till 900. What I want to do is that compare
C1 with another sheet that is in same format but missing number in between
for example its has C1=101 and D1=103. If C101 is there i need to copy all
value in that column to sheet 2 underneath C101 but if D1 is 103 then it
should put 0' in D1=102 from D2 - D25. let me know if there can be any macro
written to compare and copy the values.

If you dont understand anypart please feel free to ask as many questions

Your help will be life saver.

Regards

Arain


Joel wrote this code but its not working please let me know if you can fix it.


To explain in detail. The first sheet looks like this

c d e
101 103 104
2256 2223 3345

sheet 2 looks like this

C D E F
101 102 103 104

Now i want to check if 101 exist in sheet 1 copy all values underneath that
column to sheet 2 till C25

If 102 does not exist replace the cell with D25

if 103 exist copy all value undernead 103 to sheet 2 and so on.

Sub copycells()
Const FirstSheet = "sheet1"
Const SecondSheet = "sheet2"


Sheets(FirstSheet).Activate
LastColumn = Cells(1, Columns.Count).End(xlToLeft).Column

Set HeaderRange = Range(Cells(1, "C"), Cells(1, LastColumn))

For Each cell In HeaderRange

If cell = Sheets(SecondSheet).Cells(cell.Row, cell.Column) Then

Sheets(FirstSheet).Activate
LastRow = Cells(Rows.Count, cell.Column).End(xlUp).Row
Set RowRange = Range(Cells(2, cell.Column), Cells(LastRow, cell.Column))

RowRange.Copy Destination:=Sheets(SecondSheet).Cells(2, cell.Column)
Else

Sheets(SecondSheet).Activate
Cells(1, cell.Column) = cell

Set PasteRange = Sheets(SecondSheet).Range(Cells(2, cell.Column), _
Cells(25, cell.Column))

PasteRange.Select
Selection = 0

End If


Please help
 
G

Guest

Is this better?

I am not sure about what to do if 102 is missing in Sheet1. Currently cells
under 102 are set to 0.

Sub copycells()
Const FirstSheet = "sheet1"
Const SecondSheet = "sheet2"


Sheets(SecondSheet).Activate
LastColumn = Cells(1, Columns.Count).End(xlToLeft).Column

Set HeaderRange = Range(Cells(1, 1), Cells(1, LastColumn))

col = 1

For Each cell In HeaderRange

col = Application.Match(cell, Sheets(FirstSheet).Range("1:1"), 0)
If IsNumeric(col) Then

Sheets(FirstSheet).Activate
LastRow = Cells(Rows.Count, col).End(xlUp).Row
Set RowRange = Range(Cells(2, col), Cells(LastRow, col))
RowRange.Copy Destination:=Sheets(SecondSheet).Cells(2, cell.Column)
Else

Sheets(SecondSheet).Activate
Set PasteRange = Sheets(SecondSheet).Range(Cells(2, cell.Column), _
Cells(25, cell.Column))

PasteRange.Select
Selection = 0

End If
Next

End Sub

HTH
 
G

Guest

Ok it didnt do anything but just put 0 in all columns as soon as it found
that 102 is not in sheet 1 and put 0 for all the rest of them even with
correct data. I want it in a way that if sheet 1 doesnot have 102 and sheet2
has it than just put 0 from 1-25 in row. if there is a match in data for
example of 103 has data in sheet 1 then copy the data for 103 from sheet 1 to
sheet 2 which is about 25 line.

Please help me this is very important for my work. i appreciate all your help
 
G

Guest

In my test I had 101,103 and 104 in row 1 of Sheet1 and data underneath each
heading.

In Sheet2 I had 101,102,103 and 104 in row 1 but no data in row 2 onwards


It placed the Sheet1 data under the correct headings in Sheet2 with 0s under
102.

I have re-tested and it works OK.

It's getting late here in UK so I'll be signing off soon.


If you want, send sample w/sheet to:

toppers at REMOVETHISjohn.topley.fsnet.co.uk
 
G

Guest

Sorry ...typo ( I said it's getting late!)

toppers at REMOVETHISjohntopley.fsnet.co.uk
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top