View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.misc
Toppers Toppers is offline
external usenet poster
 
Posts: 4,339
Default Please help really need this to work

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

"Arain" wrote:

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