2013-07-21 2 views
1

데이터를 Excel 2007 파일로 내보내는 Access 2007 데이터베이스 내에 VBA 코드가 있습니다. 나는이 코드 조각에 문제가 :Excel 2007 워크 시트의 마지막 행을 찾기 위해 vba에 액세스

Sub GetLastRow(strSheet, strColum) 
Dim MyRange As Range 
Dim lngLastRow As Long 

Set MyRange = Worksheets(strSheet).Range(strColum & "1") 

lngLastRow = Cells(65536, MyRange.Column).End(xlUp).Row 
lngLastRow = lngLastRow + 1 
Rows(lngLastRow & ":1048576").Select 

Selection.Delete Shift:=xlUp 
End Sub 

문제는 내가 수동으로 엑셀 세션을 열하지 않는 한 엑셀 파일 (이 Excel 파일에 이미있는) 헤더 행 속해 계산하지 않습니다 변수 lngLastRow입니다 그런 다음 코드를 계속 실행하십시오. 이 문제를 올바르게 해결하고 싶습니다만, Excel 파일을 표시하는 코드를 포함시켜야 자동으로 문제가 해결 될 수 있습니다. 그러나 내가 어디에서 어떻게 할 수 있는지 알 수 없다.

다음은 위 함수를 호출하는 함수입니다.

Function CreateExcelData() 
'Copies data to be exported to an Excel workbook 
Dim objExcel   As Excel.Application 
Dim strTemplate  As String 
Dim strPathFile  As String 
Dim RowCount   As Integer 
Dim wbExported  As Workbook 'The initial exported data 
Dim wbAllData  As Workbook 'Workbook to copy exported data to 
Dim rngUsed   As Range  'Used range in exported data 
Dim Sheet   As Worksheet 

'Try GetObject first in case Excel Application is already open. 
On Error Resume Next 
Set objExcel = GetObject(, "excel.Application") 
If Err.Number <> 0 Then 
    'GetObject returns error if not already open 
    'so use CreateObject 
    On Error GoTo 0 'Turnoff ASAP so error trapping is available 
    Set objExcel = CreateObject("Excel.Application") 
End If 

strTemplate = "TEMPLATE.xlsm" 
strPathFile = strPath & strTemplate 
strPathFileFinal = strPath & strReportName & "_" & Mydat & ".xlsm" 

FileCopy strPathFile, strPathFileFinal 

'Open the exported data workbook and assign to a variable 
Set wbExported = objExcel.Workbooks.Open(strFilePath) 

'Open the data workbook to receive the exported data and assign to a variable. 
Set wbAllData = objExcel.Workbooks.Open(strPathFileFinal) 

'Exported data 
With wbExported.Sheets(1).UsedRange 
    Set rngUsed = .Offset(1, 0) _ 
     .Resize(.Rows.Count - 1, .Columns.Count) 
End With 

With wbAllData.Sheets("MainSheet") 
    'Copy exported data and paste to first empty cell of MainSheet in File 
    rngUsed.Copy 
    .Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues 
End With 

Call GetLastRow("MainSheet", "A") 

wbExported.Close 

wbAllData.Save 
wbAllData.Close 

Set rngUsed = Nothing 
Set wbExported = Nothing 
Set wbAllData = Nothing 
Set objExcel = Nothing 

Kill strFilePath 

End Function 

답변

2

코드는 WorksheetsRanges에 자격이 부분적으로 자격을 갖춘 참고 문헌을 가지고 있습니다. 이것들은 ActiveWorkbook 또는 ActiveSheet을 가리키며 아마도 원하지 않는 것이므로 예측할 수없는 결과를 초래할 수 있습니다.

이 리팩터링 귀하의 답변에 대한 너무 많은이

GetLastRow wbAllData.Worksheets("MainSheet").Columns("A") 
+0

감사와 같은

Sub GetLastRow(MyRange As Excel.Range) Dim lngLastRow As Long With MyRange.Worksheet lngLastRow = .Cells(.Rows.Count, MyRange.Column).End(xlUp).Row .Range(.Cells(lngLastRow + 1, 1), .Cells(.Rows.Count, 1)).EntireRow.Delete End With End Sub 

통화를 시도 처음으로 일했다. 이것은 코드가 작동하고 막 중단됨에 따라 상당히 짜증나 며, 나는 그것을 고치려고 많은 시간을 "잃어 버렸습니다". 감사 – user1407577

관련 문제