Page 1 of 1

ขออนุญาตถามกรณีปัญหาเขียน code เชื่อมต่อจาก workbook ไป google sheet

Posted: Sun Mar 10, 2024 9:13 pm
by 9KiTTi
ขออนุญาตสอบถามกรณีผมเขียน code เพื่อให้ทำงานตอนเปิดไฟล์ excel ให้ตรวจสอบกับช่อง A1 ใน workbook ชื่อชีท Main ถ้ามีค่าน้อยกว่า ในช่อง A1 ของไฟล์ google sheet ชื่อชีท version ให้แสดงข้อความแจ้งเตือน แต่ติด error ตัวนี้ครับ Compile Error: Expected Array ขอคำแนะนำด้วยครับ ขอบพระคุณครับ

Code: Select all

Sub Checkver()
    Dim ws As Worksheet
    Dim ul As String
    Dim ky As String

    ky = "1uAFl3HGeXzyS0vCR9shHQCoDYYMmsHfdktM3FNIdm9E"
    ul = "https://spreadsheets.google.com/tq?tqx=out:html&key=" & ky

    If ws("Main").Range("A1").Value < ul("Version").Range("A1") Then
        MsgBox "ข้อมูลตรง"
    Else
        MsgBox "กรุณาปรับข้อมูลให้ตรงกัน!!!"
    End If
End Sub

'https://docs.google.com/spreadsheets/d/1uAFl3HGeXzyS0vCR9shHQCoDYYMmsHfdktM3FNIdm9E/edit?usp=sharing

Re: ขออนุญาตถามกรณีปัญหาเขียน code เชื่อมต่อจาก workbook ไป google sheet

Posted: Mon Mar 11, 2024 8:09 am
by snasui
:D ตัวอย่างการ Query ข้อมูลจาก Google Sheet มาเทียบกับค่าเป้าหมายใน Excel ครับ

Code: Select all

Sub Import_Sheets_to_Excel()
    Dim QRT As QueryTable, ul As String, ky As String
    With Worksheets("Main")
        If .QueryTables.Count > 0 Then
            .QueryTables(1).Delete
            .Range("b1:b2").ClearContents
        End If
        ky = "1uAFl3HGeXzyS0vCR9shHQCoDYYMmsHfdktM3FNIdm9E"
        ul = "https://spreadsheets.google.com/tq?tqx=out:html&key=" & ky
        Set QRT = .QueryTables.Add(Connection:="URL;" & ul, Destination:=.Range("B1"))
        QRT.Name = "GoogleSheetData"
        QRT.FieldNames = True
        QRT.RowNumbers = False
        QRT.FillAdjacentFormulas = False
        QRT.PreserveFormatting = True
        QRT.RefreshOnFileOpen = False
        QRT.BackgroundQuery = True
        QRT.RefreshStyle = xlInsertDeleteCells
        QRT.SavePassword = False
        QRT.SaveData = True
        QRT.AdjustColumnWidth = True
        QRT.RefreshPeriod = 0
        QRT.Refresh BackgroundQuery:=False
        If .Range("A1").Value < .Range("b2") Then
            MsgBox "ข้อมูลตรงกัน", vbExclamation
        Else
            MsgBox "กรุณาปรับข้อมูลให้ตรงกัน!!!", vbExclamation
        End If
    End With
End Sub

Re: ขออนุญาตถามกรณีปัญหาเขียน code เชื่อมต่อจาก workbook ไป google sheet

Posted: Mon Mar 11, 2024 10:32 am
by 9KiTTi
snasui wrote: Mon Mar 11, 2024 8:09 am :D ตัวอย่างการ Query ข้อมูลจาก Google Sheet มาเทียบกับค่าเป้าหมายใน Excel ครับ

Code: Select all

Sub Import_Sheets_to_Excel()
    Dim QRT As QueryTable, ul As String, ky As String
    With Worksheets("Main")
        If .QueryTables.Count > 0 Then
            .QueryTables(1).Delete
            .Range("b1:b2").ClearContents
        End If
        ky = "1uAFl3HGeXzyS0vCR9shHQCoDYYMmsHfdktM3FNIdm9E"
        ul = "https://spreadsheets.google.com/tq?tqx=out:html&key=" & ky
        Set QRT = .QueryTables.Add(Connection:="URL;" & ul, Destination:=.Range("B1"))
        QRT.Name = "GoogleSheetData"
        QRT.FieldNames = True
        QRT.RowNumbers = False
        QRT.FillAdjacentFormulas = False
        QRT.PreserveFormatting = True
        QRT.RefreshOnFileOpen = False
        QRT.BackgroundQuery = True
        QRT.RefreshStyle = xlInsertDeleteCells
        QRT.SavePassword = False
        QRT.SaveData = True
        QRT.AdjustColumnWidth = True
        QRT.RefreshPeriod = 0
        QRT.Refresh BackgroundQuery:=False
        If .Range("A1").Value < .Range("b2") Then
            MsgBox "ข้อมูลตรงกัน", vbExclamation
        Else
            MsgBox "กรุณาปรับข้อมูลให้ตรงกัน!!!", vbExclamation
        End If
    End With
End Sub
ทดสอบแล้วครับ เป็นการ copy มาใส่ครับอาจารย์ แล้วจะขึ้นข้อความว่า ข้อมูลตรงกัน ไม่ว่าจะเปลี่ยนค่าที่สมุดงานไหน หลังจากรัน code ค่าจะเปลี่ยนทั้ง 2 สมุดงานครับ ไม่ขึ้นข้อความตามเงื่อนไขที่ต้องการครับ

Re: ขออนุญาตถามกรณีปัญหาเขียน code เชื่อมต่อจาก workbook ไป google sheet

Posted: Mon Mar 11, 2024 10:56 am
by snasui
:D การเช็คค่าตรง ๆ แบบนั้นไม่ได้เขียนสั้น ๆ แบบที่เขียนมาได้ครับ วิธีที่ผมทำตัวอย่างไว้นี้เป็นการทำให้สามารถทำงานต่อได้ หากพบว่าวิธีเดิมทำได้แบบง่าย ๆ รบกวนนำมาแชร์ด้วยครับ

ส่วนที่ขึ้นว่าข้อมูลตรงกันนั้น จำเป็นต้องตรวจสอบเงื่อนไขว่าเขียนเอาไว้อย่างไร ในเซลล์ A1 มีค่าอะไรอยู่ก่อนแล้วหรือไม่ครับ

Re: ขออนุญาตถามกรณีปัญหาเขียน code เชื่อมต่อจาก workbook ไป google sheet

Posted: Mon Mar 11, 2024 8:32 pm
by 9KiTTi
ขอบพระคุณครับอาจารย์