我正在使用 Excel 表格通過 WhatsApp 自動執行訂單。
我的編程經驗很簡單,所以我使用教程和其他堆疊溢位執行緒來獲得解決方案。我有一些可行的方法,但即使專案數量單元格為空,這些腳本也會發送完整串列,這對我不起作用。
據我了解,我需要一個 If Else 陳述句來執行此操作,但我不知道在哪里放置它。
目標是如果列中的單元格為空,則跳過該行。我怎樣才能做到這一點?
下面是打開瀏覽器并發送訊息的腳本。
Sub WebWhatsApp()
Dim pop As Range
Dim BOT As New WebDriver
Dim KS As New Keys
Dim count_row As Integer
count_row = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
Dim rng As Range
Set rng = Sheets("Interface").Range(Cells(12, 1), Cells(count_row, 5))
Dim myString As String
myString = Rang2String(rng)
BOT.Start "chrome", "https://web.whatsapp.com/"
BOT.Get "/"
MsgBox _
"Please scan the QR code." & _
"After you are logged in, please confirm this message box by clicking 'ok'", vbOKOnly, "WhatsApp Bot"
searchtext = Sheets("Interface").Range(Cells(5, 8), Cells(5, 8))
textmessage = myString
BOT.FindElementByXPath("//*[@id='side']/div[1]/div/label/div/div[2]").Click
BOT.Wait (500)
BOT.SendKeys (searchtext)
BOT.Wait (500)
BOT.SendKeys (KS.Enter)
BOT.Wait (500)
BOT.SendKeys (textmessage)
BOT.Wait (1000)
BOT.SendKeys (KS.Enter)
MsgBox "Done."
End Sub
這是將 Excel 作業表的范圍轉換為主腳本作為文本訊息發送的字串的腳本。
Function Rang2String(rng As Range) As String
Dim strng As String
Dim myRow As Range
Dim KS As New Keys
With rng
For Each myRow In .Rows
strng = strng & Join(Application.Transpose(Application.Transpose(myRow.Value)), " | ") & vbNewLine
Next
End With
Rang2String = Left(strng, Len(strng) - 1)
End Function
我意識到答案可能非常明顯,但我似乎看不到解決方案。
提前致謝。
uj5u.com熱心網友回復:
Option Explicit
Function Rang2String(rng As Range) As String
Const COL_QU = "D" ' quantity column
Dim e As String, myrow As Range
With Application
For Each myrow In rng.Rows
If Len(myrow.Cells(1, COL_QU)) > 0 Then
Rang2String = Rang2String & e & Join(.Transpose(.Transpose(myrow)), " | ")
e = vbNewLine
End If
Next
End With
End Function
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/405366.html
標籤:
下一篇:如何使用路徑檔案打開作業簿?
