zhuxuhong 发表于 2005-11-23 19:44:00

[VBA]批量替换图块

我在做一个块批量替换的程序。原先图中有二、三十种块,现在用其他的图块来替换,如GC120-->10211、GC119-->12111。这种对应关系我已经在ACCESS数据库中已经建立,现在就是在考虑程序运行效率的问题。我想了两种方法:
1)一次性将对应关系读入数组中保存,这样就只需访问数据库一次就行。替换时再从数组中找对应关系。
2)每次都从数据库中去找对应关系。
请问版主,上面两种方法,哪种应该会运行快点?还有没有其他更好的方法?

王咣生 发表于 2005-11-23 19:57:00

你可以考虑做成INI文件,格式

GC120=10211
GC119=12111
GC121=12112
GC122=12113
GC123=12114
然后用API函数读取.
VB读取INI文件程序:
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _
    (ByVal lpApplicationName As String, _
   ByVal lpKeyName As Any, ByVal lpDefault As String, _
   ByVal lpReturnedString As String, ByVal nSize As Long, _
   ByVal lpFileName As String) As Long
Public Function ReadIni(ByVal file As String, ByVal section As String, _
ByVal key As String, ByVal default As String) As String
    Dim x As Long
    Dim sSection As String, sEntry As String, sDefault As String
    Dim sRetBuf As String, iLenBuf As Integer, sFileName As String
   
    'Six arguments
    sSection$ = section
    sEntry$ = key
    sDefault$ = default
    sRetBuf$ = String$(256, 0)'256 null character
    iLenBuf% = Len(sRetBuf$)
    sFileName$ = file
   
    'function will return a value of 12 in this case
    x = GetPrivateProfileString(sSection$, sEntry$, sDefault$, sRetBuf$, iLenBuf%, sFileName$)
   
    'sValue$ will contain the string 'PACKARD BELL'
    sValue$ = Left$(sRetBuf$, x)
   
    'return value
    ReadIni = sValue$
   
End Function
Sub Test_ReadIni()
    MsgBox ReadIni("d:\test.ini", "TEST", "2", "null")
End Sub
文件d:\test.ini的内容:

1=hello
2=world
3=!
页: [1]
查看完整版本: [VBA]批量替换图块