1. 程式人生 > >表格批量導入金蝶專業版銷售訂單

表格批量導入金蝶專業版銷售訂單

pen drive ali info 視圖 項目 base erp 人民幣

項目需求

客戶整理出一個excel表格,表格中有訂單必需的幾個字段。然後把表格批量到導入到金蝶的專業版訂單中。

如圖:

技術分享圖片

項目開發思路:用vba代碼把這個原表中需要的字段提取出來。然後生成到新的一張excel表格中,訂單需要的字段能固定的,都固定寫在生成導入表格裏,

不能固定的,用vba代碼寫一個窗體,菜單,讓客戶可以簡單選擇。

如圖:

技術分享圖片

項目開發過程及主要代碼:

Private Sub UserForm_Initialize()

TextBox1.Text = Format(Date, "yyyy-m-d")

End Sub


Private Sub CommandButton1_Click()
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strCn As String, strSQL As String ‘字符串變量
strCn = "Driver={sql server};" _
& "server=192.168.42.141;" _
& "uid=sa;pwd=sa123456;" _
& "database=AIS20181129203623;" _
& "AutoTranslate=False"

strSQL = "select * from zj1"

cn.Open strCn ‘打開連接
rs.Open strSQL, cn
Dim i As Integer, j As Integer, sht As Worksheet ‘i,j為整數變量;sht 為excel工作表對象變量,指向某一工作表

Set sht = ThisWorkbook.Worksheets("w1")
‘sht.Range("a1:a4") = rs("fexplanation")
‘循環讀取數據並將數據顯示到excel中
i = 1
Do While Not rs.EOF ‘當數據指針未移到記錄集末尾時,循環下列操作

i = i + 1
sht.Cells(i, 1) = rs("a1") ‘把當前記錄的字段1的值保存到sheet1工作表的第i行第1列
sht.Cells(i, 2) = rs("a2") ‘把當前字段2的值保存到sheet1工作表的第i行第2列
sht.Cells(i, 3) = rs("a3") ‘把當前字段2的值保存到sheet1工作表的第i行第2列
sht.Cells(i, 4) = rs("a4")
sht.Cells(i, 5) = rs("a5")
rs.MoveNext ‘把指針移向下一條記錄

‘i = i + 1 ‘i加1,準備把下一記錄相關字段的值保存到工作表的下一行

Loop ‘循環

rs.Close ‘關閉記錄集,至此,程序將把某數據表的字段1和字段2保存在excel工作表sheet1的第1、2列,行數等於數據表的記錄數
Sheet3.Cells(1, 1) = "單位組"
Sheet3.Cells(1, 2) = "單位"
Sheet3.Cells(1, 3) = "名稱"
Sheet3.Cells(1, 4) = "單價"
Sheet3.Cells(1, 5) = "代碼"
End Sub

Private Sub CommandButton4_Click()
Dim aa, ab As Integer
Dim a1, b1
‘循環匹配物料名稱和計量單位,單價
For aa = 2 To Sheet1.[b65536].End(3).Row
a1 = Sheet1.Cells(aa, 2).Value
For ab = 2 To Sheet3.[a65536].End(3).Row
b1 = Sheet3.Cells(ab, 3).Value
If b1 = a1 Then
Sheet2.Cells(aa, 12) = Sheet3.Cells(ab, 5)
Sheet2.Cells(aa, 16) = Sheet3.Cells(ab, 4)
Sheet2.Cells(aa, 15) = Sheet3.Cells(ab, 2)
End If
Next
Next
Dim d, t As String
c = Year(Now)
d = Format(Date, "md") ‘當前年月日
t = Format(Time, "hms") ‘當前時間
Sheet2.Range("b:b").NumberFormatLocal = "yyyy-m-d"
Sheet2.Range("f:f").NumberFormatLocal = "yyyy-m-d"
Sheet2.Range("t:t").NumberFormatLocal = "yyyy-m-d"
Dim a
For a = 2 To Sheet1.[b65536].End(3).Row
Sheet2.Cells(a, 1) = "SEOR" & c & d & t & a
Sheet2.Cells(a, 2) = TextBox1.Text
Sheet2.Cells(a, 3) = Sheet1.Cells(a, 4) & "/" & Sheet1.Cells(a, 5)
Sheet2.Cells(a, 4) = "人民幣"
Sheet2.Cells(a, 5) = "‘1"
Sheet2.Cells(a, 6) = TextBox1.Text
Sheet2.Cells(a, 7) = "賒銷"

Sheet2.Cells(a, 10) = "‘*"

Sheet2.Cells(a, 13) = Sheet1.Cells(a, 2)
‘Sheet2.Cells(a, 13) = Sheet1.Cells(a, 4)

Sheet2.Cells(a, 17) = Sheet1.Cells(a, 3)

Sheet2.Cells(a, 18) = "‘16"
Sheet2.Cells(a, 19) = "‘0"
Sheet2.Cells(a, 20) = TextBox1.Text

Next
End Sub

Private Sub CommandButton5_Click()
For Z = 2 To Sheet2.[b65536].End(3).Row

Next
Application.ScreenUpdating = False
Dim j As Integer
Dim souce As Worksheet
Dim target As Workbook
Set souce = ThisWorkbook.Worksheets("cg1")

Set target = Workbooks.Open(ThisWorkbook.Path & "\銷售.xls")
target.Sheets(1).Rows.Delete

souce.Range(souce.Cells(1, 1), souce.Cells(Z - 1, 20)).Copy target.Sheets(1).Range(target.Sheets(1).Cells(1, 1), target.Sheets(1).Cells(Z - 1, 20))


Application.ScreenUpdating = True
ActiveWorkbook.Save
ActiveWorkbook.Close


End Sub

通過以上代碼把原來的表格生成以下形式

技術分享圖片

用到的sql語句

建一個視圖,把物料名稱,代碼單位等篩選出來

CREATE VIEW zj1 AS
select t1.fname as a1 ,t2.fname as a2,t3. fname as a3 ,t3.FOrderPrice as a4 ,t3.FNumber as a5 from
t_unitgroup as t1 ,t_measureunit as t2 ,t_icitem as t3
where t1.funitgroupid =t3.funitgroupid and t2.fitemid=t3.funitid

查詢視圖看看效果

select* from zj1

如圖

技術分享圖片

最後生成的小程序有3個表組成

如圖:

技術分享圖片

技術分享圖片

技術分享圖片

表格批量導入金蝶專業版銷售訂單