2016-08-30 4 views
0

이 특정 문제로 인해 Access에서 내 보낸 데이터를 Excel에 추가하는 코드를 가져올 수 없습니다. 양식에 표시된 일부 데이터로 간단한 Access 데이터베이스를 만들었습니다. 그 후 코드를 사용하여 표시된 레코드를 Excel로 내보낼 수 있습니다.VBA를 사용하여 Access에서 Excel에 추가

지금까지 그렇게 좋았습니다. 그러나 다음 레코드를 내보낼 때 Excel의 첫 번째 행에서 이전에 내 보낸 데이터를 덮어 씁니다. 코드를 다음 행에 추가하는 등의 작업을 수행합니다.

"ActiveCell.Value"및 "ActiveCell.Offset"을 추가하는 방법에 대한 몇 가지 항목을 찾았지만 지식이 너무 제한되어 코드에서 작동하지 않습니다. VBE에 오류가 발생했습니다. 나는 이것을 알아낼 수없는 것 같다.

Private Sub Command15_Click() 
Dim oExcel   As Object 
Dim oExcelWrkBk  As Object 
Dim oExcelWrSht  As Object 
Dim bExcelOpened As Boolean 

'Start Excel 
On Error Resume Next 
Set oExcel = GetObject(, "Excel.Application") 'Bind to existing instance of Excel 
If Err.Number <> 0 Then 'Could not get instance of Excel, so create a new one 
    Err.Clear 
    On Error GoTo Error_Handler 
    Set oExcel = CreateObject("excel.application") 
    bExcelOpened = False 
Else 'Excel was already running 
    bExcelOpened = True 
End If 
On Error GoTo Error_Handler 
oExcel.ScreenUpdating = False 
oExcel.Visible = False 'Keep Excel hidden until we are done with our manipulation 
'Set oExcelWrkBk = oExcel.Workbooks.Add() 'Start a new workbook 
Set oExcelWrkBk = oExcel.Workbooks.Open("C:\test.xlsx")  'Open an existing Excel file 
Set oExcelWrSht = oExcelWrkBk.Sheets(1) 'which worksheet to work with 

'Start copying over your form values to the Excel Spreadsheet 
'Cells(8, 3) = 8th row, 3rd column 
oExcelWrSht.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0) = Me.1 
oExcelWrSht.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0) = Me.2 
oExcelWrSht.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0) = Me.3 
oExcelWrSht.Cells(Rows.Count, 4).End(xlUp).Offset(1, 0) = Me.4 
oExcelWrSht.Cells(Rows.Count, 5).End(xlUp).Offset(1, 0) = Me.5 
oExcelWrSht.Cells(Rows.Count, 6).End(xlUp).Offset(1, 0) = Me.6 
oExcelWrSht.Cells(Rows.Count, 7).End(xlUp).Offset(1, 0) = Me.7 
oExcelWrSht.Cells(Rows.Count, 8).End(xlUp).Offset(1, 0) = Me.8 
oExcelWrSht.Cells(Rows.Count, 9).End(xlUp).Offset(1, 0) = Me.9 
'... and so on ... 

oExcelWrSht.Range("A1").Select 'Return to the top of the page 

' oExcelWrkBk.Close True, sFileName 'Save and close the generated workbook 
' 'Close excel if is wasn't originally running 
' If bExcelOpened = False Then 
'  oExcel.Quit 
' End If Error_Handler_Exit: 
On Error Resume Next 
oExcel.Visible = True 'Make excel visible to the user 
Set oExcelWrSht = Nothing 
Set oExcelWrkBk = Nothing 
oExcel.ScreenUpdating = True 
Set oExcel = Nothing 
Exit Sub Error_Handler: 
MsgBox "The following error has occured" & vbCrLf & vbCrLf & _ 
     "Error Number: " & Err.Number & vbCrLf & _ 
     "Error Source: Export2XLS" & vbCrLf & _ 
     "Error Description: " & Err.Description _ 
     , vbOKOnly + vbCritical, "An Error has Occured!" 
Resume Error_Handler_Exit End Sub 
+0

데이터베이스를 사용하는 경우 왜 레코드를 Excel에 추가하려고합니까? Access에서 레코드를 저장하고 (데이터베이스의 용도로 사용) Excel에서 데이터베이스에서 필요로하는 레코드를 가져 오지 않는 이유는 무엇입니까? – jkpieterse

+0

그래서 이것을 실행할 때마다 반드시 10 번째 행의 값을 설정하게됩니까? 내가 시도하고 다음 행으로 이동 시도가 누락되었습니다. –

+0

@jkpieterse : 어떻게 이것을 할 수 있습니까? 나를 올바른 방향으로 밀어 넣을 수 있습니까? –

답변

0

나는 이것을 시도 했으므로 아무런 문제가 없으므로 오른쪽 엑셀 라이브러리에 대한 참조가 있다고 가정하면 작동하는지 확인할 수 있습니까?

Sub Test() 
Dim oExcel As Excel.Application 
Dim oExcelWrkBk As Excel.Workbook 
Dim oExcelWrSht As Excel.Worksheet 

'Start Excel 
On Error Resume Next 
Set oExcel = GetObject(, "Excel.Application") 
If Err <> 0 Then 
    Err.Clear 
    On Error GoTo Error_Handler 
    Set oExcel = CreateObject("Excel.Application") 
Else 
    On Error GoTo Error_Handler 
End If 

oExcel.ScreenUpdating = False 
oExcel.Visible = False 'This is false by default anyway 

Set oExcelWrkBk = oExcel.Workbooks.Open("C:\test.xlsx") 
Set oExcelWrSht = oExcelWrkBk.Sheets(1) 

oExcelWrSht.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0) = "Test1" 
oExcelWrSht.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0) = "Test2" 
oExcelWrSht.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0) = "Test3" 

oExcelWrSht.Range("A1").Select 

oExcelWrkBk.Save 

oExcel.ScreenUpdating = True 
oExcel.Visible = True 

Exit_Point: 
Set oExcelWrSht = Nothing 
Set oExcelWrkBk = Nothing 
Set oExcel = Nothing 
Exit Sub 

Error_Handler: 
MsgBox Err & " - " & Err.Description 
GoTo Exit_Point 
End Sub 
+0

코드가 작동합니다! 고맙습니다. Excel Object Library 및 Microsoft Form 2.0과 같은 일부 라이브러리 참조에 몇 가지 문제가있었습니다. 나는 이것들과 오류를 어디에 추가했는지 추가했다. 한 가지 질문. 데이터를 Excel로 내 보낸 후에 Excel 파일을 닫지 않고 열린 Excel 파일이 자동으로 저장 될 수 있습니까? –

+0

네, 그걸 안으로 추가 할게요. –

+0

다시 한번, 매우 제한된 VBA 지식으로 나를 도와 주셔서 감사합니다. 코드는 내가 원하는 것을 정확하게 수행합니다. –

관련 문제