Can`t seem to get my code to recognize my SpreadSheet

Darren Smith

Well-known Member
Joined
Nov 23, 2020
Messages
631
Office Version
  1. 2019
Platform
  1. Windows
Problem is the worksheet for some reason will not work. It says ws = Nothing?? I have set it up same as before baffled.
I tried to set a workbook variable Then using Thisworkbook.Sheets still no luck.
Don`t Know of any other way it could or should work

VBA Code:
Private Sub Add_Break_Lines_Click()

                              
                Dim Com As ComboBox
                Dim ws As Worksheet
                Dim wb As Workbook
                
                
                Set wb = Workbooks("Automated Cardworker")
                Set ws = wb.Worksheets("Job Card Master")
                Set Com = Me.Add_Break_Lines
                

            
               With ws
               
                Select Case Com.Value
                    Case ("Break Lines 1 Page Job Card")
                        Color .Range("A13:Q61")
            
                    Case ("Break Lines 2 Page Job Card")
                        Color .Range("A13:Q61")
                        Color .Range("A66:Q120")
               
                    Case ("Break Lines 3 Page Job Card")
                         Color .Range("A13:Q61")
                        Color .Range("A66:Q122")
                        Color .Range("A127:Q178")
                        
                    Case ("Break Lines 4 Page Job Card")
                        Color .Range("A13:Q61")
                        Color .Range("A66:Q122")
                        Color .Range("A127:Q183")
                        Color .Range("A188:Q244")
            
                    Case ("Break Lines 5 Page Job Card")
                        Color .Range("A13:Q61")
                        Color .Range("A66:Q122")
                        Color .Range("A127:Q183")
                        Color .Range("A188:Q244")
                        Color .Range("A249:Q299")
                End Select
                
            End With

            End Sub
 
You said "If I add formulas back in the cell colors in column P disappear" which should only happen if you copied a cell without colour to put the formula back (or if you run your code again without removing the formulas first)
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
You said "If I add formulas back in the cell colors in column P disappear" which should only happen if you copied a cell without colour to put the formula back (or if you run your code again without removing the formulas first)
My code does remove formulas to start with then adds them back at the end. But when they add back in they delete the color of the cell.
 
Upvote 0
How are you adding them back? Just adding a formula to a cell shouldn't alter its colour.
 
Upvote 0
I am adding them back using a List box command see below.

VBA Code:
     If ListBox4 = ("Formatting & Formula Update") Then

    Range("P13").Formula = "= RoundUp(H13, 0) * O13"
    Range("P13").AutoFill Range("P13:P61")

    Range("P66").Formula = "= RoundUp(H66, 0) * O66"
    Range("P66").AutoFill Range("P66:P122")
   
    Range("P127").Formula = "= RoundUp(H127, 0) * O127"
    Range("P127").AutoFill Range("P127:P183")
   
       
    Range("P188").Formula = "= RoundUp(H188, 0) * O188"
    Range("P188").AutoFill Range("P188:P244")
   
       
    Range("P249").Formula = "= RoundUp(H249, 0) * O249"
    Range("P249").AutoFill Range("P249:P299")

   End If

    End Sub
 
Upvote 0
Autofill is the problem there. You can just assign the formula directly like this:

Code:
Range("P13:P61").Formula = "= RoundUp(H13, 0) * O13"
 
Upvote 0
Solution
The last range says "Method range of object _Global Failed"?
If I put a brake on the 2nd range the 1st range works
 
Upvote 0
What's the current version of the code and which line is causing the error?
 
Upvote 0
VBA Code:
    Range("P249:299").Formula = "= RoundUp(H249, 0) * O249"

    ActiveWindow.DisplayZeros = False
 
Upvote 0
I`ve used a Combobox to fill the formulas all works fine.
Your other advice I have done what you recommended thanks for your help.
 
Upvote 0
You were just missing a P in there:

Range("P249:P299").Formula
 
Upvote 0

Forum statistics

Threads
1,213,495
Messages
6,113,992
Members
448,538
Latest member
alex78

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