VBA code to conditionally copy the data
Solved/Closed
sunny2u86
Posts
14
Registration date
Thursday 2 February 2012
Status
Member
Last seen
4 June 2012
-
24 May 2012 à 02:07
sunny2u86 Posts 14 Registration date Thursday 2 February 2012 Status Member Last seen 4 June 2012 - 4 Jun 2012 à 11:34
sunny2u86 Posts 14 Registration date Thursday 2 February 2012 Status Member Last seen 4 June 2012 - 4 Jun 2012 à 11:34
Related:
- VBA code to conditionally copy the data
- How to copy data from one excel sheet to another - Guide
- Vba find - Guide
- Tmobile data check - Guide
- Cs 1.6 code - Guide
- Www.gameloft.com unlock code ✓ - Phones, PDA & GPS Forum
9 responses
RWomanizer
Posts
365
Registration date
Monday 7 February 2011
Status
Contributor
Last seen
30 September 2013
120
24 May 2012 à 04:26
24 May 2012 à 04:26
You can use following codes
Sub SplitSheets()
Dim DataSht, wsCrit, SplitSht As Worksheet
Dim lrUnq, lrData, I As Long
Dim FtrVal As String
Application.ScreenUpdating = False
Set DataSht = Worksheets("sheet1") 'change it to the name of your raw data sheet
lrData = DataSht.Range("a" & Rows.Count).End(xlUp).Row
Set wsCrit = Worksheets.Add
DataSht.Range("B1:l" & lrData).AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=wsCrit.Range("A1"), Unique:=True
lrUnq = wsCrit.Range("a" & Rows.Count).End(xlUp).Row
For I = 2 To lrUnq
FtrVal = wsCrit.Range("A" & i).Value
Set SplitSht = Worksheets.Add
DataSht.Select
'DataSht.ShowAllData
ActiveSheet.AutoFilterMode = False
ActiveSheet.Range("A1:Z" & lrData).AutoFilter Field:=2, Criteria1:=FtrVal
Range("a1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
SplitSht.Select
Range("A1").Select
ActiveSheet.Paste
'Cells.Select
Cells.EntireColumn.AutoFit
SplitSht.Name = FtrVal
Application.CutCopyMode = False
Next i
Application.DisplayAlerts = False
wsCrit.Delete
Application.DisplayAlerts = True
.AutoFilterMode = False
End Sub
Sub SplitSheets()
Dim DataSht, wsCrit, SplitSht As Worksheet
Dim lrUnq, lrData, I As Long
Dim FtrVal As String
Application.ScreenUpdating = False
Set DataSht = Worksheets("sheet1") 'change it to the name of your raw data sheet
lrData = DataSht.Range("a" & Rows.Count).End(xlUp).Row
Set wsCrit = Worksheets.Add
DataSht.Range("B1:l" & lrData).AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=wsCrit.Range("A1"), Unique:=True
lrUnq = wsCrit.Range("a" & Rows.Count).End(xlUp).Row
For I = 2 To lrUnq
FtrVal = wsCrit.Range("A" & i).Value
Set SplitSht = Worksheets.Add
DataSht.Select
'DataSht.ShowAllData
ActiveSheet.AutoFilterMode = False
ActiveSheet.Range("A1:Z" & lrData).AutoFilter Field:=2, Criteria1:=FtrVal
Range("a1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
SplitSht.Select
Range("A1").Select
ActiveSheet.Paste
'Cells.Select
Cells.EntireColumn.AutoFit
SplitSht.Name = FtrVal
Application.CutCopyMode = False
Next i
Application.DisplayAlerts = False
wsCrit.Delete
Application.DisplayAlerts = True
.AutoFilterMode = False
End Sub
sunny2u86
Posts
14
Registration date
Thursday 2 February 2012
Status
Member
Last seen
4 June 2012
27 May 2012 à 02:20
27 May 2012 à 02:20
is any one who can help me..pls.
sunny2u86
Posts
14
Registration date
Thursday 2 February 2012
Status
Member
Last seen
4 June 2012
28 May 2012 à 11:30
28 May 2012 à 11:30
hi please use the this link to get the raw file " http://speedy.sh/RJs2g/pending-raw.xls "
he data it contain is different then I asked but the logic is same.I need all the data in sheet1 will be copied to the sheet2,sheet3 and so on according to the "Group" in sheet1..
the sheet attached is without macro.
thank you
sunny.
he data it contain is different then I asked but the logic is same.I need all the data in sheet1 will be copied to the sheet2,sheet3 and so on according to the "Group" in sheet1..
the sheet attached is without macro.
thank you
sunny.
TrowaD
Posts
2921
Registration date
Sunday 12 September 2010
Status
Contributor
Last seen
27 December 2022
555
29 May 2012 à 10:50
29 May 2012 à 10:50
Hi Sunny,
Is it possible to name each sheet after the group they are in?
So don't name your sheet: "FO CAL" but "Field Ops - Calgary".
Or keep the sheet name "FO CAL" and change the groupname accordingly.
Make a sheet for every groupname.
Post your results.
Best regards,
Trowa
Is it possible to name each sheet after the group they are in?
So don't name your sheet: "FO CAL" but "Field Ops - Calgary".
Or keep the sheet name "FO CAL" and change the groupname accordingly.
Make a sheet for every groupname.
Post your results.
Best regards,
Trowa
sunny2u86
Posts
14
Registration date
Thursday 2 February 2012
Status
Member
Last seen
4 June 2012
30 May 2012 à 10:22
30 May 2012 à 10:22
Hi Trowa,
yes you can change the name of the sheet according to the group they are in say
"Field Ops - Calgary"..
Please let me know if you need any other details.
Thank You,
Sunny
yes you can change the name of the sheet according to the group they are in say
"Field Ops - Calgary"..
Please let me know if you need any other details.
Thank You,
Sunny
sunny2u86
Posts
14
Registration date
Thursday 2 February 2012
Status
Member
Last seen
4 June 2012
30 May 2012 à 10:21
30 May 2012 à 10:21
HI Trowa,
yes you can change the name of the sheet according to the group they are in say
"Field Ops - Calgary"..
Please let me know if you need any other details.
Thank You,
Sunny
yes you can change the name of the sheet according to the group they are in say
"Field Ops - Calgary"..
Please let me know if you need any other details.
Thank You,
Sunny
TrowaD
Posts
2921
Registration date
Sunday 12 September 2010
Status
Contributor
Last seen
27 December 2022
555
31 May 2012 à 09:23
31 May 2012 à 09:23
Good one Sunny,
I meant for you to change the sheet names.
But since I had some spare time (and I am a kind person) I did it for you.
Here is the code:
And here is your file with code:
http://speedy.sh/acm4R/Sunny-pending-raw.xls
Best regards,
Trowa
I meant for you to change the sheet names.
But since I had some spare time (and I am a kind person) I did it for you.
Here is the code:
Sub MoveData()
Dim lRow As Integer
lRow = Range("A" & Rows.Count).End(xlUp).Row
Sheets("raw").Activate
For Each cell In Range("F2:F" & lRow)
cell.EntireRow.Copy
Sheets(cell.Value).Activate
Range("B" & Rows.Count).End(xlUp).Offset(1, -1).PasteSpecial
Next cell
Application.CutCopyMode = False
End Sub
And here is your file with code:
http://speedy.sh/acm4R/Sunny-pending-raw.xls
Best regards,
Trowa
sunny2u86
Posts
14
Registration date
Thursday 2 February 2012
Status
Member
Last seen
4 June 2012
31 May 2012 à 10:38
31 May 2012 à 10:38
Hi Trowa,
Great work again from you man!
I really appreciate your work,but one question,since this raw data is changeable and new group is added as required so can you tell me how can it change it according to the requirement..
thank you
Sunny
Great work again from you man!
I really appreciate your work,but one question,since this raw data is changeable and new group is added as required so can you tell me how can it change it according to the requirement..
thank you
Sunny
TrowaD
Posts
2921
Registration date
Sunday 12 September 2010
Status
Contributor
Last seen
27 December 2022
555
4 Jun 2012 à 10:19
4 Jun 2012 à 10:19
Hi Sunny,
Good to see you like it :).
The adjusted code will check if a sheet already exists with the groups name. If not, a sheets is created with the missing groups name along with the format (first row header and column width of the first 6 columns.).
Here is the code:
And here is your file with code for your convenience:
http://speedy.sh/zvnTp/Sunny-pending-raw.xls
Kind regards,
Trowa
Good to see you like it :).
The adjusted code will check if a sheet already exists with the groups name. If not, a sheets is created with the missing groups name along with the format (first row header and column width of the first 6 columns.).
Here is the code:
Sub MoveData()
Dim lRow As Integer
lRow = Range("A" & Rows.Count).End(xlUp).Row
Sheets("raw").Activate
For Each cell In Range("F2:F" & lRow)
If Not SheetExists(cell.Value) Then
Sheets.Add
ActiveSheet.Name = cell.Value
Sheets("raw").Range("A1:F1").Copy
Sheets(cell.Value).Range("A1").PasteSpecial Paste:=xlPasteAll
Sheets(cell.Value).Range("A1").PasteSpecial Paste:=xlPasteColumnWidths
End If
cell.EntireRow.Copy
Sheets(cell.Value).Activate
Range("B" & Rows.Count).End(xlUp).Offset(1, -1).PasteSpecial
Next cell
Application.CutCopyMode = False
End Sub
Function SheetExists(SheetName As String) As Boolean
SheetExists = False
On Error GoTo NoSuchSheet
If Len(Sheets(SheetName).Name) > 0 Then
SheetExists = True
Exit Function
End If
NoSuchSheet:
End Function
And here is your file with code for your convenience:
http://speedy.sh/zvnTp/Sunny-pending-raw.xls
Kind regards,
Trowa
sunny2u86
Posts
14
Registration date
Thursday 2 February 2012
Status
Member
Last seen
4 June 2012
4 Jun 2012 à 11:34
4 Jun 2012 à 11:34
Hey man,
Thanks again for your help!
thank you,
Sunny..
Thanks again for your help!
thank you,
Sunny..
24 May 2012 à 14:00
but this is showing error called "invalid and unqualified" and also please let me know where we can set the condition?