VBA tidy up code

tezza

Active Member
Joined
Sep 10, 2006
Messages
375
Office Version
  1. 2016
  2. 2010
Platform
  1. Windows
  2. Web
Hi

I'm just wondering if there's a way to tidy up this piece of coding:

VBA Code:
        celltxt = ActiveSheet.Range("G" & n).Text
           If InStr(1, celltxt, "Shadowing") Then
               Range("T" & n).Value = Range("S" & n)
               Range("U" & n).Value = "0"
               Range("V" & n).Value = "0"
               Range("W" & n).Value = "0"
               Range("X" & n).Value = "0"
               Range("Y" & n).Value = "0"
           End If

It looks in Row G for "Shadowing" then gives a value of Cell S (same row) if true, also Columns U to Y will then be 0
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
This part:
VBA Code:
               Range("U" & n).Value = "0"
               Range("V" & n).Value = "0"
               Range("W" & n).Value = "0"
               Range("X" & n).Value = "0"
               Range("Y" & n).Value = "0"
can be simplified to:
VBA Code:
               Range("U" & n & ":Y" & n).Value = "0"

Though, if you want the number 0 in those cells (and not the text "0"), remove the double-quotes from around the 0.
Double-quotes indicate text entries. Numeric values do not use double-quotes around them.
 
Upvote 0
Solution
This part:
VBA Code:
               Range("U" & n).Value = "0"
               Range("V" & n).Value = "0"
               Range("W" & n).Value = "0"
               Range("X" & n).Value = "0"
               Range("Y" & n).Value = "0"
can be simplified to:
VBA Code:
               Range("U" & n & ":Y" & n).Value = "0"

Though, if you want the number 0 in those cells (and not the text "0"), remove the double-quotes from around the 0.
Double-quotes indicate text entries. Numeric values do not use double-quotes around them.
Thank you and well spotted with the "0" :)
 
Upvote 0
You are welcome!
 
Upvote 0

Forum statistics

Threads
1,215,091
Messages
6,123,062
Members
449,090
Latest member
fragment

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