Link to home
Start Free TrialLog in
Avatar of Lawrence Salvucci
Lawrence SalvucciFlag for United States of America

asked on

Load results from query from MS Access to MS Excel and putting values in next blank row and formatting the values to the same as the previous row.

I am trying to export the results of a query into an existing excel workbook and I'm having a hard time getting this code to work completely. It works but it doesn't always skip to the next blank cell in column B. When I fire it for the first time it works but then if I fire it again after that it just overwrites the last entry I just put into the workbook. It's not going to the next blank cell in column B after that first initial run. Can anyone help me fix this code to do just that so every time I run it the entry will go into the next blank row where there is no value in column B? There are values in row 1, then a blank row in row 2, then the headers in row 3, and then the values start in row 4. The last entry I have currently is row 350. So when I would run this it would put the results in row 351. Then when I would run it again instead of putting the value in row 352 it would put it in 351 and overwrite the values I just put in there. Also, how can I paste the values and then run the format painter to make the format of these new entries the same as the last row with values from Column B to H? Any help would be greatly appreciated.

Private Sub cmdExcelExport_Click()
Dim lngColumn As Long
Dim xlx As Object, xlw As Object, xls As Object, xlc As Object
Dim rst As DAO.Recordset
Dim blnEXCEL As Boolean, blnHeaderRow As Boolean

blnEXCEL = False
blnHeaderRow = True

' Establish an EXCEL application object
On Error Resume Next
Set xlx = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
      Set xlx = CreateObject("Excel.Application")
      blnEXCEL = True
End If
Err.Clear
On Error GoTo 0

' Change True to False if you do not want the workbook to be
' visible when the code is running
xlx.visible = True

Set xlw = xlx.Workbooks.Open("\\BCAR1\BC-Net\IT\Help Desk\M2M\Group Code - PN Prefix ref table v.2.xlsx")
Set xls = xlw.Worksheets("Sort By Prefix")
Set xlc = xls.Range("B" & xls.UsedRange.rows.Count)
Set rst = CurrentDb.OpenRecordset("SELECT * FROM qryGroupCodeExcelExport WHERE qryGroupCodeExcelExport.[GroupCode] = """ & Me.txtGroupCode.value & """")

If rst.EOF = False And rst.BOF = False Then

      rst.MoveFirst
    'Inserts Header row
      'If blnHeaderRow = True Then
      '      For lngColumn = 0 To rst.Fields.Count - 1
      '            xlc.Offset(0, lngColumn).value = rst.Fields(lngColumn).Name
      '      Next lngColumn
      '      Set xlc = xlc.Offset(1, 0)
      'End If

      ' write data to worksheet
      Do While rst.EOF = False
            For lngColumn = 0 To rst.Fields.Count - 1
                  xlc.Offset(0, lngColumn).value = rst.Fields(lngColumn).value
            Next lngColumn
            rst.MoveNext
            Set xlc = xlc.Offset(1, 0)
      Loop
End If

rst.Close
Set rst = Nothing

dbs.Close
Set dbs = Nothing

' Close the EXCEL file while saving the file, and clean up the EXCEL objects
Set xlc = Nothing
Set xls = Nothing
xlw.Close True   ' close the EXCEL file and save the new data
Set xlw = Nothing
If blnEXCEL = True Then xlx.Quit
Set xlx = Nothing
End Sub

Open in new window

Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

try using the find method:
  
    xlc = xls.Cells.Find(What:="*", _
                    After:=Range("A1"), _
                    LookAt:=xlPart, _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious, _
                    MatchCase:=False).Row
    
    MsgBox "Last Row: " & xlc 

Open in new window

or from:
http://www.utteraccess.com/forum/find-row-excel-access-t1992483.html
xlc = xlx.Cells(xlx.Rows.Count, 1).End(xlUp).Row

Open in new window

Avatar of Lawrence Salvucci

ASKER

Neither of those worked. For the first one I am getting a "Compile Error: Variable not defined" for these:

xlPart
xlFormulas
xlByRows
xlPrevious
.row

The second one I am getting an error that says "Run-Time Error '424': Object Required" for the entire line of code when I debug it.
ASKER CERTIFIED SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial