2015-02-06 121 views
0

起初,我可能会阻止在使用英语写作时犯错误,因为它不是我的第一语言。Excel VBA - 从文本文件中导入SQL语句时使用替换

我正在使用Excel VBA和SQL学习几件事情,以使我的工作更轻松。每季度,我必须制作一份包含大约60张表格的报告。所以我正在编写一个程序,该程序将帮助我从Access数据库获取一些数据,并将其导入新的Excel工作文件中,并以更好的方式将其格式化以供发布。 对此,我使用了一个ADO连接,然后在每个表格的主Excel文件中创建一个电子表格,并将该表格填充到一个记录集中。

为了使调试更容易,我选择将每个SQL语句放在一个文本文件中。我已经构建了一个读取该文件内容的函数。我还用关键字“STR1”和“STR2”来填充每条SQL语句的“WHERE”子句,使用用户在启动我的VBA程序时输入的一些参数。 为此,我在阅读我的文本文件并将其导入字符串变量中后使用REPLACE。

我的问题是,对于某些文件它使替换和一切工​​作正常(一个新文件被创建并包含我的记录集),但对于我的一些文本文件,REPLACE不会替换我的关键字。

这是我的一段代码,只是我认为需要清理的部分。无法发布全部,因为它包含一些收藏和类模块:

''' Version du programme ou on crée tous les fichiers dans form_2 et où on les exporte après/ version of my program where I create all Tables im my main file containing the code, export them in a new workbook and delete them 

'Déclaration initiale/ Init of variables 

Dim strMyPath As String, strDBName As String, strDB As String 
Dim rs As New ADODB.Recordset 
Dim db2 As New ADODB.Connection 

'Appel de xx pour remplir anmois/ calling fct_anmois to fill my where conditions with the 5 couples years-quarter 

Call fct_anmois(InputBox("Entre une valeur année", "Année"), InputBox("Entre une valeur codée mois: 21- 1er Trimestre; 22- 2ème Trimestre; 23- 3ème Trimestre; 24- 4ème Trimestre")) 


'Quatres premiers Tableaux/ 4 1st Tables 

' A entrer avec des parametres input utilisateurs que je définirai après: voir ma collection AnTrimes/ 

Dim STR1 As String 

STR1 = "((Base1.Annee)=" & anmois.item(5).Ann & ") And ((Base1.Mois)=" & Chr$(34) & anmois.item(5).Trime & Chr$(34) & ")" 

Dim STR2 As String 
STR2 = "((Base1.Annee)=" & anmois.item(1).Ann & " and (Base1.Mois)=" & Chr$(34) & anmois.item(1).Trime & Chr$(34) & ") or " & _ 
     "((Base1.Annee)=" & anmois.item(2).Ann & " and (Base1.Mois)=" & Chr$(34) & anmois.item(2).Trime & Chr$(34) & ") or " & _ 
     "((Base1.Annee)=" & anmois.item(3).Ann & " and (Base1.Mois)=" & Chr$(34) & anmois.item(3).Trime & Chr$(34) & ") or " & _ 
     "((Base1.Annee)=" & anmois.item(4).Ann & " and (Base1.Mois)=" & Chr$(34) & anmois.item(4).Trime & Chr$(34) & ") or " & _ 
     "((Base1.Annee)=" & anmois.item(5).Ann & " and (Base1.Mois)=" & Chr$(34) & anmois.item(5).Trime & Chr$(34) & ")" 


'Debug.Print (STR1) 
'Debug.Print (STR2) 

'-------------- 
'L'Objet CONNEXION/ Connexion Object 

''''''''''''''''Définition du Chemin d'accès vers la base de données/ Database workpath 

strDBName = "Bulletin_Light.accdb" 
strMyPath = ThisWorkbook.Path 
strDB = strMyPath & "\" & strDBName 

