import/export data using vba · 2014-12-03 · referencing excel cells in vba vba...

35
Import/Export Data Using VBA

Upload: others

Post on 31-Dec-2019

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Import/Export Data Using VBA

Page 2: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Objectives

Referencing Excel Cells in VBA

Importing Data from Excel to VBA

Using VBA to Modify Contents of Cells

새 서브 프로시저 작성하기

프로시저 실행하고 결과 확인하기

VBA 코드 이해하기

Page 3: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Referencing Excel Cells in VBA

Page 4: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Referencing Excel Cells in VBA

엑셀창에서 Alt+F11 단축기Visual Basic 편집기 창호출.

Page 5: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Referencing Excel Cells in VBA

1

2

3 모듈 개체 추가

모듈개체 추가하는 이유•전체 개체에서 사용 가능한여러 가지 선언과 정의 작업이 가능하기 때문에

Page 6: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Referencing Excel Cells in VBA

새 서브 프로시저 작성하기

Page 7: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Referencing Excel Cells in VBA

프로시저 실행하고 결과 확인하기

F5 누르기1

2

Page 8: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Referencing Excel Cells in VBA

프로시저 실행하고 결과 확인하기

Page 9: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Referencing Excel Cells in VBA

VBA 코드 이해하기

Option Explicit 명령변수 선언 요청 명령. ‘변수 선언 요구’에 체크 표시한 경우 나타남 변수와 상수의 선언선언문 변수(상수)명 As 데이터형식•선언문⇒ 변수인 경우 Dim문, 상수인 경우 Const문 사용•데이터 형식

자료형 이름 범위 사이즈(바이트)

바이트형 Byte 0~255 1정수형 Integer -32768~32767 2

긴 정수형 Long -2147483648~2147483647 4단정도부동소수

점수형Single 4

배정도부동소수점수형

Double 8

4538 104.1~104.3 −×±×±

324308 109.4~108.1 −×±×±

Page 10: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Referencing Excel Cells in VBA

VBA 코드 이해하기

서브 프로시저(Sub Procedure) 구성Sub 프로시저명( )

실행명령End Sub

a = Range(“A5”). Value

Range 개체는 셀 범위를 나타내는 개체. A5 cell 의미

속성명

MsgBox (“Value in A5”) & aMsgbox 함수는 사용자에게

메시지 창을 표시할 때 사용Msbbox(메시지 내용)

지정한 a의 값

Page 11: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Referencing Excel Cells in VBA

프로시저 실행하고 결과 확인하기

b=25Range(“A6”). Value=b

A6 cell에 25(b)넣기

VBA 코드 이해하기

Page 12: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Referencing Excel Cells in VBA

프로시저 실행하고 결과 확인하기

Page 13: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Referencing Excel Cells in VBA

VBA 코드 이해하기

c=InputBox (“Enter a number to be placed in cell a7:”)

InputBox 함수: 사용자의 입력 값을 받아 처리하고자 할 때 사용. MsgBox 함수: 사용자에게 전달한 내용을 메시지창에 표시.

특정상황에 사용자의 선택이 필요한 경우 사용.

InputBox 함수 MsgBox 함수

Page 14: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Referencing Excel Cells in VBA

1

24 F8 키 누르기

‘호출스택표식’아이콘코드가 중단된 위치 표시

3

Page 15: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Importing Data from Excel to VBA

Page 16: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

1

2

Importing Data from Excel to VBA

3 모듈 개체 추가

Page 17: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Importing Data from Excel to VBA

새 서브 프로시저 작성하기

Page 18: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Importing Data from Excel to VBA

프로시저 실행하고 결과 확인하기

생성

Page 19: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Importing Data from Excel to VBA

VBA 코드 이해하기

a= Range(“A1:C5”)A1에서 C5까지

a로 지정

Range(“D5:F7”).Value=aD5에서 F7까지 a의 값이 입력

Page 20: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Using VBA to Modify Contents of Cells

Page 21: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Referencing Excel Cells in VBA

엑셀창에서 Alt+F11 단축기Visual Basic 편집기 창호출.

Page 22: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

1

2

3 모듈 개체 추가

Using VBA to Modify Contents of Cells

Page 23: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Using VBA to Modify Contents of Cells

새 서브 프로시저 작성하기

Page 24: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Using VBA to Modify Contents of Cells

프로시저 실행하고 결과 확인하기

Page 25: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Using VBA to Modify Contents of Cells

VBA 코드 이해하기

For Each ~ Next 구문이란?

For Each~Next

For Each C In WorkSheets

Next

처리

개체를 저장할 변수 지정 개체의 집합 지정어떤 개체의 집합이 있고, 그 안의 모든 요소에대해 처리를 수행할 때For Each~Next구문 사용

Page 26: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Using VBA to Modify Contents of Cells

For Each 개체 변수 In 컬렉션반복해서 실행할 명령(주로 개체 변수에서 작업할 내용이 코드로 입력)

Next

For each c in worksheets("sheet1").range("A1:C3")⇒ c라는 워크시트1 개체 변수를 선언하는데 이것은 A1에서 C3까지의 값.

c.Value = c.Value+5변수 값

대입

VBA 코드 이해하기

Page 27: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

2

3

1

45

6

Using VBA to Modify Contents of Cells

Page 28: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Using VBA to Modify Contents of Cells

2

1 드래그

클릭!!

Page 29: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Using VBA to Modify Contents of Cells

Page 30: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Using VBA to Modify Contents of Cells

VBA 코드 이해하기

If ~ Then구문이란?

If ~ Then

If 조건 Then

End If

처리(1)

조건이 성립할때( 조건식이 참)는 처리(1)을 수행. 성립하지 않은 경우 아무것도수행하지 않음.

거짓

Page 31: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Using VBA to Modify Contents of Cells

IF c.Value =15c.Value = 9999변수 값

대입

VBA 코드 이해하기

c.Value가 15인 경우 9999대입.조건이 성립하지 않는 경우는 그대로 두기.

Page 32: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Summary

Page 33: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Summary

Referencing Excel Cells in VBA

a = (“A5”). Value

Range 개체는 셀 범위를 나타내는 개체. A5 cell 의미

속성명

(“Value in A5”) & aMsgbox 함수는 사용자에게

메시지 창을 표시할 때 사용Msgbox(메시지 내용)

지정한 a의 값

함수: 사용자의 입력 값을 받아 처리하고자 할 때 사용. MsgBox 함수: 사용자에게 전달한 내용을 메시지창에 표시.

특정상황에 사용자의 선택이 필요한 경우 사용.

Range?

MsgBox?

InputBox?

Page 34: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Summary

Importing Data from Excel to VBA

a= Range A1에서 C5까지

a로 지정

Range(“D5:F7”).Value=aD5에서 F7까지 a의 값이 입력

?(“A1:C5”)

Page 35: Import/Export Data Using VBA · 2014-12-03 · Referencing Excel Cells in VBA VBA 코드이해하기 Option Explicit 명령 변수선언요청명령. ‘변수선언요구’에체크표시한경우나타남

Summary

If 조건 Then

End If

처리(1)

For Each C In WorkSheets

Next

처리

개체를 저장할 변수 지정 개체의 집합 지정

Using VBA to Modify Contents of CellsIf~ Then

For Each~Next

?

?