Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 29
Default Runtime Error 1004 when runing Sub() from btnClick

Hello,

I have a bit of code that formats the contents of a worksheet and creates
some columns with formulas.

Here is the code:

Public Function LastFriday(SomeDate As Date) As Date
Dim myDate As Date
myDate = DateSerial(Year(SomeDate), Month(SomeDate) + 1, 0)
While Weekday(myDate, vbSunday) < 6
myDate = DateAdd("d", -1, myDate)
Wend
LastFriday = myDate
End Function
Public Function NextPeriod(InvDate As Date) As Date
NextPeriod = DateSerial(Year(InvDate), Month(InvDate) + 1, 1)
End Function
Public Function CurrentPeriod(IDate As Date) As Date
CurrentPeriod = DateSerial(Year(IDate), Month(IDate), 1)
End Function
Sub FormatData()
Application.ScreenUpdating = False
ActiveWindow.DisplayGridlines = False
' Declarations
Dim DataWks As Worksheet
Dim LastRow As Long
Dim LastCol As Long
Dim PTCache As PivotCache

' Set the DataWks variable
Set DataWks = Worksheets("Data")

With DataWks
LastRow = ActiveSheet.UsedRange.Rows.Count - 6
LastCol = Range("IV1").End(xlToLeft).Column

' Create the "Period" Column
.Cells(1, LastCol).Copy
.Cells(1, LastCol + 1).PasteSpecial Paste:=xlPasteFormats, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
.Cells(1, LastCol + 1).WrapText = False
.Cells(1, LastCol + 1).Value = "Booked Month"
.Columns(LastCol + 1).AutoFit


' Populate the Month Column with new Monts
.Range(.Cells(2, LastCol + 1), Cells(LastRow, LastCol + 1)).Formula = _

"=IF(MONTH(I2)=12,CurrentPeriod(I2),IF(I2<=LastFri day(I2),CurrentPeriod(I2),NextPeriod(I2)))"
.Range(.Cells(2, LastCol + 1), Cells(LastRow, LastCol + 1)).NumberFormat
= "MM-YYYY"


' Create the "Country" Column
Columns("B:B").Select
Selection.Insert Shift:=xlToRight
Selection.NumberFormat = "General"
.Cells(1, 1).Copy
.Cells(1, 2).PasteSpecial Paste:=xlPasteFormats, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
.Cells(1, 2).WrapText = False
.Cells(1, 2).Value = "Country"


' Populate the Country Column with new Countries
.Range(.Cells(2, 2), Cells(LastRow, 2)).Formula = _
"=VLOOKUP(A2,ctry_lookup,2,false)"
.Columns("B:B").AutoFit

End With
End Sub

The good part:
* If I run the code from my "Data" worksheet, it all works perfectly and my
data gets formatted and the new columns with my formulas get added.

The problem:
I added a second worksheet to the workbook and added a simple button (not
activeX control) that runs the Sub() when clicked.

* When I click the button to run the Sub() I get a Runtime 1004 error at
this line:
..Range(.Cells(2, LastCol + 1), Cells(LastRow, LastCol + 1)).Formula = _

"=IF(MONTH(I2)=12,CurrentPeriod(I2),IF(I2<=LastFri day(I2),CurrentPeriod(I2),NextPeriod(I2)))"

* If I simply run the Sub() from the Tools menu the error will show up if I
am on any worksheet other than the one containg the data to be formatted.

If anyone can point me in the right direction as to why this error is
happening, I'd greatly appreciate it.

thanks
Juan Correa

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 29
Default Runtime Error 1004 when runing Sub() from btnClick

Nevermind....
All I needed was to add this line:
..Select
Right after my
With DataWks statement....

I'll keep on going for now.
Thanks anyway

Juan Correa
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Runtime Error 1004 when runing Sub() from btnClick

You're missing a dot in this line:

..Range(.Cells(2, LastCol + 1), .Cells(LastRow, LastCol + 1)).

(that second .cells() was refering to the activesheet--not the sheet that
DataWks sheet.)

You've got at least one more occurence to fix, too.

