이 특정 문제로 인해 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
데이터베이스를 사용하는 경우 왜 레코드를 Excel에 추가하려고합니까? Access에서 레코드를 저장하고 (데이터베이스의 용도로 사용) Excel에서 데이터베이스에서 필요로하는 레코드를 가져 오지 않는 이유는 무엇입니까? – jkpieterse
그래서 이것을 실행할 때마다 반드시 10 번째 행의 값을 설정하게됩니까? 내가 시도하고 다음 행으로 이동 시도가 누락되었습니다. –
@jkpieterse : 어떻게 이것을 할 수 있습니까? 나를 올바른 방향으로 밀어 넣을 수 있습니까? –