[VBA] Enter a value in a neighboring cell based on cell occupancy

Martin_H

Board Regular
Joined
Aug 26, 2020
Messages
190
Office Version
  1. 365
Platform
  1. Windows
Hi Team,

let there be a specific range:
VBA Code:
Union(Range("E14:E19,E21:E26,E28:E33,E35:E40,E42:E47,E49:E54,E56:E61,E63:E68,E70:E75,E77:E82,E84:E89,E91:E96,E98:E103,E105:E110,E112:E117,E119:E124,E126:E131,E133:E138,E140:E145,E147:E152"), Range("E154:E159,E161:E166,E168:E173,E175:E180,E182:E187,E189:E194,E196:E201,E203:E208,E210:E215,E217:E222,E224:E229,E231:E236,E238:E243,E245:E250,E252:E257"))

For each occupied E in my range (↑), I want to enter the value 10 in the same row as E, but in the AC column.

For example:
Cell E14 is occupied with text that is formatted as General, macro should insert value 10 in the cell AC14.
The macro should check the entire range and enter a value 10 in the neighboring AC cell for each occupied E.

Thank you for help.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Did you just mean something like this ?
VBA Code:
Sub PopulateAC()
    Dim rng As Range, rCell As Range

    Set rng = Union(Range("E14:E19,E21:E26,E28:E33,E35:E40,E42:E47,E49:E54,E56:E61,E63:E68,E70:E75,E77:E82,E84:E89,E91:E96,E98:E103,E105:E110,E112:E117,E119:E124,E126:E131,E133:E138,E140:E145,E147:E152"), Range("E154:E159,E161:E166,E168:E173,E175:E180,E182:E187,E189:E194,E196:E201,E203:E208,E210:E215,E217:E222,E224:E229,E231:E236,E238:E243,E245:E250,E252:E257"))
    For Each rCell In rng
        If rCell.Value <> "" Then
            Range("AC" & rCell.Row).Value = 10
        End If
    Next rCell

End Sub
 
Upvote 0
Did you just mean something like this ?
VBA Code:
Sub PopulateAC()
    Dim rng As Range, rCell As Range

    Set rng = Union(Range("E14:E19,E21:E26,E28:E33,E35:E40,E42:E47,E49:E54,E56:E61,E63:E68,E70:E75,E77:E82,E84:E89,E91:E96,E98:E103,E105:E110,E112:E117,E119:E124,E126:E131,E133:E138,E140:E145,E147:E152"), Range("E154:E159,E161:E166,E168:E173,E175:E180,E182:E187,E189:E194,E196:E201,E203:E208,E210:E215,E217:E222,E224:E229,E231:E236,E238:E243,E245:E250,E252:E257"))
    For Each rCell In rng
        If rCell.Value <> "" Then
            Range("AC" & rCell.Row).Value = 10
        End If
    Next rCell

End Sub
Exactly what I have been looking for (y)

Would it be also possible to add a comment with some random text to those cells aswell?

Apply same logic as before.

Only add comment with some random text to those cells as value 10.
 
Upvote 0
Do you mean an Excel Comment or Note or just text in the cell ?
How do you want the code to work out what text, is the same text for all rows in AC with the 10 ? Do you want me to just set up a variable that you can change later ?
 
Upvote 0
Do you mean an Excel Comment or Note or just text in the cell ?
How do you want the code to work out what text, is the same text for all rows in AC with the 10 ? Do you want me to just set up a variable that you can change later ?

I mean Excel Comment. Let's just say "Hello", will be the same for all cells in AC with value 10.

I would like to change the text or the value by myself if needed.
 
Upvote 0
OK but can you just confirm you mean the new "Threaded Comment" not the Original Comment that is now called a Note ?
 
Upvote 0
OK but can you just confirm you mean the new "Threaded Comment" not the Original Comment that is now called a Note ?
1653979192099.png

This kind of comment.
 
Upvote 0
See if this gives you what you need.
(and to add to the confusion, although it is not called Note in Excel it is still Comment in VBA)

VBA Code:
Sub PopulateAC()
    Dim rng As Range, rCell As Range
    Dim sCmt As String, cmt As Comment
    
    sCmt = "Hello1"

    Set rng = Union(Range("E14:E19,E21:E26,E28:E33,E35:E40,E42:E47,E49:E54,E56:E61,E63:E68,E70:E75,E77:E82,E84:E89,E91:E96,E98:E103,E105:E110,E112:E117,E119:E124,E126:E131,E133:E138,E140:E145,E147:E152"), Range("E154:E159,E161:E166,E168:E173,E175:E180,E182:E187,E189:E194,E196:E201,E203:E208,E210:E215,E217:E222,E224:E229,E231:E236,E238:E243,E245:E250,E252:E257"))
    For Each rCell In rng
        If rCell.Value <> "" Then
            Range("AC" & rCell.Row).Value = 10
            
            ' Optional if you want to replace an existing comment
            Set cmt = Range("AC" & rCell.Row).Comment
            If Not cmt Is Nothing Then
                Range("AC" & rCell.Row).Comment.Delete
            End If
            ' End Optional
            
            Range("AC" & rCell.Row).AddComment sCmt
        End If
    Next rCell

End Sub
 
Upvote 0
Solution
See if this gives you what you need.
(and to add to the confusion, although it is not called Note in Excel it is still Comment in VBA)

VBA Code:
Sub PopulateAC()
    Dim rng As Range, rCell As Range
    Dim sCmt As String, cmt As Comment
   
    sCmt = "Hello1"

    Set rng = Union(Range("E14:E19,E21:E26,E28:E33,E35:E40,E42:E47,E49:E54,E56:E61,E63:E68,E70:E75,E77:E82,E84:E89,E91:E96,E98:E103,E105:E110,E112:E117,E119:E124,E126:E131,E133:E138,E140:E145,E147:E152"), Range("E154:E159,E161:E166,E168:E173,E175:E180,E182:E187,E189:E194,E196:E201,E203:E208,E210:E215,E217:E222,E224:E229,E231:E236,E238:E243,E245:E250,E252:E257"))
    For Each rCell In rng
        If rCell.Value <> "" Then
            Range("AC" & rCell.Row).Value = 10
           
            ' Optional if you want to replace an existing comment
            Set cmt = Range("AC" & rCell.Row).Comment
            If Not cmt Is Nothing Then
                Range("AC" & rCell.Row).Comment.Delete
            End If
            ' End Optional
           
            Range("AC" & rCell.Row).AddComment sCmt
        End If
    Next rCell

End Sub
Awesome.

Thank you very much @Alex Blakenburg, much appreciated.
 
Upvote 0

Forum statistics

Threads
1,215,157
Messages
6,123,340
Members
449,097
Latest member
thnirmitha

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