Predefinição:Taxonomia/Wuerhosaurus

Revisão em 05h28min de 16 de maio de 2024 por Jaewoo (discussão | contribs) (Criou a página com " excel = win32.DispatchEx("Excel.Application") excel.Visible = False # 엑셀 창 숨김 # 엑셀 파일 열기 workbook = excel.Workbooks.Open(file_path) # VBA 매크로 코드 vba_code = """ Private Sub Worksheet_Change(ByVal Target As Range) ' 변경된 범위가 B열에 있는지 확인 If Not Intersect(Target, Me.Columns("B")) Is Nothing Then ' 첫 번째 행은 제목 행이므로 건너뛰기 Call AutoNumberW...")
(dif) ← Revisão anterior | Revisão atual (dif) | Revisão seguinte → (dif)
   excel = win32.DispatchEx("Excel.Application")
   excel.Visible = False  # 엑셀 창 숨김
   # 엑셀 파일 열기
   workbook = excel.Workbooks.Open(file_path)
   
   # VBA 매크로 코드
   vba_code = """

Private Sub Worksheet_Change(ByVal Target As Range)

   ' 변경된 범위가 B열에 있는지 확인
   If Not Intersect(Target, Me.Columns("B")) Is Nothing Then
       ' 첫 번째 행은 제목 행이므로 건너뛰기
       Call AutoNumberWithMergedCells
   End If

End Sub

Sub AutoNumberWithMergedCells()

   Dim ws As Worksheet
   Dim rng As Range
   Dim cell As Range
   Dim currentNumber As Long
   ' 현재 활성 시트 가져오기
   Set ws = ThisWorkbook.ActiveSheet
   
   ' 데이터가 있는 마지막 행 찾기 (B열 기준)
   Dim lastRow As Long
   lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
   
   ' 초기 순번 설정
   currentNumber = 1
   
   ' A열의 순번 초기화 (제목 행을 제외하고)
   ws.Range("A2:A" & lastRow).ClearContents
   
   ' 순번 입력 (제목 행을 제외하고)
   For Each cell In ws.Range("A2:A" & lastRow)
       ' 해당 셀의 병합 범위 가져오기
       Set rng = cell.MergeArea
       
       ' 병합된 셀의 첫 번째 셀에만 순번 부여
       If cell.Address = rng.Cells(1, 1).Address Then
           cell.Value = currentNumber
           currentNumber = currentNumber + 1
       End If
   Next cell

End Sub

   """
   # Sheet1의 코드 모듈에 접근
   sheet_code_module = workbook.VBProject.VBComponents(sheet_name).CodeModule
   sheet_code_module.AddFromString(vba_code)