.End(xlUp).Row not working in Access

Vagus14

Registered User.
Local time
Today, 16:25
Joined
May 19, 2014
Messages
66
I can't get the code in red below to work. How would I go about coding this section? Is it possible to code this part? "
"lastRow = ws.Cells(ws.Rows.Count, "F").End(xlUp).Row 'Error 1004"

Errors recieved:
"1004" Application-defined or object-defined error

The objective is so it will look in column "F" for total count of data. Next line it adds + 1 and that's the cell it will insert the picture.

Thanks for your help.

Code:
Public Function AddITARPic()
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
 
With xlApp
Dim wb As Object
Dim ws As Object
Dim lastRow As Long
Set wb = .Workbooks.Open("D:\Sample.xlsm")
Set ws = wb.Sheets(1)
 
[COLOR=seagreen]'Code below counts the cells with data and selects the last empty cell[/COLOR]
[COLOR=black]lastRow = ws.Cells(ws.Rows.Count, "F").[/COLOR][COLOR=red]End(xlUp).Row  'Error 1004[/COLOR]
[COLOR=black]Blankcell = lastRow + 1[/COLOR]
ws.Cells(Blankcell, "A").Select
ws.pictures.insert ("D:\sample.png")
 
xlApp.Visible = True
LinkToFile = False
SaveWithDocument = True
wb.Close
Set xlApp = Nothing
End With
End Function
 
Constants like xlUp typically don't work when using late binding. You can find the numerical equivalent, or there's another method to get the lady row that escapes me right now. I have it on another computer that I'll be on in a little bit if you need it.
 
Hey Paul,

Thanks for the help. How would I incorporate the numerical equivalent? I farily new to the coding process.

Lady row? Yeah any solution you can provide I'm all ears. Thanks!
 
Go into excel immediate window and type: ?xlUp

?xlup
-4162

Or google microsoft.com :P
 
Why are you finding the lastrow in Col F and then using this for column A ?

If all columns have the same number of data rows does
lastrow = ws.UsedRange.Rows.Count

work for Access code

Brian
 
The other method I mentioned:

lastRow = ws.cells.SpecialCells(11).row
 
Thank you for everyone's help! I am new to VBA so you were very helpful. Vote up for everyone here and hopefully this thread will help others!

Code:
Public Function LoopAddPic()
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
With xlApp
Dim wb As Object
Dim ws As Object
Dim lastRow As Long
Set wb = .Workbooks.Open("D:\Sample.xlsm")
Set ws = wb.Sheets(1)
'Code below counts the cells with data and selects the last empty cell
lastRow = ws.UsedRange.Rows.Count
'Cells(ws.Rows.Count, "F") '.End(xlUp).Row
Blankcell = lastRow + 1
ws.Cells(Blankcell, "F").Select
ws.pictures.insert ("D:\sample.png")
xlApp.Visible = True
LinkToFile = False
SaveWithDocument = True
wb.Close
Set xlApp = Nothing
End With
End Function
 
One more question, sorry. How would u get this to auto save and close with the same name? I did a google search but haven't found anything concrete.
 
Try

xlApp.ActiveWorkbook.Save
xlApp.Quit
 
For some reason the code opens the workbook in read only format. So when I do what you suggusted it prompts for a new file name. Is there a way to prevent this?

Here's the code I am using.

Code:
Public Function AddITARPic()
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
With xlApp
Dim wb As Object
Dim ws As Object
Dim lastRow As Long
 
Set wb = .Workbooks.Open("D:\Sample.xlsm")
Set ws = wb.Sheets(1)
 
'Code below counts the cells with data and selects the last empty cell
lastRow = ws.UsedRange.Rows.Count
Blankcell = lastRow + 1
ws.Cells(Blankcell, "F").Select
ws.pictures.insert ("D:\sample.png")
 
xlApp.Visible = True
LinkToFile = False
SaveWithDocument = True
'ws.SaveAs FileName:="D:\" & BookName
wb.Close , [COLOR=red]SaveChanges:=True (The excel file is read only)[/COLOR]
Set xlApp = Nothing
End With
End Function
 
The spreadsheet isn't open already is it? I've never used that extension, so I don't know if that's the problem.
 
Paul,

Everything is working correctly. Thanks again for your help!

James
 

Users who are viewing this thread

Back
Top Bottom