Click here to Skip to main content
15,741,692 members
Please Sign up or sign in to vote.
4.33/5 (3 votes)
See more:

TO copy one excel file cell reading to another excel template.
Cell to cell copypaste.

Below just to show what those declaration means..

VB
---------------------------------------------------------
Public Class TCombMerge
    Implements ICombMerge

    Dim sHT As String = String.Empty 'header top
    Dim sHB As String = String.Empty 'header bottom
    Dim sRT As String = String.Empty 'report top
    Dim sRB As String = String.Empty 'report bottom
    Dim sTF As String = String.Empty 'template file

   Function MergeFiles() As Long Implements ICombMerge.MergeFiles
        Dim retVal As Long = 0, i As Long = 0, j As Long = 0
        Dim sFiles As ArrayList = Nothing, oExc As Object = Nothing
        Dim oWbkSrc As Object = Nothing, oWbkDst As Object = Nothing
        Dim oWshSrc As Object = Nothing, oWshDst As Object = Nothing
-----------------------------------------------------------

-----------------------------------------------------------
Begin from here to end.
is this right? is there room to simplify the code.
EXAMPLE: stop until there are no reading.



VB
--REPORT TOP & BOTTOM ----

---------------------------
-----BEGIN FOR TOP ------
--------------------------

'open source file Report Top / OPEN ONCE
oWbkSrc = oExc.Workbooks.Open(sRT) 

'ReportTop:A2->I2 copy to Template:A16->I16 
For i = 2 
j = i + 14 
oWshSrc.Range("A"& i.ToString).Copy(oWshDst.Range("A" & j.ToString))
 
'corresponding row of B,C,D,E,F,G,H,I 
oWshSrc.Range("A"& i.ToString).Copy(oWshDst.Range("A"& j.ToString).offset(0,1)) 

----NEXT, SKIP 1 CELL THEN----


'ReportTop:A3->I3 copy to Template:A18->I18
For i = 2 
j = i + 16 
oWshSrc.Range("A"& i.ToString).Copy(oWshDst.Range("A" & j.ToString))
 
'corresponding row of B,C,D,E,F,G,H,I 
oWshSrc.Range("A"& i.ToString).Copy(oWshDst.Range("A"& 
j.ToString).offset(0,1)) 


----NEXT, SKIP 1 CELL THEN----

'ReportTop:A4->I4 copy to Template:A20->I20
For i = 2 
j = i + 18 
oWshSrc.Range("A"& i.ToString).Copy(oWshDst.Range("A" & j.ToString))
 
'corresponding row of B,C,D,E,F,G,H,I 
oWshSrc.Range("A"& i.ToString).Copy(oWshDst.Range("A"& j.ToString).offset(0,1)) 



----NEXT, SKIP 1 CELL THEN----

'ReportTop:A5->I5 copy to Template:A22->I22
For i = 2 
j = i + 20 
oWshSrc.Range("A"& i.ToString).Copy(oWshDst.Range("A" & j.ToString))
 
'corresponding row of B,C,D,E,F,G,H,I 
oWshSrc.Range("A"& i.ToString).Copy(oWshDst.Range("A"& j.ToString).offset(0,1))



----NEXT, SKIP 1 CELL THEN----

'ReportTop:A7->I7 copy to Template:A24->I24
For i = 2 
j = i + 22
oWshSrc.Range("A"& i.ToString).Copy(oWshDst.Range("A" & j.ToString))
 
'corresponding row of B,C,D,E,F,G,H,I 
oWshSrc.Range("A"& i.ToString).Copy(oWshDst.Range("A"& j.ToString).offset(0,1))


LOOP UNTILL THERE ARE NO READING.
---------------------------
-----BEGIN FOR BOTTOM ------
--------------------------

'open source file Report Bottom
oWbkSrc = oExc.Workbooks.Open(sRB)//OPEN ONCE 

'ReportBottom:A2->I2 copy to Template:A17->I17 
For i = 2 
j = i + 15 
oWshSrc.Range("A"& i.ToString).Copy(oWshDst.Range("A" & j.ToString).offset(1,0) 

'corresponding row of B,C,D,E,F,G,H,I 
oWshSrc.Range("A"& i.ToString).Copy(oWshDst.Range("A" & j.ToString).offset(0,1)) 


-----------BEGIN FROM 1st SKIPPED CELL----------


'ReportBottom:A3->I3 copy to Template:A19->I19 
For i = 2 
j = i + 17 
oWshSrc.Range("A"& i.ToString).Copy(oWshDst.Range("A" & j.ToString).offset(1,0) 

'corresponding row of B,C,D,E,F,G,H,I 
oWshSrc.Range("A"& i.ToString).Copy(oWshDst.Range("A" & j.ToString).offset(0,1)) 



-----------NEXT 2nd SKIPPED CELL-----------


'ReportBottom:A4->I4 copy to Template:A21->I21 
For i = 2 
j = i + 19 
oWshSrc.Range("A"& i.ToString).Copy(oWshDst.Range("A" & j.ToString).offset(1,0) 

'corresponding row of B,C,D,E,F,G,H,I 
oWshSrc.Range("A"& i.ToString).Copy(oWshDst.Range("A" & j.ToString).offset(0,1)) 


-----------NEXT 3rd SKIPPED CELL-----------


'ReportBottom:A5->I5 copy to Template:A23->I23 
For i = 2 
j = i + 21 
oWshSrc.Range("A"& i.ToString).Copy(oWshDst.Range("A" & j.ToString).offset(1,0) 

'corresponding row of B,C,D,E,F,G,H,I 
oWshSrc.Range("A"& i.ToString).Copy(oWshDst.Range("A" & j.ToString).offset(0,1)) 


LOOP UNTILL THERE ARE NO READING.
Posted
Updated 20-Dec-11 14:17pm
v10

1 solution

Not sure what do you mean by simplification. If you have the business logic, you can write them in module/function in programmers' simplicity format.
 
Share this answer
 
Comments
Human2.0 8-Dec-11 1:46am    
Thanks for the reply Ganesan, i mean the codes actually repetitive..imagine writing the chunk of code for each cell until cell A800 in excel. its a loop thing.

so if any one could suggest how to create the loop based on what i written so far would be helpful for me to progress further :)
seravan 8-Dec-11 21:26pm    
Just wanted to ask on why do you need to copy cell by cell? Why not just copy the whole range then post it to it's target?
Human2.0 8-Dec-11 22:46pm    
Hi Seravan, good question

If u read my code. You could see there are TopReport, BottomReport

The 2 separate workbook need to be read cell by cell so that

the final new file will have reading like :
top reading
bottom reading
top reading
bottom reading
.
.
.
so its like one below each other, 1 by 1
Amir Mahfoozi 21-Dec-11 0:28am    
Hi human,
If you want to find out what is the best way to do a job in Excel automation start to record a macro then do what you mean then stop recording and after that go to macro sources and see what have happened in there. I always use this pattern to implement whatever users want in c#.
Good Luck

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900