Question about 2 different snippets of VBA code.

DeezNuts

Board Regular
Joined
Aug 12, 2014
Messages
177
I have one code that looks for the value of a hidden cell and places it in another to prevent formula editing by accident. Just curious on this part if this code is as efficient as it can be or can it be shortened or combined.

Here is the one section of it

Code:
    Range("D2").Value = Range("XEX1048549").Value
    Range("E2").Value = Range("XEX1048550").Value
    Range("E3").Value = Range("XFB1048549").Value
    Range("D4").Value = Range("XFB1048550").Value
    Range("E4").Value = Range("XFB1048551").Value


Okay now for the BIGGY :) I have a timestamp script that works based on certain columns I would love to get it modified to only work on certain cells

so it would work like this
Columns 4, 10, 16, 22, 28
StartRow = 6
Skip 3 rows
So in Column 4 cell D6, D10, D14 etc Columns are every 6 so 4,10,16 etc. Here is what I have but it does the full column

Code:
If Target.Row >= 6 Then
Select Case Target.Column
    Case Is = 4, 10, 16, 22, 28
Application.EnableEvents = False
        If Target.Value >= "0" Then
        Target.Offset(0, -1).Value = Now
        Target.Offset(1, -1).Value = "Graded"
    Else
        Target.Offset(1, -1).Value = "Not Graded"
    End If
    Application.EnableEvents = True
End Select
End If
End Sub

I would like the date to not update if an edit is done. Only need the date when the initial grade is input. The date is blank until that grade is input currently but changes when the grade changes( which is a pain for editing/testing the page).
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,402
The 1st code looks good to me.

Here's the "BIGGY" code if I followed your criteria correctly.
Code:
[color=darkblue]Private[/color] [color=darkblue]Sub[/color] Worksheet_Change([color=darkblue]ByVal[/color] Target [color=darkblue]As[/color] Range)
    [color=darkblue]If[/color] Target.Count = 1 [color=darkblue]Then[/color]
        [color=darkblue]If[/color] Target.Row >= 6 And (Target.Row - 2) Mod 4 = 0 [color=darkblue]Then[/color]      [color=green]'Rows 6,10,14...etc[/color]
            [color=darkblue]If[/color] (Target.Column + 2) Mod 6 = 0 [color=darkblue]Then[/color]                   [color=green]'Columns 4, 10, 16, 22, 28...etc.[/color]
                Application.EnableEvents = [color=darkblue]False[/color]
                [color=darkblue]On[/color] [color=darkblue]Error[/color] [color=darkblue]GoTo[/color] ReEnable
                [color=darkblue]If[/color] Target.Value >= 0 [color=darkblue]Then[/color]
                    [color=darkblue]If[/color] Target.Offset(0, -1) = "" [color=darkblue]Then[/color] Target.Offset(0, -1).Value = Now [color=green]'Time stamp once[/color]
                    Target.Offset(1, -1).Value = "Graded"
                [color=darkblue]Else[/color]
                    Target.Offset(1, -1).Value = "Not Graded"
                [color=darkblue]End[/color] [color=darkblue]If[/color]
ReEnable:   [color=green]'Re-enable events[/color]
                Application.EnableEvents = [color=darkblue]True[/color]
                [color=darkblue]If[/color] Err.Number <> 0 [color=darkblue]Then[/color] MsgBox Err.Description, vbCritical, "Error " & Err.Number
            [color=darkblue]End[/color] [color=darkblue]If[/color]
        [color=darkblue]End[/color] [color=darkblue]If[/color]
    [color=darkblue]End[/color] [color=darkblue]If[/color]
End [color=darkblue]Sub[/color]
 
Last edited:

DeezNuts

Board Regular
Joined
Aug 12, 2014
Messages
177
That sir is outstanding thank you very much. Question looking at what you did. I tried this prior to asking

Code:
[COLOR=darkblue]If[/COLOR] Target.Value >= 0 [COLOR=darkblue]Or[/COLOR] Target.Offset(0, -1) = "" Then

Curious why doing it this way did not work for me.
 

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,402
That sir is outstanding thank you very much. Question looking at what you did. I tried this prior to asking

Code:
[COLOR=darkblue]If[/COLOR] Target.Value >= 0 [B][COLOR=darkblue]Or[/COLOR][/B] Target.Offset(0, -1) = "" Then

Curious why doing it this way did not work for me.

You're welcome.

I'm guessing you may have wanted an AND instead of an OR.
Even then, you want the test for the time stamp by itself regardless of the Target.Value....right?
 
Last edited:

DeezNuts

Board Regular
Joined
Aug 12, 2014
Messages
177
okay well I like your way better since it works :) Thank you again I appreciate it.
 

Watch MrExcel Video

Forum statistics

Threads
1,130,051
Messages
5,639,773
Members
417,112
Latest member
PachRedoc

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