Excel VBA

[Excel 파일저장]

1
2
3
4
' excel 파일 저장
ActiveWorkbook.SaveAs "주소\파일명"61 '.xlsx파일로저장
ActiveWorkbook.ExportAsFixedFormat xlTypePDF, "주소\파일명"  '.pdf파일로저장
 
cs

엑셀 파일 저장포맷 참고 : https://docs.microsoft.com/en-us/office/vba/api/excel.xlfileformat


[Excel 문자열 포맷지정]

1
2
3
4
5
'Format 지정
Format("문자열""포멧형식")
Format(TextBox2.Value, "yyyy년 mm월 dd일")
Format(TextBox3.Value, "#,###.0000")
Format(2019-05-01"mmmm dd, yyyy"'May 01, 2019
cs

참고 : https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/format-function-visual-basic-for-applications


[변수 Array로 선언]

1
2
3
'Array 선언
Public A(1 to 19As Variant
 
cs


[For문]

1
2
3
4
'For문
For i = 0 to 2
실행문
Next
cs

[if문]

1
2
3
4
5
6
7
'if문
if 조건 Then
    실행문
ElseIf 조건 Then
    실행문
End If
 
cs

[with문]

1
2
3
4
5
6
7
8
'with문
'반복되는 개체를 한번만 써서 사용가능
With combobox1
    .AddItem "S&P500"
    .AddItem "Eurostoxx50"
    .AddItem "Nikkei225"
End with
 
cs


[Do While문]

1
2
3
4
5
6
7
8
9
'Do While 문
Do While 조건
    실행문
Loop
 
Do While Selection.Text = B(i)
    Selection.Text = A(i)
    Call findmethod
Loop
cs


[For each문]

1
2
3
4
5
'For Each 문
For Each s in ActiveDocument.Shapes 'word 문서상의 모든 도형에 접근
    s.Select
    Call findmethod
Next
cs

[Call]

1
2
3
'Call
'프로시져 실행
Call 프로시져명
cs


[특정시간마다 프로시져 반복실행]

1
2
3
4
5
'특정시간(OnTime)에 프로시져 반복실행
Application.OnTime Now + 반복시간, "프로시져명"
 
timeInterval = TimeValue("00:00:05")                '5초마다 실행
Application.OnTime Now + timeInterval, "works_To_Do"
cs

[엑셀파일 열기]

1
2
3
 
'엑셀파일 열기
Workbooks.Open("주소\파일명")
cs


[워드파일 열기]

1
2
3
4
5
6
7
'word파일 열기
'vbe창에서 도구-참조에서 Microsoft Word 16.0 Object Library 체크해야함
Dim wApp As Word.Application
Dim wDoc As Word.Document
Set wApp = CreateObject("word.application")
wApp.Visible = True
Set wDoc = wApp.Documents.Open("주소\파일명")
cs


[폴더 열기]

1
2
'폴더 열기
Activeworkbook.FollowHyperlink Address:="주소", NewWindow:=True
cs


Word VBA 관련


[워드파일 저장]

1
2
3
'word파일 저장
ActiveDocument.SaveAs2 "주소\파일명", FileFormat:=16 '.docx파일로 저장
ActiveDocument.SaveAs2 "주소\파일명", wdFormatPDF '.pdf파일로 저장
cs


[워드 표 접근]

1
2
3
4
 
'word 표 접근
ActiveDocument.Tables(표번호).Cells(행,열).select
ActiveDocument.Tables(7).Cells(4,2).Tables(1).Select ' Table안에 Table있음
cs


Excel VBA Form 관련

1
2
3
4
5
6
7
8
'form 숨기기(끄기)
폼명.Hide
UserForm1.Hide
 
 
 
'UserForm 관련
ComboBox1.AddItem "KOSPI200"
cs


+ Recent posts