Juan Correa wrote:

Hello,

I have a bit of code that formats the contents of a worksheet and creates
some columns with formulas.

Here is the code:

Public Function LastFriday(SomeDate As Date) As Date
Dim myDate As Date
myDate = DateSerial(Year(SomeDate), Month(SomeDate) + 1, 0)
While Weekday(myDate, vbSunday) < 6
myDate = DateAdd("d", -1, myDate)
Wend
LastFriday = myDate
End Function
Public Function NextPeriod(InvDate As Date) As Date
NextPeriod = DateSerial(Year(InvDate), Month(InvDate) + 1, 1)
End Function
Public Function CurrentPeriod(IDate As Date) As Date
CurrentPeriod = DateSerial(Year(IDate), Month(IDate), 1)
End Function
Sub FormatData()
Application.ScreenUpdating = False
ActiveWindow.DisplayGridlines = False
' Declarations
Dim DataWks As Worksheet
Dim LastRow As Long
Dim LastCol As Long
Dim PTCache As PivotCache

' Set the DataWks variable
Set DataWks = Worksheets("Data")

With DataWks
LastRow = ActiveSheet.UsedRange.Rows.Count - 6
LastCol = Range("IV1").End(xlToLeft).Column

' Create the "Period" Column
.Cells(1, LastCol).Copy
.Cells(1, LastCol + 1).PasteSpecial Paste:=xlPasteFormats, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
.Cells(1, LastCol + 1).WrapText = False
.Cells(1, LastCol + 1).Value = "Booked Month"
.Columns(LastCol + 1).AutoFit


' Populate the Month Column with new Monts
.Range(.Cells(2, LastCol + 1), Cells(LastRow, LastCol + 1)).Formula = _

"=IF(MONTH(I2)=12,CurrentPeriod(I2),IF(I2<=LastFri day(I2),CurrentPeriod(I2),NextPeriod(I2)))"
.Range(.Cells(2, LastCol + 1), Cells(LastRow, LastCol + 1)).NumberFormat
= "MM-YYYY"


' Create the "Country" Column
Columns("B:B").Select
Selection.Insert Shift:=xlToRight
Selection.NumberFormat = "General"
.Cells(1, 1).Copy
.Cells(1, 2).PasteSpecial Paste:=xlPasteFormats, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
.Cells(1, 2).WrapText = False
.Cells(1, 2).Value = "Country"


' Populate the Country Column with new Countries
.Range(.Cells(2, 2), Cells(LastRow, 2)).Formula = _
"=VLOOKUP(A2,ctry_lookup,2,false)"
.Columns("B:B").AutoFit

End With
End Sub

The good part:
* If I run the code from my "Data" worksheet, it all works perfectly and my
data gets formatted and the new columns with my formulas get added.

The problem:
I added a second worksheet to the workbook and added a simple button (not
activeX control) that runs the Sub() when clicked.

* When I click the button to run the Sub() I get a Runtime 1004 error at
this line:
.Range(.Cells(2, LastCol + 1), Cells(LastRow, LastCol + 1)).Formula = _

"=IF(MONTH(I2)=12,CurrentPeriod(I2),IF(I2<=LastFri day(I2),CurrentPeriod(I2),NextPeriod(I2)))"

* If I simply run the Sub() from the Tools menu the error will show up if I
am on any worksheet other than the one containg the data to be formatted.

If anyone can point me in the right direction as to why this error is
happening, I'd greatly appreciate it.

thanks
Juan Correa


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
runtime error '1004' application or object defined error Janis Excel Programming 4 November 18th 09 03:01 PM
runtime error '1004' application or object defined error. Please help deej Excel Programming 0 August 1st 07 09:26 AM
Excel 2003 Macro Error - Runtime error 1004 Cow Excel Discussion (Misc queries) 2 June 7th 05 01:40 PM
Runtime Error '1004' Dean[_4_] Excel Programming 2 January 9th 05 09:02 PM
Runtime error '1004': Capinvest Excel Programming 2 August 4th 03 07:16 PM


All times are GMT +1. The time now is 08:47 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"