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

Darren Smith

Active Member
Joined
Nov 23, 2020
Messages
417
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
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,714
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
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)
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Darren Smith

Active Member
Joined
Nov 23, 2020
Messages
417
Office Version
  1. 2019
Platform
  1. Windows
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.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,714
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
How are you adding them back? Just adding a formula to a cell shouldn't alter its colour.
 

Darren Smith

Active Member
Joined
Nov 23, 2020
Messages
417
Office Version
  1. 2019
Platform
  1. Windows
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
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,714
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS

ADVERTISEMENT

Autofill is the problem there. You can just assign the formula directly like this:

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

Darren Smith

Active Member
Joined
Nov 23, 2020
Messages
417
Office Version
  1. 2019
Platform
  1. Windows
The last range says "Method range of object _Global Failed"?
If I put a brake on the 2nd range the 1st range works
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,714
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS

ADVERTISEMENT

What's the current version of the code and which line is causing the error?
 

Darren Smith

Active Member
Joined
Nov 23, 2020
Messages
417
Office Version
  1. 2019
Platform
  1. Windows
VBA Code:
    Range("P249:299").Formula = "= RoundUp(H249, 0) * O249"

    ActiveWindow.DisplayZeros = False
 

Darren Smith

Active Member
Joined
Nov 23, 2020
Messages
417
Office Version
  1. 2019
Platform
  1. Windows
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.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,714
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
You were just missing a P in there:

Range("P249:P299").Formula
 

Watch MrExcel Video

Forum statistics

Threads
1,129,905
Messages
5,638,911
Members
417,058
Latest member
BRYCEPIETROWIAK

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
Top