Cannot resolve subscript error excel vba

drluke

Active Member
Joined
Apr 17, 2014
Messages
314
Office Version
  1. 365
Platform
  1. Windows
Have searched the forum, online etc but cannot work out why I am getting "Subscript out of Range" error in the below code.

The error is in this line
Code:
With Worksheets("Journal")
The rest of the with (which forms part of the rest of the code, but this is where the debugger shows the error)
Code:
Sub FuncAllocJnl()
Dim LastCol As Long, x As Long, i As Long
Dim LastRow As Long
Dim rng As Range, c As Range
Dim z As Variant
    Application.ScreenUpdating = False
    
    With Worksheets("Journal")
        .Rows("7:" & Rows.Count).ClearContents
        .Cells(4, 3).Value = Date
        .Cells(4, 5) = Format(Date, "yyyy")
        .Cells(4, 6) = Format(Date, "mm")
        .Cells(4, 6).NumberFormat = "mm"
        .Cells(4, 8).Value = "Functional Allocations" & " " & MonthName(Month(Date)) & " " & Year(Date)
    End With
Worksheet("Journal") exists in the workbook.
Any advise would be appreciated.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
With that error you do not have a sheet called "Journal" in the active workbook.
Do you have more than one workbook open?
Have you checked the sheet name for leading/trailing spaces?
 
Last edited:
Upvote 0
I had another workbook open. The code works perfectly now that I have closed the other workbook. Does this mean that when the workbook with this code is open, no other workbook can be open? It's not uncommon for more than one workbook to be open at the same time.
 
Upvote 0
You can have any number of workbooks open, you just need to qualify the sheets.
So if the sheet is in the same workbook as the code you would use
Code:
With ThisWorkbook.Worksheets("Journal")
otherwise something like
Code:
With Workbooks("Book1.xlsm").Worksheets("Journal")
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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