Excel-tips: Dela upp data i flera kalkylblad/arbetsböcker baserat på kolumnvärde
When managing large datasets in Excel, it can be highly beneficial to split data into multiple worksheets based on specific column values. This method improves not only the organization of data but also enhances readability and facilitates easier data analysis.
Suppose you have a large sales record containing multiple entries such as the product name, the quantity sold of the first quarter. The goal is to split this data into separate worksheets based on each product name so that individual sales performance can be analyzed separately.
Split data into multiple worksheets based on column value
Split data into multiple workbooks based on column value with VBA code
Split data into multiple worksheets based on column value
Normally, you can sort the data list first, and then copy and paste them one by one into other new worksheets. But this will need your patience to copy and paste repeatedly. In this section, we will introduce two straightforward methods to efficiently tackle this task in Excel, saving you time and reducing the potential for errors.
Split data into multiple worksheets based on column value with VBA code
1. Håll ner ALT + F11 nycklar för att öppna Microsoft Visual Basic för applikationer fönster.
2. klick Insert > Modulernaoch klistra in följande kod i modulfönstret.
Sub Splitdatabycol()
'updateby Extendoffice
Dim lr As Long
Dim ws As Worksheet
Dim vcol, i As Integer
Dim icol As Long
Dim myarr As Variant
Dim title As String
Dim titlerow As Integer
Dim xTRg As Range
Dim xVRg As Range
Dim xWSTRg As Worksheet
Dim xWS As Worksheet
On Error Resume Next
Set xTRg = Application.InputBox("Please select the header rows:", "Kutools for Excel", "", Type:=8)
If TypeName(xTRg) = "Nothing" Then Exit Sub
Set xVRg = Application.InputBox("Please select the column you want to split data based on:", "Kutools for Excel", "", Type:=8)
If TypeName(xVRg) = "Nothing" Then Exit Sub
vcol = xVRg.Column
Set ws = xTRg.Worksheet
lr = ws.Cells(ws.Rows.Count, vcol).End(xlUp).Row
title = xTRg.AddressLocal
titlerow = xTRg.Cells(1).Row
icol = ws.Columns.Count
ws.Cells(1, icol) = "Unique"
Application.DisplayAlerts = False
If Not Evaluate("=ISREF('xTRgWs_Sheet!A1')") Then
Sheets.Add(after:=Worksheets(Worksheets.Count)).Name = "xTRgWs_Sheet"
Else
Sheets("xTRgWs_Sheet").Delete
Sheets.Add(after:=Worksheets(Worksheets.Count)).Name = "xTRgWs_Sheet"
End If
Set xWSTRg = Sheets("xTRgWs_Sheet")
xTRg.Copy
xWSTRg.Paste Destination:=xWSTRg.Range("A1")
ws.Activate
For i = (titlerow + xTRg.Rows.Count) To lr
On Error Resume Next
If ws.Cells(i, vcol) <> "" And Application.WorksheetFunction.Match(ws.Cells(i, vcol), ws.Columns(icol), 0) = 0 Then
ws.Cells(ws.Rows.Count, icol).End(xlUp).Offset(1) = ws.Cells(i, vcol)
End If
Next
myarr = Application.WorksheetFunction.Transpose(ws.Columns(icol).SpecialCells(xlCellTypeConstants))
ws.Columns(icol).Clear
For i = 2 To UBound(myarr)
ws.Range(title).AutoFilter field:=vcol, Criteria1:=myarr(i) & ""
If Not Evaluate("=ISREF('" & myarr(i) & "'!A1)") Then
Set xWS = Sheets.Add(after:=Worksheets(Worksheets.Count))
xWS.Name = myarr(i) & ""
Else
xWS.Move after:=Worksheets(Worksheets.Count)
End If
xWSTRg.Range(title).Copy
xWS.Paste Destination:=xWS.Range("A1")
ws.Range("A" & (titlerow + xTRg.Rows.Count) & ":A" & lr).EntireRow.Copy xWS.Range("A" & (titlerow + xTRg.Rows.Count))
Sheets(myarr(i) & "").Columns.AutoFit
Next
xWSTRg.Delete
ws.AutoFilterMode = False
ws.Activate
Application.DisplayAlerts = True
End Sub
3. Tryck sedan på F5 key to run the code, and a prompt box is popped out to remind you select the header row, and then, click OK. Se skärmdump:
4. In the second prompt box, please select the column data that you want to split based on, then, click OK. Se skärmdump:
5. All data in the active worksheet is divided into multiple worksheets based on the column values. The resulting worksheets are named according to the values in the split cells and are placed at the end of the workbook. See screenshot:
Split data into multiple worksheets based on column value with Kutools for Excel
Kutools för Excel brings smart feature – Split data right into your Excel environment. Splitting data into multiple worksheets is no longer a challenge. Our intuitive tool automatically divides your dataset based on the chosen column value or rows count, ensuring that each piece of information is exactly where you need it. Say goodbye to the tedious task of manually organizing your spreadsheets and embrace a faster, error-free way to manage your data.
När du har installerat Kutools för Excel, välj dataintervallet och klicka sedan Kutools Plus > Split data att öppna Split Data into multiple worksheets dialog ruta.
- Välja Specifik kolumn alternativet i Dela baserat på section, and choose the column value which you want to split the data based on from the drop-down list.
- Om dina data har rubriker och du vill infoga dem i varje nytt delat kalkylblad, kontrollera Mina data har rubriker option. (You can specify the number of header rows based on your data. For example, if your data contains two headers, please type 2.)
- Sedan kan du ange namnen på delat kalkylblad under Nytt kalkylbladets namn section, specify the worksheet names rule from the Rules drop down list, you can add the Prefix or Ändelse för arknamnen också.
- Klicka på OK knapp. Se skärmdump:
Now, the data in the worksheet is split into multiple worksheets in a new workbook.
Split data into multiple workbooks based on column value with VBA code
Occasionally, rather than dividing data into multiple worksheets, it may be more beneficial to split the data into separate workbooks based on a key column. Here's a step-by-step guide on how to use VBA code to automate the process of splitting data into multiple workbooks based on a specific column value.
1. Håll ner ALT + F11 nycklar för att öppna Microsoft Visual Basic för applikationer fönster.
2. klick Insert > Modulernaoch klistra in följande kod i Modulfönster.
Sub SplitDataByColToWorkbooks()
' Updateby Extendoffice
Dim lr As Long
Dim ws As Worksheet
Dim vcol, i As Integer
Dim myarr As Variant
Dim title As String
Dim titlerow As Integer
Dim xTRg As Range
Dim xVRg As Range
Dim xWS As Workbook
Dim savePath As String
' Set the directory to save new workbooks
savePath = "C:\Users\AddinsVM001\Desktop\multiple files\" ' Modify this path as needed
Application.DisplayAlerts = False
Set xTRg = Application.InputBox("Please select the header rows:", "Kutools for Excel", Type:=8)
If TypeName(xTRg) = "Nothing" Then Exit Sub
Set xVRg = Application.InputBox("Please select the column you want to split data based on:", "Kutools for Excel", Type:=8)
If TypeName(xVRg) = "Nothing" Then Exit Sub
vcol = xVRg.Column
Set ws = xTRg.Worksheet
lr = ws.Cells(ws.Rows.Count, vcol).End(xlUp).Row
title = xTRg.Address(False, False)
titlerow = xTRg.Row
ws.Columns(vcol).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=ws.Cells(1, ws.Columns.Count), Unique:=True
myarr = Application.Transpose(ws.Cells(1, ws.Columns.Count).Resize(ws.Cells(ws.Rows.Count, ws.Columns.Count).End(xlUp).Row).Value)
ws.Cells(1, ws.Columns.Count).Resize(ws.Cells(ws.Rows.Count, ws.Columns.Count).End(xlUp).Row).ClearContents
For i = 2 To UBound(myarr)
Set xWS = Workbooks.Add
ws.Range(title).AutoFilter Field:=vcol, Criteria1:=myarr(i)
ws.Range("A" & titlerow & ":A" & lr).SpecialCells(xlCellTypeVisible).EntireRow.Copy
xWS.Sheets(1).Cells(1, 1).PasteSpecial Paste:=xlPasteAll
xWS.SaveAs Filename:=savePath & myarr(i) & ".xlsx"
xWS.Close SaveChanges:=False
Next i
ws.AutoFilterMode = False
Application.DisplayAlerts = True
ws.Activate
End Sub
3. Tryck sedan på F5 key to run the code, and a prompt box is popped out to remind you select the header row, and then, click OK. Se skärmdump:
4. In the second prompt box, please select the column data that you want to split based on, then, click OK. Se skärmdump:
5. After splitting, all data in the active worksheet is divided into multiple workbooks based on the column values. All the split workbooks are saved into the folder you specified. See screenshot:
Relaterade artiklar:
- Dela data i flera kalkylblad efter antal rader
- Att effektivt dela upp ett stort dataintervall i flera Excel-kalkylblad baserat på ett specifikt radantal kan effektivisera datahanteringen. Att till exempel dela upp en datauppsättning var 5:e rad i flera ark kan göra den mer hanterbar och organiserad. Den här guiden erbjuder två praktiska metoder för att utföra denna uppgift snabbt och enkelt.
- Slå samman två eller flera tabeller till en baserad på nyckelkolumner
- Om du antar att du har tre tabeller i en arbetsbok, nu vill du slå samman dessa tabeller i en tabell baserat på motsvarande nyckelkolumner för att få resultatet enligt nedanstående skärmdump. Det här kan vara en besvärlig uppgift för de flesta av oss, men oroa dig inte, den här artikeln, jag kommer att introducera några metoder för att lösa detta problem.
- Split Text Strings by Delimiter into Multiple Rows
- Normalt kan du använda funktionen Text till kolumn för att dela upp cellinnehåll i flera kolumner med en specifik avgränsare, såsom kommatecken, punkt, semikolon, snedstreck, etc. Men ibland kan du behöva dela upp det avgränsade cellinnehållet i flera rader och upprepa data från andra kolumner enligt skärmbilden nedan. Har du några bra sätt att hantera denna uppgift i Excel? Denna handledning introducerar några effektiva metoder för att slutföra det här jobbet i Excel.
- Split multiline cell contents into separated rows/columns
- Om du antar att du har cellinnehåll med flera rader som separeras med Alt + Enter, och nu måste du dela upp flerradsinnehållet till separerade rader eller kolumner, vad kan du göra? I den här artikeln kommer du att lära dig hur du snabbt delar upp flerradscellinnehåll i separerade rader eller kolumner.
Bästa kontorsproduktivitetsverktyg
Uppgradera dina Excel-färdigheter med Kutools för Excel och upplev effektivitet som aldrig förr. Kutools för Excel erbjuder över 300 avancerade funktioner för att öka produktiviteten och spara tid. Klicka här för att få den funktion du behöver mest...
Fliken Office ger ett flikgränssnitt till Office och gör ditt arbete mycket enklare
- Aktivera flikredigering och läsning i Word, Excel, PowerPoint, Publisher, Access, Visio och Project.
- Öppna och skapa flera dokument i nya flikar i samma fönster, snarare än i nya fönster.
- Ökar din produktivitet med 50 % och minskar hundratals musklick för dig varje dag!