what is wrong with this code in that it works fine in one workbook, but gives me 'run-time error '1004' in a differeent workbook?

kbishop94

Active Member
Joined
Dec 5, 2016
Messages
458
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I have a "test workbook" that I use when I'm trying to figure out how to make something work... then after I fiddle with the code enough to (finally) get it to work the way i need to, I copy it over to my main workbook.
Well, i have checked every conceivable thing that I thought it could be (property settings, names, workbook settings, etc) and it still wont work in my main one(?)

Here is the code:

Code:
Private Sub CommandButton1_Click()


Dim fCell As Range
Dim ws1 As Worksheet
Dim vMax As Variant
Set ws1 = Worksheets("Sheet1")


[COLOR=#008000]' sets a range for column "A" on worksheet1 starting at row 18 and going down to the last entry that is in that column[/COLOR]
For Each fCell In Worksheets("Sheet1").Range("A18", Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp))


[COLOR=#008000]'finds the cell with the highest value in the previously defined range of 'fCell'[/COLOR]
vMax = Application.WorksheetFunction.Max(Range(Cells(18, 1), Cells(fCell, 1)))


Next fCell


[COLOR=#008000]'posts the highest value cell in fCell range and places it in a text field on the userform[/COLOR]
Me.txtIncidentID.Value = vMax


End Sub

This is the line that is snagging it up after I copy it over to the main workbook (if I temporarily take it out, it runs):
Code:
vMax = Application.WorksheetFunction.Max(Range(Cells(18, 1), Cells(fCell, 1)))

The code has no problem running in my 'test workbook':
Working just fine on my test workbook...
2wea1dz.jpg



But i get this errror after copying and pasting into my other (main) workbook.... :oops:

zvq4i0.jpg



What do I have wrong that it wont run with that line of code?? THank you

Keith
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Code:
With ws1
vMax = Application.WorksheetFunction.Max(.Range(.Cells(18, 1), .Cells(fCell, 1)))
End With
Range and Cells not qualified.

What is wrong with the site. This thread is fighting me to do the post?
 
Last edited:
Upvote 0
Code:
With ws1
vMax = Application.WorksheetFunction.Max(.Range(.Cells(18, 1), .Cells(fCell, 1)))
End With
Range and Cells not qualified.

What is wrong with the site. This thread is fighting me to do the post?


Thank you!
icon14.png


Now, 2nd question... so why would it work in one workbook but not the other?
 
Upvote 0
I'm surprised it runs in any workbook. This syntax doesn't look right:

Application.WorksheetFunction.Max(Range(Cells(18, 1), Cells(fCell, 1)))

as fCell is dimensioned as a range. Did you mean Cells(fCell.Row,1)?
 
Upvote 0
I'm surprised it runs in any workbook. This syntax doesn't look right:

Application.WorksheetFunction.Max(Range(Cells(18, 1), Cells(fCell, 1)))

as fCell is dimensioned as a range. Did you mean Cells(fCell.Row,1)?

I'm not defending or arguing for whatever code that was that i posted. I am very much a novice (a self taught/learning) novice at VBA. I couldnt tell you why, or how that it works... but just so you know that I am not seeing things... here it is working lol :p


https://youtu.be/v1COyn2QJFE
 
Upvote 0
How should that range be formatted in a correct way that will work in my other workbook
 
Upvote 0
Now, 2nd question... so why would it work in one workbook but not the other?

If Range and Cells references are not quaified with their parent objects, then VBA will use the ActiveSheet as the parent. When you were in the other workbook, you oould have been running the code with the ActiveSheet being the one needed to make the code work right. Or you might have changed th code at some point since running it in the other workook. Hard to tell frm here.
 
Last edited:
Upvote 0
I still cannot get the code (the range property) to work(?) How should it be written?

I tried this:
Did you mean Cells(fCell.Row,1)?
and no it doesnt work either. Im sure I just have something simple in how it should be formatted/written incorrect, but I cant see it(?) THanks
 
Upvote 0

Forum statistics

Threads
1,214,942
Messages
6,122,367
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top