1
這是我的場景。我想從Excel工作表「Sheet2」中的數據讀取值。我如何將特定單元格的值讀入可用於計算的變量等等?這是我的,但我沒有得到它的工作。我錯過了什麼嗎?值未正確返回
Private Sub CommandButton1_Click()
'Worksheets("Sheet2").Select
Range("A1").Select
Dim i, iHdr, iLine, sOutStr
Dim sDatum, sVerskaf As String
Dim sNr, sBoL, sPOOrder, sStockCode, sOrder, sVragbief, sNommer, sBondel, sLotNum, sKaartjie, sKode, sM3Hash, sStukke, sLengte, sBreedte, sDikte, sM3At, sM3, sEen
Dim sXML
i = 3
For Each sCellA In Sheets("Sheet2").Range("B" & i & ":B10")
i = i + 1
sDatum = Cells(i, 1).Value
sVerskaf = Cells(i, 2).Value
sNr = Cells(i, 3).Value
sBoL = Cells(i, 4).Value
sPOOrder = Cells(i, 5).Value
sStockCode = Cells(i, 6).Value
sXML = "<Info><Date>" & sDatum & "</Date>"
sXML = sXML & "<Supplier>" & sVerskaf & "</Supplier>"
sXML = sXML & "<Number>" & sNr & "</Number>"
sXML = sXML & "<BoL>" & sBoL & "</BoL>"
sXML = sXML & "<PurchaseOrder>" & sPOOrder & "</PurchaseOrder>"
sXML = sXML & "<StockCode>" & sStockCode & "</StockCode></Info>"
Next
'MsgBox sXML, vbInformation
End Sub
另一個愚蠢的問題:我是否需要將變量聲明爲String或將一個dim變量存儲任何值? –
假設您使用的是較新版本的vba,請從Excel 2007+中說出,您可以將變量聲明爲他們需要的任何類型。 VBA相當寬容,從範圍值到變量的隱式轉換 – fnostro
@ user3180965您可以將其聲明爲字符串或變體,但VBA程序員建議不要始終使用變體數據類型來捕獲意外的編程錯誤。 – L42