''''''''''''''''Connect to a data source: 
db2.Open ConnectionString:="Provider = Microsoft.ACE.OLEDB.12.0; data source=" & strDB 

'''''''''''''''Chargement du vecteur syntaxe sql par appel de la procédure bosql/ Fill two vector, one containing text file name vartitres(), the other the content of the text file in which I have my SQL statement with a STR1/ STR2 in place of my WHERE Condition bouclesql(). It does it for each text file in a defined folder 

Call bosql 

Dim i As Integer 
i = 0 

''''''''''''''''Début de la boucle géante Par Feuille 

For i = 0 To UBound(bouclesql) 

    'Création de la nouvelle feuille/ Create a new worksheet names it like the text file ,checks if it already exists and deletes ii in case 

Dim sht As Worksheet, shtname As String 
    Set sht = ThisWorkbook.Sheets.add(after:=Worksheets(Worksheets.count)) 'worksheets(ThisWorkbook.Sheets.count) 
    If sht.name = Replace(vartitres(i), ".txt", "") _ 
     Then ThisWorkbook.Worksheets(sht.name).Delete _ 
     Else sht.name = Replace(vartitres(i), ".txt", "") 

    sht.Cells.Font.name = "Times New Roman" 
    sht.Cells.Font.Size = 10 


''' Correction du STR1/STR2 (s'il y en a) de la syntaxe SQL i/ Correction of the keyword STR1/STR2 in the WHERE Clause of my bouclesql() by the right content stored in these variables 
'''Store the right statement in STRALL 

''' de bouclesql(i)par la bonne condition sur le WHERE 


Dim STRALL As String 
If (vartitres(i) = "Tab1.txt" Or vartitres(i) = "Tab2.txt" Or vartitres (i) = "Tab3.txt" Or vartitres(i) = "Tab4.txt") Then 
    STRALL = Replace(bouclesql(i), "STR1", STR1) 
Else 
    STRALL = Replace(bouclesql(i), "STR2", STR2) 
End If 

    'Debug.Print bouclesql(i) 
    Debug.Print STRALL 

'SQL Statement 
    Dim cmd As New ADODB.Command 
     cmd.CommandType = adCmdText 
     cmd.CommandText = STRALL 
     cmd.ActiveConnection = db2 


'''Ouverture du recordset 

rs.Open cmd, , adOpenKeyset, adLockOptimistic 

回答

0

也许区分大小写的问题? 代码

[...] Replace(bouclesql(i), "STR1", STR1) [...] 
[...] Replace(bouclesql(i), "STR2", STR2) [...] 

大写 “STR1” 或 “STR2” 可能是一个问题。 例如:

Dim strTry1 As String = "dog_cat_duck" 
    Dim strTry2 As String = "Dog_Cat_Duck" 

    MsgBox(Replace(strTry1, "dog", "car")) 
    MsgBox(Replace(strTry2, "dog", "car")) 

替换strTry1的作品。 替换strTry2不起作用。

在使用Replace()之前,请尝试使用lCase/uCase

编辑/提示: 这是可怕的阅读:

Dim STRALL As String 
If (vartitres(i) = "Tab1.txt" Or vartitres(i) = "Tab2.txt" Or vartitres(i) = "Tab3.txt" Or vartitres(i) = "Tab4.txt") _ 
Then STRALL = Replace(bouclesql(i), "STR1", STR1) Else STRALL = Replace(bouclesql(i), "STR2", STR2) 

好多了:

Dim STRALL As String 
If (vartitres(i) = "Tab1.txt" Or vartitres(i) = "Tab2.txt" Or vartitres(i) = "Tab3.txt" Or vartitres(i) = "Tab4.txt") Then 
    STRALL = Replace(bouclesql(i), "STR1", STR1) 
Else 
    STRALL = Replace(bouclesql(i), "STR2", STR2) 
End If 
+0

这样的小白!我发现了这个问题!这是STRALL填充部分。我的文件名发生了变化,我忘了在IF子句中更改它。非常感谢!!! 我也遇到了问题。代码的一部分应该删除工作表,如果它存在不起作用。它创建一个新的工作表,无法命名它并提示我一条错误消息。我该如何解决这个问题? – Meomeoowww 2015-02-06 07:38:52