Terry Very Good

[ChatGPT] ChatGPT로 VBA를 자유롭게 빠르게 사용해보자! 본문

신기술 습득/ChatGPT

[ChatGPT] ChatGPT로 VBA를 자유롭게 빠르게 사용해보자!

테리베리 2023. 4. 11. 13:14
728x90
반응형

가장 좋은 꿀팁은 

질문: 인터넷에서 엑셀표를 복사해왔는데, 깔끔하게 보일 수 있는 모든 유용한 vba 코드를 알려줘.

___________________________

테스트해보기 위한 사전 준비: 엑셀 열고 > 매크로 허용 옵션 > VBA창 띄우기(ALT+F11) > 네이버 파이넨스 표 복붙

질문: 엑셀 시트에서 빈 행을 삭제하는 vba 작성해줘!

Sub deleteEmptyRows()
    Dim lastRow As Long
    Dim i As Long
    
    ' 마지막 행 찾기
    lastRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    ' 역순으로 루프를 돌며 빈 행 삭제
    For i = lastRow To 1 Step -1
        If WorksheetFunction.CountA(Rows(i)) = 0 Then
            Rows(i).Delete
        End If
    Next i
End Sub

 

질문: 모든 행의 높이를 동일하게 맞추는 vba를 작성해줘!

Sub setRowHeight()
    Dim rowHeight As Double
    rowHeight = 15 ' 변경할 행의 높이를 입력하세요
    
    Dim i As Integer
    For i = 1 To ActiveSheet.UsedRange.Rows.Count
        Rows(i).rowHeight = rowHeight
    Next i
End Sub

결과: 실행하고 나니 이쁘게 맞춰졌군!

'질문: 엑셀 시트에서 빈 행을 삭제하는 vba 작성해줘!
Sub setFont()
    Dim fontName As String
    fontName = "Arial" ' 변경할 폰트 이름을 입력하세요
    
    Dim fontSize As Double
    fontSize = 10 ' 변경할 폰트 크기를 입력하세요
    
    Dim rng As Range
    Set rng = ActiveSheet.Cells ' 대상 범위를 변경하고 싶은 셀 범위로 지정하세요
    
    rng.Font.Name = fontName
    rng.Font.Size = fontSize
End Sub

'질문: 모든 행의 높이를 동일하게 맞추는 vba를 작성해줘!
Sub setBorder()
    Dim rng As Range
    Set rng = ActiveSheet.UsedRange ' 대상 범위를 변경하고 싶은 셀 범위로 지정하세요
    
    rng.BorderAround LineStyle:=xlContinuous, Weight:=xlThin, ColorIndex:=xlAutomatic ' 테두리 선 스타일과 두께, 색상을 입력하세요
End Sub

________________________________________________________

가장 좋은 꿀팁은 

질문: 인터넷에서 엑셀표를 복사해왔는데, 깔끔하게 보일 수 있는 모든 유용한 vba 코드를 알려줘.

1. 빈 행과 열 삭제하기

Sub CleanTable()
   Dim iLastRow As Long, iLastCol As Long
   Dim rng As Range
   Dim i As Long, j As Long

   '마지막 행과 열 찾기
   iLastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
   iLastCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column

   '빈 행 삭제
   For i = iLastRow To 1 Step -1
      If WorksheetFunction.CountA(Rows(i)) = 0 Then Rows(i).Delete
   Next i

   '빈 열 삭제
   For j = iLastCol To 1 Step -1
      If WorksheetFunction.CountA(Columns(j)) = 0 Then Columns(j).Delete
   Next j

   '모든 셀 선택 및 테두리 적용
   Set rng = ActiveSheet.Range(Cells(1, 1), Cells(iLastRow, iLastCol))
   rng.Select
   rng.BorderAround LineStyle:=xlContinuous, Weight:=xlThin, ColorIndex:=xlAutomatic
End Sub

2. 텍스트 정렬하기

Sub AlignText()
   Dim iLastRow As Long, iLastCol As Long
   Dim rng As Range

   '마지막 행과 열 찾기
   iLastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
   iLastCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column

   '모든 셀 선택 및 가운데 정렬
   Set rng = ActiveSheet.Range(Cells(1, 1), Cells(iLastRow, iLastCol))
   rng.Select
   With Selection
      .HorizontalAlignment = xlCenter
      .VerticalAlignment = xlCenter
      .WrapText = True
   End With
End Sub

3. 특정 컬럼의 데이터 포맷 변경하기

Sub FormatColumn()
   Dim iLastRow As Long
   Dim col As Range

   '마지막 행 찾기
   iLastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

   'B 컬럼의 데이터 포맷 변경
   Set col = Range("B2:B" & iLastRow)
   col.Select
   Selection.NumberFormat = "#,##0.00"
End Sub

4. 셀 색상 변경하기

Sub ColorCells()
   Dim iLastRow As Long, iLastCol As Long
   Dim rng As Range

   '마지막 행과 열 찾기
   iLastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
   iLastCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column

   '3행부터 10행까지 셀 색상 변경
728x90
반응형