silentwolf
Well-known Member
- Joined
- May 14, 2008
- Messages
- 1,216
- Office Version
- 2016
Hi guys,
I am having troubles to put an autofit for the worksheet after a loop is executed..
I tried to use autofit for the worksheeht after the loop like
wksExcel.UsedRange.Autofit
this line of code I have put after the loop from the above code..
Could someone please let me know where I need to put it correctly?
Many thanks
Silentwolf
I am having troubles to put an autofit for the worksheet after a loop is executed..
Code:
Public Sub ObjekteInExcelSchreiben()
Dim appExcel As Excel.Application
Dim wbkExcel As Excel.Workbook
Dim wksExel As Excel.Worksheet
Dim rngExcel As Excel.Range
Dim rcsO As Recordset
Dim lngZeile As Long
Dim lngZaehler As Long
Set appExcel = Excel.Application
Set wbkExcel = appExcel.Workbooks.Open(CurrentProject.Path & "\Regiebericht.xlsx", , , , , , , , , , , , False)
appExcel.Visible = True
Set wksExel = wbkExcel.Worksheets("Objekte")
Set rngExcel = wksExel.UsedRange
lngZeile = rngExcel.Rows.Count + rngExcel.Row - 1
Set rcsO = CurrentDb.OpenRecordset("qryObjektMitKontakte")
lngZaehler = lngZeile + 1
Do Until rcsO.EOF
wksExel.Cells(lngZaehler, 1).Value = rcsO.Fields("Obje_ID").Value
wksExel.Cells(lngZaehler, 2).Value = rcsO.Fields("Obje_Name").Value
wksExel.Cells(lngZaehler, 3).Value = rcsO.Fields("Obje_Adresse").Value
wksExel.Cells(lngZaehler, 4).Value = rcsO.Fields("Obje_Ort").Value
wksExel.Cells(lngZaehler, 5).Value = rcsO.Fields("Obje_Plz").Value
wksExel.Cells(lngZaehler, 6).Value = rcsO.Fields("Land_Name").Value
wksExel.Cells(lngZaehler, 7).Value = rcsO.Fields("Obje_Kont_Id_f").Value
wksExel.Cells(lngZaehler, 8).Value = rcsO.Fields("KontaktName").Value
rcsO.MoveNext
lngZaehler = lngZaehler + 1
Loop
End Sub
I tried to use autofit for the worksheeht after the loop like
wksExcel.UsedRange.Autofit
this line of code I have put after the loop from the above code..
Could someone please let me know where I need to put it correctly?
Many thanks
Silentwolf