Hi all,
Mình dùng Access, xuất data ra file excel, nhưng hễ cứ tới phần chữ màu đỏ bên dưới là xuất hiện lỗi Out of range. Nhờ anh chị có kinh nghiệm chỉ giáo giúp nhé, Cảm ơn
Hàm Nút outfile như sau:
Private Sub cmd_Outfile_userSheet_Click()
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("qry_DMS_UserQualifiedList_0")
'FileCopy txt_templatePath, txt_outputpath
Call WriteRecordset("A2", "User", rs, txt_templatePath, 2, "", False)
Set rs = Nothing
End Sub
Public Sub WriteRecordset(Vposition As String, shetname As String, vrc As DAO.Recordset, path As String, ShowHiddenVeryHidden As Double, pwd As String, ClosefileAfterDone As Boolean)
Dim wb As String: Dim pos As Byte
Dim objWbook As Excel.Application
pos = InStrRev(path, "\")
wb = Mid(path, pos + 1)
vrc.MoveFirst
Excel.Application.DisplayAlerts = False
Excel.Application.Visible = True
If IsWorkbookOpen(path) = False Then
Excel.Workbooks.Open path
ElseIf IsWorkbookOpen(path) = True Then
Workbooks(wb).Activate
End If
Excel.Sheets("" & shetname & "").Visible = True
Excel.Sheets("" & shetname & "").Select
If CheckIfSheetProtected(Excel.Sheets("" & shetname & "")) Then Call unProtectSheet(shetname, pwd)
Excel.Range("" & Vposition & "").CopyFromRecordset vrc
Excel.Sheets("" & shetname & "").Visible = ShowHiddenVeryHidden
Excel.ActiveWorkbook.Save
If ClosefileAfterDone = True Then Excel.ActiveWorkbook.Close
Excel.Application.DisplayAlerts = True
End Sub
Function IsWorkbookOpen(fname As String)
Dim filenum As Integer, errnum As Integer
On Error Resume Next
filenum = FreeFile()
Open fname For Input Lock Read As #filenum
Close filenum
errnum = Err
On Error GoTo 0
Select Case errnum
Case 0
IsWorkbookOpen = False
Case 70
IsWorkbookOpen = True
End Select
End Function
Mình dùng Access, xuất data ra file excel, nhưng hễ cứ tới phần chữ màu đỏ bên dưới là xuất hiện lỗi Out of range. Nhờ anh chị có kinh nghiệm chỉ giáo giúp nhé, Cảm ơn
Hàm Nút outfile như sau:
Private Sub cmd_Outfile_userSheet_Click()
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("qry_DMS_UserQualifiedList_0")
'FileCopy txt_templatePath, txt_outputpath
Call WriteRecordset("A2", "User", rs, txt_templatePath, 2, "", False)
Set rs = Nothing
End Sub
Public Sub WriteRecordset(Vposition As String, shetname As String, vrc As DAO.Recordset, path As String, ShowHiddenVeryHidden As Double, pwd As String, ClosefileAfterDone As Boolean)
Dim wb As String: Dim pos As Byte
Dim objWbook As Excel.Application
pos = InStrRev(path, "\")
wb = Mid(path, pos + 1)
vrc.MoveFirst
Excel.Application.DisplayAlerts = False
Excel.Application.Visible = True
If IsWorkbookOpen(path) = False Then
Excel.Workbooks.Open path
ElseIf IsWorkbookOpen(path) = True Then
Workbooks(wb).Activate
End If
Excel.Sheets("" & shetname & "").Visible = True
Excel.Sheets("" & shetname & "").Select
If CheckIfSheetProtected(Excel.Sheets("" & shetname & "")) Then Call unProtectSheet(shetname, pwd)
Excel.Range("" & Vposition & "").CopyFromRecordset vrc
Excel.Sheets("" & shetname & "").Visible = ShowHiddenVeryHidden
Excel.ActiveWorkbook.Save
If ClosefileAfterDone = True Then Excel.ActiveWorkbook.Close
Excel.Application.DisplayAlerts = True
End Sub
Function IsWorkbookOpen(fname As String)
Dim filenum As Integer, errnum As Integer
On Error Resume Next
filenum = FreeFile()
Open fname For Input Lock Read As #filenum
Close filenum
errnum = Err
On Error GoTo 0
Select Case errnum
Case 0
IsWorkbookOpen = False
Case 70
IsWorkbookOpen = True
End Select
End Function