LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 557
Default Sort Data in Specific way

On 3 Nov, 11:30, Joel wrote:
I added some comments to code below and copied formula instead of value.

Sub SortColumns()

With Sheets("Sheet1")
* *'copy columns A - c from sheet 1 to sheet 2
* *.Columns("$A:$C").Copy _
* * * Destination:=Sheets("Sheet2").Columns("$A")

* *'row.count is Last row on worksheet
* *'Search Up column A from Last Row until data is found
* *LastRow = .Range("A" & Rows.Count).End(xlUp).Row
* *NewRow = LastRow + 1

* *'Move column D,E and then column F,G
* *For ColCount = 4 To 6 Step 2
* * * RowCount = 1
* * * 'Loop until no data is found in column D and F (Value of ColCount)
* * * Do While .Cells(RowCount, ColCount) < ""

* * * * *'Get Data from Sheet1
* * * * *Key = .Cells(RowCount, ColCount)
* * * * *MyFormula = .Cells(RowCount, ColCount + 1).Formula

* * * * *With Sheets("Sheet2")
* * * * * * 'check if Key is in column A
* * * * * * Set c = .Columns("A").Find( _
* * * * * * * *what:=Key, _
* * * * * * * *LookIn:=xlValues, _
* * * * * * * *lookat:=xlWhole)

* * * * * * 'check if Key is found. *Will be nothing if not found
* * * * * * If c Is Nothing Then
* * * * * * * *'If column F check if data is in column D
* * * * * * * *If ColCount = 6 Then
* * * * * * * * * 'Look for Key in Column D
* * * * * * * * * Set c = .Columns("D").Find( _
* * * * * * * * * * *what:=Key, _
* * * * * * * * * * *LookIn:=xlValues, _
* * * * * * * * * * *lookat:=xlWhole)

* * * * * * * * * 'check if Key is found. *Will be nothing if not found
* * * * * * * * * If c Is Nothing Then
* * * * * * * * * * *'Write Key and fomula to sheet 2 (Col D,E or F,G) in
New row
* * * * * * * * * * *.Cells(NewRow, ColCount) = Key
* * * * * * * * * * *.Cells(NewRow, ColCount + 1).Formula = MyFormula
* * * * * * * * * * *'Increment Row to put data on Sheet2
* * * * * * * * * * *NewRow = NewRow + 1
* * * * * * * * * Else
* * * * * * * * * * *'Write Key and fomula to sheet 2 (Col D,E or F,G)
* * * * * * * * * * *'Write data to row where key was found in FIND function
above
* * * * * * * * * * *.Cells(c.Row, ColCount) = Key
* * * * * * * * * * *.Cells(c.Row, ColCount + 1).Formual = MyFormula
* * * * * * * * * End If
* * * * * * * *Else
* * * * * * * * * 'Write Key and fomula to sheet 2 (Col D,E or F,G) in New row
* * * * * * * * * .Cells(NewRow, ColCount) = Key
* * * * * * * * * .Cells(NewRow, ColCount + 1).Formula = MyFormula
* * * * * * * * * NewRow = NewRow + 1
* * * * * * * *End If
* * * * * * Else
* * * * * * * *'Key was found in Column A
* * * * * * * *'Write Key and fomula to sheet 2 (Col D,E or F,G)
* * * * * * * *'Write data to row where key was found in FIND function above
* * * * * * * *.Cells(c.Row, ColCount) = Key
* * * * * * * *.Cells(c.Row, ColCount + 1).Formula = MyFormula
* * * * * * End If
* * * * *End With
* * * * *RowCount = RowCount + 1
* * * Loop
* *Next ColCount
End With

End Sub



"K" wrote:
Thanks lot Joel your code works perfectly fine. I just have two
questions that i got formulas like (=1000-200) etc in column E and G.
Is it possible that when macro copy data to Sheet 2 it should keep
formulas in these columns instead of just values. *Like if i have
formula in Sheet 1 column E cell which is (=1000-200) so when macro
copy data to Sheet 2 it should keep this formula in column E as
(=1000-200) instead of putting value (800). I know you can only see
the result in cell but when you select that cell you can see its
formula aswell in above formula bar so thats why i need formulas to be
in cell. *And second question is that i really liked the way you
solved my problem is it possilbe for you explain me bit more that how
your code is doing its work, *just for my knowledge. *Thanks again for
your help- Hide quoted text -


- Show quoted text -


Thanks lot joel
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
HOW DO I SORT DATA USING A SPECIFIC CHARACTER IN A CELL GEORGE Excel Discussion (Misc queries) 1 May 31st 07 04:42 PM
HOw do i find and sort out data repeats for specific data set JRDePaul Excel Discussion (Misc queries) 0 February 26th 07 11:55 PM
Data Sort Macro for specific cells slundrigan via OfficeKB.com Excel Programming 1 December 21st 06 06:20 AM
sort data by specific word in cell? Nicki_xxxxxxxxx Excel Discussion (Misc queries) 1 April 4th 06 10:39 AM
How can I find and sort specific data within a column? bpreas - ExcelForums.com Excel Discussion (Misc queries) 3 August 2nd 05 07:11 PM


All times are GMT +1. The time now is 01:17 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"