起初,我可能會阻止在使用英語寫作時犯錯誤,因爲它不是我的第一語言。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
這樣的小白!我發現了這個問題!這是STRALL填充部分。我的文件名發生了變化,我忘了在IF子句中更改它。非常感謝!!! 我也遇到了問題。代碼的一部分應該刪除工作表,如果它存在不起作用。它創建一個新的工作表,無法命名它並提示我一條錯誤消息。我該如何解決這個問題? – Meomeoowww 2015-02-06 07:38:52