这是我的情景。我想从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
答案 0 :(得分:1)
一些事情:
您不能使用For Each
之类的。 For Each
遍历一系列对象而不需要索引值(如i
),常规For循环用于使用Sheet.Cells(i,j)
选项迭代单元格
由于您引用了一列值,您甚至根本不需要for循环,只需直接访问单元格Sheets("Sheet2").Cells(3,1)
或ActiveSheet.Cells(3,2)
等。
最后,您无需将这些值分配给变量,您可以直接在xml格式中使用单元格值:sXML = "<Info><Date>" & Sheets("Sheet2").Cells(3,1).Value & "</Date>"