|
下面的语句是我的一个文件的功能,类似你的要求,可以参考。
ACCESS数据库:PIM_Base.mdb放在公共盘,国别码清单放在数据库的CO_NO表内
选择EXCEL文件的 Sh_Input表后,在单元格J1内,自动按数据库CO_NO表内的国别清单填写序列,ACCESS数据库更新,单元格序列就自动更新。
Private Sub Worksheet_Activate()
Dim iArr_Data()
myDatabase = "PIM_Base.mdb"
Open_AccessData
With Sh_Input
.AutoFilterMode = False
.Range("A7:K7").AutoFilter
For x = 2 To 10
If .Cells(1048576, x).End(xlUp).Row >= iRow_Data Then iRow_Data = .Cells(1048576, x).End(xlUp).Row
Next
iCO_All = .Range("D6").Value
If iRow_Data > 7 Then
niArr_Data = iRow_Data - 7
iArr_Data() = .Range("J8:K" & iRow_Data)
End If
.Range("A" & iRow_Data + 1 & ":K1048576").Clear
iList_Country = Empty
'//=====提取国别代码资料=====//
strSQL = "Select CO_NO,CO_Name From List_Country Order By CO_NO"
rstAnswers.Open strSQL, cnnAccess
Do Until rstAnswers.EOF = True
If iList_Country = Empty Then iList_Country = rstAnswers(0) & "_" & rstAnswers(1) Else iList_Country = iList_Country & "," & rstAnswers(0) & "_" & rstAnswers(1)
rstAnswers.Movenext
Loop
rstAnswers.Close
'//====================================//
With .Range("J1").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=iList_Country
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = "错误信息"
.InputMessage = ""
.ErrorMessage = "请选择正确的目的国别!"
.IMEMode = xlIMEModeNoControl
.ShowInput = True
.ShowError = True
End With
.Range("J1").Copy .Range("D6")
.Range("D6").Value2 = iCO_All
If niArr_Data <> 0 Then
.Range("J1").Copy .Range("J8:J" & iRow_Data)
.Range("J8:J8").Resize(niArr_Data) = iArr_Data()
Else
.Range("A1:K1").Copy .Range("A8:K107")
End If
iCustomer = .Range("D3").Value
iList_Customer = Empty
'//=====提取客户资料=====//
strSQL = "Select Customer_SN & '.' & Customer_Code & '_' & Customer_Name From List_Customer Order By Customer_SN"
rstAnswers.Open strSQL, cnnAccess
Do Until rstAnswers.EOF = True
If iList_Customer = Empty Then iList_Customer = rstAnswers(0) Else iList_Customer = iList_Customer & "," & rstAnswers(0)
rstAnswers.Movenext
Loop
rstAnswers.Close
'//====================================//
Set rstAnswers = Nothing
Set cnnAccess = Nothing
With .Range("D3").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=iList_Customer
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = "错误信息!"
.InputMessage = ""
.ErrorMessage = "请选择正确的贸易商名称!"
.IMEMode = xlIMEModeNoControl
.ShowInput = True
.ShowError = True
End With
.Range("D3").Value2 = iCustomer
.Range("B6").Copy
.Range("D6").PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With
Erase iArr_Data()
End Sub
|
|