TotallyConfused
Board Regular
- Joined
- May 4, 2017
- Messages
- 247
- Office Version
- 365
- Platform
- Windows
Hello
Can anyone please tell what is wrong with my code. When I run this Sub CalcWriteMisc() everything works perfectly.
Now that I have written the data into cells, I need to create a range name for them. Each day a new row of data will be added, so I am unable to manually create the range names. Immediately after running the sub listed above, I run Sub CreateRangeNames(). This is where my problem starts. On the command of '.Cells(2,1).Select' which is part of 'With ws2' I receive this error message. 'Runtime error 1004 Select method of range class failed'.
Notice in both subs, I refer to the same sheets in the DIM and SET statements so nothing different. I discovered that if I have the 'ws2' sheet displayed, when I run this last macro, that part of the macro works okay, HOWEVER, the same error message will now appear on the '.Cells2,1).Select' under 'With ws4'. What's confusing is that when I ran the first Sub, I didn't have ANY of those three sheets on the screen, yet it worked okay. Where am I going wrong?
I don't know if this matters, but I'm writing this on a computer with Vista and with Excel 2010. It will be used on Windows 10 with Excel 2007. THANK YOU for any help.
TotallyConfused (Boy am I ever)
Can anyone please tell what is wrong with my code. When I run this Sub CalcWriteMisc() everything works perfectly.
Code:
Sub CalcWriteMisc() 'Calculates and writes misc. items to MiscItemsDB
Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Sheets("DataEntryItemsDB")
Dim ws3 As Worksheet: Set ws3 = ThisWorkbook.Sheets("Variables")
Dim ws4 As Worksheet: Set ws4 = ThisWorkbook.Sheets("MiscItemsDB")
Dim SourceRo As Integer 'Row in DataEntryItemsDB
Dim TotalParts As Integer 'Total of all columns for parts,oil,tires,battery
Dim TotalLabor As Integer 'Total of all labor columns
Dim X As Integer 'Loop counter
X = 1
With ws3 'ws3 = ThisWorkbook.Sheets("Variables")
'Calc. source row in DataEntryItemsDB sheet.
SourceRo = .Range("EXCEL_DATE_V") - 42733
End With 'Jan. 1, 2017 = 42736 so will be first row (row [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=3]#3[/URL] )
With ws2 'ws2 = ThisWorkbook.Sheets("DataEntryItemsDB")
For X = 25 To 34 'Total all 10 columns of total labor
TotalLabor = TotalLabor + .Cells(SourceRo, X).Value
Next X
For X = 5 To 74
TotalParts = TotalParts + .Cells(SourceRo,X).Value
Next X
TotalParts = TotalParts - TotalLabor
End With 'End With ws2 = ThisWorkbook.Sheets("DataEntryItemsDB")
With ws4 'ws4 = ThisWorkbook.Sheets("MiscItemsDB")
.Cells(SourceRo, "A").Value = Range("EXCEL_DATE_V") 'Write EXCEL_DATE_V
.Cells(SourceRo, "B").Value = TotalParts 'Write TotalParts to current dates row
.Cells(SourceRo, "C").Value = TotalLabor 'Write TotalLabor to current dates row
End With 'END WITH ws4 = ThisWorkbook.Sheets("MiscItemsDB")
End Sub
Now that I have written the data into cells, I need to create a range name for them. Each day a new row of data will be added, so I am unable to manually create the range names. Immediately after running the sub listed above, I run Sub CreateRangeNames(). This is where my problem starts. On the command of '.Cells(2,1).Select' which is part of 'With ws2' I receive this error message. 'Runtime error 1004 Select method of range class failed'.
Code:
Public Sub CreateRangeNames() 'Create and names a range ALSO places name in NAME MANAGER
Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Sheets("DataEntryItemsDB")
Dim ws4 As Worksheet: Set ws4 = ThisWorkbook.Sheets("MiscItemsDB")
Dim DataEntryItemsDBRn As Range 'Name of range in DataEntryItemsDBRn
Dim MiscItemsDBRn As Range 'Name of range in MiscItemsDBRn
With ws2 'ws2 = ThisWorkbook.Sheets("DataEntryItemsDB")
.Cells(2, 1).Select 'Move cursor to cell A2 and makes it the ActiveCell
'The following line of code creates the range and names it 'DataEntryItemsDBRn'
Set DataEntryItemsDBRn = Range(ActiveCell, Cells(ActiveCell.End(xlDown).Row, _ ActiveCell.End(xlToRight).Column))
ActiveWorkbook.Names.Add Name:="DataEntryItemsDBRn",_ RefersTo:="=" & DataEntryItemsDBRn.Address
End With 'END WITH ws2 = ThisWorkbook.Sheets("DataEntryItemsDB")
With ws4 'ws4 = ThisWorkbook.Sheets("MiscItemsDB")
.Cells(2, 1).Select 'Move cursor to cell A2 and makes it the ActiveCell
Set MiscItemsDBRn = Range(ActiveCell, Cells(ActiveCell.End(xlDown).Row, ActiveCell.End(xlToRight).Column))
ActiveWorkbook.Names.Add Name:="MiscItemsDBRn", RefersTo:="=" &_ MiscItemsDB.Address 'Enter range name in NAME MANAGER
End With 'ws4 = ThisWorkbook.Sheets("MiscItemsDB")
End Sub
Notice in both subs, I refer to the same sheets in the DIM and SET statements so nothing different. I discovered that if I have the 'ws2' sheet displayed, when I run this last macro, that part of the macro works okay, HOWEVER, the same error message will now appear on the '.Cells2,1).Select' under 'With ws4'. What's confusing is that when I ran the first Sub, I didn't have ANY of those three sheets on the screen, yet it worked okay. Where am I going wrong?
I don't know if this matters, but I'm writing this on a computer with Vista and with Excel 2010. It will be used on Windows 10 with Excel 2007. THANK YOU for any help.
TotallyConfused (Boy am I ever)