728x90

UserForm 을 이용하여 만들어서 사용하면 좀 더 편리하다.

여러가지 경우의 수를 IF문으로 라디오버튼과 CommandButton 을 이용해서 하면 편리하다.


그냥 module 할 때에는 좀 지저분하기도 한데 UserForm 으로 만들어서 하면 UserForm 만 백업받아두면 상당히 편리하다.

엑셀의 셀값을 넘길때 알아야 할 사항은

숫자는 uid =" & C.Text & "

문자열은 subject =""" & C.Offset(, 4).Text & """  또는 subject ='" & C.Offset(, 4).Text & "'

strSQL = strSQL & " 이름 Like '%" & T & "%' "

값을 넘기는 따옴표, 이중따옴표를 주의하면 실수를 하지 않는다.

초보자 입장에서는 이 부분의 실수가 가장 크다.


Private Sub CommandButton1_Click()
'// 도구->참조에 Microsoft ActiveX data object Library 2.8를 체크해야 함
    Dim MySQLconn As New ADODB.Connection
    Dim strDBconn As String
    Dim DBtblName As String
    Dim shtName As Variant
   
    Dim server_name As Variant
    Dim user_id As Variant
    Dim DB_pass As Variant
    Dim database_name As Variant

    Dim sSQL As String                '// MySQL Query 문 변수
    Dim i As Long, dbRow As Long, n As Long
   
    Application.ScreenUpdating = False  '// 화면 갱신 정지
    Application.DisplayAlerts = False

    Set shtName = Worksheets("DB_Setting")   '// DB_Setting Sheet 에서 설정한 값을 가져온다
    Set server_name = shtName.Range("A2")     '// IP  설정 값
    Set DB_port = shtName.Range("B2")   '// PORT  설정 값
    Set user_id = shtName.Range("C2")   '// User  설정 값
    Set DB_pass = shtName.Range("D2")   '// PASS  설정 값
    Set database_name = shtName.Range("E2")   '// DB  설정 값

    '// SQL 문 작성
    Dim C, rngAll As Range
    Dim sRow, eRow As Long    '// 시작할 행의 변수
    Dim cnt%
    Dim myValue As String
    Dim v
   
    Application.DisplayStatusBar = True
    cnt = Selection.Rows.Count
    sRow = Selection.Row
    myValue = sRow & "/" & sRow + cnt - 1
    v = InputBox("시작할 행의 수를 입력하세요", , myValue)
    If InStr(v, "/") > 0 Then
        sRow = Trim(Split(v, "/")(0))
        eRow = Trim(Split(v, "/")(1))  '// 마지막 행
    Else
        sRow = v
    End If
    If sRow = vbNullString Then Exit Sub           '// 취소 선택시 매크로 중단
    If sRow <= 2 Then sRow = 2
    If Not IsNumeric(sRow) Then Exit Sub '// 입력한 값이 숫자가 아닌 경우 매크로 중단
   
    If eRow Then
        Set rngAll = Range(Cells(sRow, "E"), Cells(eRow, "E"))  '// UID 값이 있는 열을 지정
    Else
        Set rngAll = Range(Cells(sRow, "E"), Cells(Rows.Count, "E").End(3))
    End If

    Set MySQLconn = New ADODB.Connection
    strDBconn = "DRIVER={MySQL ODBC 5.1 Driver};SERVER=" & server_name & ";PORT=" & DB_port & ";DATABASE=" & database_name & ";USER=" & user_id & ";PASSWORD=" & DB_pass & ";OPTION=3;"
    MySQLconn.Open strDBconn   '// Open the connection
   
    For Each C In rngAll
        Application.StatusBar = "셀: " & C.Address(0, 0) & " / " & C.Text & " 진행중..."
        If IsNumeric(C) Then    '// 숫자이면
            If OptionButton1.Value = True Then     
                sSQL = "UPDATE data SET is_checking=8 Where uid =" & C.Text & ""
                Range(Cells(C.Row, "A"), Cells(C.Row, "B")).Interior.ColorIndex = 33
            ElseIf OptionButton2.Value = True Then 
                sSQL = "UPDATE data SET is_checking=7 Where uid =" & C.Text & ""
                Range(Cells(C.Row, "A"), Cells(C.Row, "B")).Interior.ColorIndex = 33
            ElseIf OptionButton3.Value = True Then 
                sSQL = "UPDATE data SET is_checking=5 Where uid =" & C.Text & ""
                Range(Cells(C.Row, "A"), Cells(C.Row, "B")).Interior.ColorIndex = 43
            ElseIf OptionButton4.Value = True Then  '
                sSQL = "UPDATE data SET is_checking=0 Where uid =" & C.Text & ""
                Range(Cells(C.Row, "A"), Cells(C.Row, "B")).Interior.ColorIndex = xlNone
            ElseIf OptionButton5.Value = True Then
                sSQL = "UPDATE data SET hidden=1 Where uid =" & C.Text & ""
            ElseIf OptionButton9.Value = True Then
                sSQL = "UPDATE data SET sex=0, role="""" Where uid =" & C.Text & ""
            ElseIf OptionButton10.Value = True Then
                sSQL = "UPDATE data SET hidden=0 Where uid =" & C.Text & ""
            End If
           
            MySQLconn.Execute sSQL
        End If
    Next C

    MySQLconn.Close
    Set MySQLconn = Nothing
    Application.StatusBar = False
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    OptionButton8 = True    '// 최초실행시 정상 기본값
    MsgBox "완료!!", 64, ThisWorkbook.Name
End Sub


블로그 이미지

Link2Me

,