Excel에서 일부 정보를 자동화하려고 합니다.
A열의 값을 "사업 부문"(열 E)의 수로 균등하게 나누고 각 "사업 부문"을 별도의 행에 표시해야 합니다.
가능하며 어떻게 해야 합니까?
입력:
Amount summary_type Application Cost Source Line of Business
0,6 Employee eDrive Monitoring eDrive R&D; APAC; Group IT;
예상 결과:
Amount summary_type Application Cost Source Line of Business
0,2 Employee eDrive Monitoring eDrive R&D;
0,2 Employee eDrive Monitoring eDrive APAC;
0,2 Employee eDrive Monitoring eDrive Group IT;
답변1
콜론으로 끝나는 "사업 부문"의 모든 작업이 있다고 가정하면 다음 코드를 사용하여 가능합니다.
실행 취소할 수 없으므로 먼저 백업을 수행하세요.
Public Sub SortRecords()
Dim intENDROW As Integer
Dim intCOUNTER As Integer
Dim intCOUNTER2 As Integer
Dim intSTRINGLENGTH As Integer
Dim intNUMBERCOLON As Integer
Dim intSTARTROW As Integer
Dim currDIVIDED As Currency
Dim intSTART As Integer
Dim intPOS As Integer
intENDROW = Range("A65536").End(xlUp).Row 'Get last row containing data
intSTARTROW = intENDROW + 3
' Re-populate headers
Range("A" & intENDROW + 2).Value = Range("A1").Text
Range("B" & intENDROW + 2).Value = Range("B1").Text
Range("C" & intENDROW + 2).Value = Range("C1").Text
Range("D" & intENDROW + 2).Value = Range("D1").Text
Range("E" & intENDROW + 2).Value = Range("E1").Text
For intCOUNTER = 2 To intENDROW
intNUMBERCOLON = 0
intSTART = 1
intSTRINGLENGTH = Len(Range("E" & intCOUNTER).Text) ' Get length of string containing "Line of Business"
For intCOUNTER2 = 1 To intSTRINGLENGTH
If Mid(Range("E" & intCOUNTER).Text, intCOUNTER2, 1) = ";" Then intNUMBERCOLON = intNUMBERCOLON + 1 ' Count how many colons are in this line
Next
If intNUMBERCOLON > 0 Then
currDIVIDED = Range("A" & intCOUNTER).Value / intNUMBERCOLON ' Get average value of Amount column
For intCOUNTER2 = 1 To intNUMBERCOLON
intPOS = InStr(intSTART, Range("E" & intCOUNTER).Text, ";", vbTextCompare) ' Find each instance of a colon
Range("E" & intSTARTROW + intCOUNTER2 - 1).Value = Mid(Range("E" & intCOUNTER).Text, intSTART, intPOS - intSTART + 1) ' Copy text before colon to new line
intSTART = intPOS + 2 ' Update start search position
Next
For intCOUNTER2 = intSTARTROW To (intNUMBERCOLON + intSTARTROW - 1)
Range("A" & intCOUNTER2).Value = currDIVIDED
Range("B" & intCOUNTER2).Value = Range("B" & intCOUNTER).Text
Range("C" & intCOUNTER2).Value = Range("C" & intCOUNTER).Text
Range("D" & intCOUNTER2).Value = Range("D" & intCOUNTER).Text
Next
intSTARTROW = intSTARTROW + intNUMBERCOLON
End If
Next
Range("A1", "A65536").NumberFormat = "General" ' Restore Amount column to a standard number
End Sub
이에:
복사해서 붙여넣기 전에 확인하실 수 있도록 일부러 같은 시트에 잘라낸 데이터를 넣어두었습니다.