Add formula for only visible cells

andrewb90

Well-known Member
Joined
Dec 16, 2009
Messages
1,077
Hello all,

I have this code that I am using to increase cell values based on another cells contents, however I would like not change my cell values if the column in Data1 is hidden.

Code:
Sub add()Sheets("Score").Select
Range("D3").Value = Range("D3") + Sheets("Data1").Range("C3")
Range("E3").Value = Range("E3") + Sheets("Data1").Range("D3")
Range("F3").Value = Range("F3") + Sheets("Data1").Range("E3")
Range("G3").Value = Range("G3") + Sheets("Data1").Range("F3")
Range("H3").Value = Range("H3") + (Sheets("Data1").Range("I3") * Sheets("Data1").Range("H3"))
Range("I3").Value = Range("I3") + (Sheets("Data1").Range("J3") * Sheets("Data1").Range("H3"))
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
maybe...
Code:
Sub MM1()
With Sheets("Score")
    If Sheets("Data1").Columns("D").EntireColumn.Hidden = True Then Exit Sub
        .Range("D3").Value = Range("D3") + Sheets("Data1").Range("C3")
        .Range("E3").Value = Range("E3") + Sheets("Data1").Range("D3")
        .Range("F3").Value = Range("F3") + Sheets("Data1").Range("E3")
        .Range("G3").Value = Range("G3") + Sheets("Data1").Range("F3")
        .Range("H3").Value = Range("H3") + (Sheets("Data1").Range("I3") * Sheets("Data1").Range("H3"))
        .Range("I3").Value = Range("I3") + (Sheets("Data1").Range("J3") * Sheets("Data1").Range("H3"))
End With
End Sub
 
Last edited:
Upvote 0
That cancels my entire sub if one column is hidden, I need to just have that line skipped if the column is hidden.
Using your code, I revised it to this, but it is lengthy already and as I add the remainder of my rows, it'l be really long. Any thoughts on how to shorten this?

Code:
Sub revised()
With Sheets("Score")
    If Sheets("Data1").Columns("C").EntireColumn.Hidden = False Then
        .Range("D3").Value = Range("D3") + Sheets("Data1").Range("C3")
    End If
    If Sheets("Data1").Columns("D").EntireColumn.Hidden = False Then
        .Range("E3").Value = Range("E3") + Sheets("Data1").Range("D3")
    End If
    If Sheets("Data1").Columns("E").EntireColumn.Hidden = False Then
        .Range("F3").Value = Range("F3") + Sheets("Data1").Range("E3")
    End If
    If Sheets("Data1").Columns("F").EntireColumn.Hidden = False Then
        .Range("G3").Value = Range("G3") + Sheets("Data1").Range("F3")
    End If
    If Sheets("Data1").Columns("I").EntireColumn.Hidden = False Then
        .Range("H3").Value = Range("H3") + (Sheets("Data1").Range("I3") * Sheets("Data1").Range("H3"))
    End If
    If Sheets("Data1").Columns("J").EntireColumn.Hidden = False Then
        .Range("I3").Value = Range("I3") + (Sheets("Data1").Range("J3") * Sheets("Data1").Range("H3"))
    End If
End With
End Sub
 
Last edited:
Upvote 0
Try this for that part of the code
Code:
With Sheets("Data1")
  If Not .Columns("C").Hidden Then Range("D3").Value = Range("D3") + .Range("C3")
  If Not .Columns("D").Hidden Then Range("E3").Value = Range("E3") + .Range("D3")
  If Not .Columns("E").Hidden Then Range("F3").Value = Range("F3") + .Range("E3")
  If Not .Columns("F").Hidden Then Range("G3").Value = Range("G3") + .Range("F3")
  If Not .Columns("I").Hidden And Not .Columns("H").Hidden Then Range("H3").Value = Range("H3") + (.Range("I3") * .Range("H3"))
  If Not .Columns("I").Hidden And Not .Columns("J").Hidden Then Range("I3").Value = Range("I3") + (.Range("J3") * .Range("H3"))
End With
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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