初步瞭解vba。利用excel自動生成我想要的insert語句

Private Declare Function CoCreateGuid Lib "ole32" (id As Any) As Long
Sub zidong()

cellNum = Sheet1.[a65536].End(xlUp).Row

For i = 2 To cellNum
'開始創建貨品===================================================
'每次生成唯一id
tt = Timer
h = Int(tt / 3600)
m = Int((tt - 3600 * h) / 60)
s = Int(tt - h * 3600 - m * 60)
ss = Left(tt - Int(tt), 15)
ss = Replace(ss, ".", "")
ss = ss + "000"
id = Format(Date, "yyyymmdd") + Format(Time, "hhmmss") + Left(ss, 7) + Left(CreateGUID, 11)
'每次獲取創建時間
createtime = Format(Date, "yyyy-mm-dd") + Format(Time, " hh:mm:ss")
'開始寫insert語句
insertdate = "INSERT INTO PaasOLT_Goods VALUES("
'拼接id
insertdate = insertdate + "'" + id + "',"
'拼接時間
insertdate = insertdate + "'" + createtime + "',"
'修改時間同創建時間
insertdate = insertdate + "'" + createtime + "',"
'創建人同修改人
insertdate = insertdate + "'" + "SYSTEM" + "',"
insertdate = insertdate + "'" + "SYSTEM" + "',"
insertdate = insertdate + "" + "0" + ","
insertdate = insertdate + "'" + "" + "',"
'GoodsName
insertdate = insertdate + "'" + Sheet1.Cells(i, 1) + "',"
'GoodsPrice
insertdate = insertdate + "" + Str(Sheet1.Cells(i, 2)) + ","
'GoodsStock
insertdate = insertdate + "" + Str(Sheet1.Cells(i, 3)) + ","
'GoodsSpecification
insertdate = insertdate + "'" + Sheet1.Cells(i, 4) + "',"
'GoodsPhoto
insertdate = insertdate + "'" + "" + "',"
'GoodsDescription
insertdate = insertdate + "'" + Sheet1.Cells(i, 5) + "',"
'EnumGoodsType
insertdate = insertdate + "'" + "1" + "',"
'GoodsNum
insertdate = insertdate + "" + "NULL" + ","
'PK_PaasOLT_Warehouse_WarehouseName
insertdate = insertdate + "" + "NULL" + ","
'SalesPrice
insertdate = insertdate + "" + "NULL" + ","
'PK_PaasOLT_CommodityCategory_Name
insertdate = insertdate + "" + "NULL" + ","
'Remark
insertdate = insertdate + "" + "NULL" + ","
'GoodsUnit
insertdate = insertdate + "" + "NULL" + ","
'EnumGoodsSupplier
insertdate = insertdate + "" + "NULL"
'結束insert
insertdate = insertdate + ")" & Chr(10) & ""
'創建貨品結束=========================================================================
Sheet2.Cells(i, 1) = insertdate
'創建商品開始=========================================================================
'每次生成唯一id
tt = Timer
h = Int(tt / 3600)
m = Int((tt - 3600 * h) / 60)
s = Int(tt - h * 3600 - m * 60)
ss = Left(tt - Int(tt), 15)
ss = Replace(ss, ".", "")
ss = ss + "000"
Commodityid = Format(Date, "yyyymmdd") + Format(Time, "hhmmss") + Left(ss, 7) + Left(CreateGUID, 11)
'每次獲取創建時間
createtime = Format(Date, "yyyy-mm-dd") + Format(Time, " hh:mm:ss")
'開始寫insert語句
insertdate = "INSERT INTO PaasOLT_Commodity VALUES("
'拼接id
insertdate = insertdate + "'" + Commodityid + "',"
'拼接時間
insertdate = insertdate + "'" + createtime + "',"
'修改時間同創建時間
insertdate = insertdate + "'" + createtime + "',"
'創建人同修改人
insertdate = insertdate + "'" + "SYSTEM" + "',"
insertdate = insertdate + "'" + "SYSTEM" + "',"
insertdate = insertdate + "" + "0" + ","
insertdate = insertdate + "'" + "" + "',"
'CommodityName
insertdate = insertdate + "'" + Sheet1.Cells(i, 6) + "',"
'PK_PaasOLT_CommodityCategory_Name
insertdate = insertdate + "'" + Sheet1.Cells(i, 8) + "',"
'CommodityCode
insertdate = insertdate + "" + "NULL" + ","
'CommodityKeyWord
insertdate = insertdate + "" + "NULL" + ","
'CommodityDescription
insertdate = insertdate + "" + "NULL" + ","
'CommodityPhoto
insertdate = insertdate + "" + "NULL" + ","
'CommodityMoney
insertdate = insertdate + "" + Str(Sheet1.Cells(i, 7)) + ","
'CommoditySalesVolume
insertdate = insertdate + "" + "0" + ","
'CommodityStock
insertdate = insertdate + "" + Str(Sheet1.Cells(i, 3)) + ","
'EnumCommodityStatus
insertdate = insertdate + "'" + "10" + "',"
'SwitchBtnJoinDiscount
insertdate = insertdate + "'" + "1" + "',"
'CommodityTag
insertdate = insertdate + "" + "NULL" + ","
'EnumCommodityType
insertdate = insertdate + "'" + "1" + "',"
'PK_PaasOLT_CarType_Name
insertdate = insertdate + "" + "NULL" + ","
'PreBookingDay
insertdate = insertdate + "" + "0" + ","
'Remark
insertdate = insertdate + "'" + Sheet1.Cells(i, 9) + "'"
'結束insert
insertdate = insertdate + ")" & Chr(10) & ""
'創建商品結束=========================================================================
Sheet2.Cells(i, 2) = insertdate
'創建商品貨品關係開始=================================================================
'每次生成唯一id
tt = Timer
h = Int(tt / 3600)
m = Int((tt - 3600 * h) / 60)
s = Int(tt - h * 3600 - m * 60)
ss = Left(tt - Int(tt), 15)
ss = Replace(ss, ".", "")
ss = ss + "000"
CommodityGoodsid = Format(Date, "yyyymmdd") + Format(Time, "hhmmss") + Left(ss, 7) + Left(CreateGUID, 11)
'每次獲取創建時間
createtime = Format(Date, "yyyy-mm-dd") + Format(Time, " hh:mm:ss")
'開始寫insert語句
insertdate = "INSERT INTO PaasOLT_CommodityGoods VALUES("
'拼接id
insertdate = insertdate + "'" + CommodityGoodsid + "',"
'拼接時間
insertdate = insertdate + "'" + createtime + "',"
'修改時間同創建時間
insertdate = insertdate + "'" + createtime + "',"
'創建人同修改人
insertdate = insertdate + "'" + "SYSTEM" + "',"
insertdate = insertdate + "'" + "SYSTEM" + "',"
insertdate = insertdate + "" + "0" + ","
insertdate = insertdate + "'" + "" + "',"
'商品主鍵
insertdate = insertdate + "'" + Commodityid + "',"
'貨品主鍵
insertdate = insertdate + "'" + id + "',"
'貨品數量
insertdate = insertdate + "" + "1"
'結束insert
insertdate = insertdate + ")" & Chr(10) & ""
'創建商品結束=========================================================================
Sheet2.Cells(i, 3) = insertdate
Next


MsgBox 123
End Sub

Private Function CreateGUID() As String

    Dim id(0 To 15) As Byte
      
    Dim Cnt As Long, GUID As String
      
    If CoCreateGuid(id(0)) = 0 Then
          
        For Cnt = 0 To 15
            CreateGUID = CreateGUID + IIf(id(Cnt) < 16, "0", "") + LCase(Hex$(id(Cnt)))
        Next Cnt
          
        CreateGUID = Left$(CreateGUID, 8) + Mid$(CreateGUID, 9, 4) + Mid$(CreateGUID, 13, 4) + Mid$(CreateGUID, 17, 4) + Right$(CreateGUID, 12)
      
    Else
          
        MsgBox "Error while creating GUID!"
          
    End If
      
End Function



 

效果圖

第一次用宏。還挺不錯

 

——————————————————————————————————

作者:Henny_CHN

轉載請標明出處,原文地址:  

https://blog.csdn.net/a1234012340a/article/details/101384650

如果感覺本文對您有幫助,請留下您的贊,您的支持是我堅持寫作最大的動力,謝謝!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章