silentwolf
Well-known Member
- Joined
- May 14, 2008
- Messages
- 1,216
- Office Version
- 2016
Hi guys,
I am not able to work this one out...
Following code is working fine when I am transfering a recordset into excel when there is already a record in excel.
But what I like to have is when there is no record in the first column should start the number 1 and after each loop adds 1 to it
So if a record is already in the excel worksheet every other record will be added by +1 in column A ..
But what or how does this code has to be changed so it wont matter if there is a record or not?
I tried with an if statement but without luck..
Would be nice if someone could help me please..
Many thanks as always!
Silentwolf
I am not able to work this one out...
Following code is working fine when I am transfering a recordset into excel when there is already a record in excel.
But what I like to have is when there is no record in the first column should start the number 1 and after each loop adds 1 to it
Code:
Sub ReportToExcel()
Dim appExcel As Excel.Application
Dim wbkExcel As Excel.Workbook
Dim wksExcel As Excel.Worksheet
Dim rngExcel As Excel.Range
Dim rcsM As Recordset
Dim lngZeile As Long
Dim lngZaehler As Long
Set appExcel = Excel.Application
Set wbkExcel = appExcel.Workbooks.Open(CurrentProject.Path & "\StundenEintrag.xlsx", , , , , , , , , , , , False)
appExcel.Visible = True
Set wksExcel = wbkExcel.Worksheets("Rechnungsdaten")
Set rngExcel = wksExcel.UsedRange
lngZeile = rngExcel.Rows.Count + rngExcel.Row - 1
Set rcsM = CurrentDb.OpenRecordset("SELECT * FROM tblKontakte WHERE Ort <> 'Kössen'", dbOpenDynaset)
lngZaehler = lngZeile + 1
Do Until rcsM.EOF
wksExcel.Cells(lngZaehler, 2).Value = rcsM.Fields("Ort").Value
wksExcel.Cells(lngZaehler, 1).Value = wksExcel.Cells(lngZaehler - 1, 1).Value + 1
rcsM.MoveNext
lngZaehler = lngZaehler + 1
Loop
End Sub
So if a record is already in the excel worksheet every other record will be added by +1 in column A ..
But what or how does this code has to be changed so it wont matter if there is a record or not?
I tried with an if statement but without luck..
Would be nice if someone could help me please..
Many thanks as always!
Silentwolf