Return Message box value from VBA script to output variable in Invoke VBA Activity

Sub FindLastColoredCell()
'PURPOSE: Determine Last Cell On Sheet Containing Specific Fill Color

Dim TargetColor As Long
Dim cell As Range
Dim sht As Worksheet
Dim x As Long
Dim lastColoredCell As String

'Choose target color based on currently selected cell
TargetColor = 15132391

'Inversely Loop Through Each Cell In Used Range on ActiveSheet
For x = ActiveSheet.UsedRange.Cells.Count To 1 Step -1
If ActiveSheet.UsedRange.Cells(x).Interior.Color = TargetColor Then
'Notify User of Cell Address [Option 1]
MsgBox "Last Color Found: " & ActiveSheet.UsedRange.Cells(x).Address
lastColoredCell = cstr(ActiveSheet.UsedRange.Cells(x).Address)

  'Select the last cell [Option 2]
    ActiveSheet.UsedRange.Cells(x).Select
 
  'Scroll To The Last Cell [Option 3]
    ActiveWindow.ScrollColumn = ActiveSheet.UsedRange.Cells(x).Column
    ActiveWindow.ScrollRow = ActiveSheet.UsedRange.Cells(x).Row
 
  'Exit Loop
    Exit Sub
End If

Next x

End Sub

“I need the msg box value to be sent to the output value of the activity” I highlighted the message box value here.
I tired to assign the value to “lastColoredCell” variable but it does not work.
@Yoichi

@Sairam_RPA - I think you need to use a Function instead of a Sub. The Function can return a value like:

Function FindLastColoredCell()
	'PURPOSE: Determine Last Cell On Sheet Containing Specific Fill Color

	Dim TargetColor As Long
	Dim cell As Range
	Dim sht As Worksheet
	Dim x As Long
	Dim lastColoredCell As String

	'Choose target color based on currently selected cell
	TargetColor = 15132391

	'Inversely Loop Through Each Cell In Used Range on ActiveSheet
	For x = ActiveSheet.UsedRange.Cells.Count To 1 Step -1
		If ActiveSheet.UsedRange.Cells(x).Interior.Color = TargetColor Then
			'Notify User of Cell Address [Option 1]
			MsgBox "Last Color Found: " & ActiveSheet.UsedRange.Cells(x).Address
			lastColoredCell = cstr(ActiveSheet.UsedRange.Cells(x).Address)

			'Select the last cell [Option 2]
			ActiveSheet.UsedRange.Cells(x).Select

			'Scroll To The Last Cell [Option 3]
			ActiveWindow.ScrollColumn = ActiveSheet.UsedRange.Cells(x).Column
			ActiveWindow.ScrollRow = ActiveSheet.UsedRange.Cells(x).Row

			'Exit Loop
			FindLastColoredCell = lastColoredCell
			Exit Function
		End If
	Next x
End Function

And then you can store that value in the Output variable of the Invoke VBA activity:
image

image

1 Like

Thankyou for the help. Appreciate it! :grinning:

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.