How do I get macro to run in just one newly created cell in a new table row?

duteberta

Board Regular
Joined
Jun 14, 2009
Messages
92
Office Version
  1. 365
Platform
  1. MacOS
Here is the VBA script I wrote to add a new table row at top of table and automatically fill in the ID column by running a macro only in that cell. The macro works fine on its own but I cannot get it to work here. The idea is that a new GUID is auto entered into the ID column of my new row.

VBA Code:
Sub AddRecordToTable()

    Dim ws As Worksheet
    Dim newRow As ListRow
    
    Set ws = ActiveSheet

    Set newRow = ws.ListObjects("tbl_PROP").ListRows.Add(1)
    
    With newRow
        .Range("PROP[ID]") = GenerateGUID
    

End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Does this work for you?

VBA Code:
With ws.ListObjects("tbl_PROP")
    .ListRows.Add(1).Range(.ListColumns("ID").Index) = GenerateGUID
End With
 
Upvote 0
Does this work for you?

VBA Code:
With ws.ListObjects("tbl_PROP")
    .ListRows.Add(1).Range(.ListColumns("ID").Index) = GenerateGUID
End With
I inserted your code like this and it does not work. The first line gets highlighted in yellow "Sub AddREcordToTable()"

VBA Code:
Sub AddRecordToTable()

    Dim ws As Worksheet
    Dim newRow As ListRow
    
    Set ws = ActiveSheet

    Set newRow = ws.ListObjects("tbl_PROP").ListRows.Add(1)
    
'    With newRow
'        .Range("PROP[ID]") = GenerateGUID

    With ws.ListObjects("tbl_PROP")
    
        .ListRows.Add(1).Range(.ListColumns("ID").Index) = GenerateGUID
        
    End With
    

End Sub
 
Upvote 0
Do you get an error message? What is that message?
Compile error

Screenshot 2024-04-01 at 10.38.58 PM.png
 
Upvote 0
What is GenerateGUID? Have you perhaps inadvertently defined it in more than one way?
 
Upvote 0
What is GenerateGUID? Have you perhaps inadvertently defined it in more than one way?
That macro works perfectly when activated manually. That macro is the following:

VBA Code:
Function randBetween(ByVal min As Long, max As Long)
    randBetween = Int(Rnd() * (max - min + 1)) + min
End Function


Function GUID()

Dim guid1, guid2, guid3, guid4, guid5, guid6, guid7, guid8 As String

guid1 = LCase(Hex(randBetween(0, 65535)))
guid2 = LCase(Hex(randBetween(0, 65535)))
guid3 = LCase(Hex(randBetween(0, 65535)))
guid4 = LCase(Hex(randBetween(0, 65535)))
guid5 = LCase(Hex(randBetween(0, 65535)))
guid6 = LCase(Hex(randBetween(0, 65535)))
guid7 = LCase(Hex(randBetween(0, 65535)))
guid8 = LCase(Hex(randBetween(0, 65535)))

guid1 = Right(String(4, "0") & guid1, 4)
guid2 = Right(String(4, "0") & guid2, 4)
guid3 = Right(String(4, "0") & guid3, 4)
guid4 = Right(String(4, "0") & guid4, 4)
guid5 = Right(String(4, "0") & guid5, 4)
guid6 = Right(String(4, "0") & guid6, 4)
guid7 = Right(String(4, "0") & guid7, 4)
guid8 = Right(String(4, "0") & guid8, 4)

GUID = guid1 & guid2 & "-" & guid3 & "-" & guid4 & "-" & guid5 & "-" & guid6 & guid7 & guid8

End Function

Sub GenerateGUID()
Dim rng As Range

For Each rng In Selection
    rng.Value = GUID()
Next rng
End Sub
 
Upvote 0
Thanks for posting all your code. That's helpful. Try:

Rich (BB code):
Sub AddRecordToTable()

    Dim ws As Worksheet
    Dim newRow As ListRow
    
    Set ws = ActiveSheet

    With ws.ListObjects("tbl_PROP")
        .ListRows.Add(1).Range(.ListColumns("ID").Index) = GUID
    End With
    
End Sub

I had assumed that your GenerateGUID was a function. Hence, previously, I suggested:
Code:
.ListRows.Add(1).Range(.ListColumns("ID").Index) = GenerateGUID

Here's a simple example to illustrate why you're getting the compile error:

Code:
'Problem!  Hello1 is a Sub, not a function, and hence doesn't have a value to be assigned to s
'Hence a Compile Error: Expected Function or variable
Sub Test1()

    Dim s As String
    
    s = Hello1

End Sub
Sub Hello1()

    Selection.Value = "Hello"

End Sub
'Using a function. No problem.
Sub Test2()

    Dim s As String
    
    s = Hello2
    MsgBox s

End Sub
Function Hello2()

    Hello2 = "Hello"

End Function
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,148
Members
453,021
Latest member
Justyna P

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