How to use Functions in VBA with my current code

Akw47

Board Regular
Joined
Nov 6, 2020
Messages
90
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I am new to VBA. Firstly, the function that I have is :

Function SumNums(pWorkRng As Range, Optional xDelim As String = " ") As Double
Dim arr As Variant
Dim xIndex As Long
arr = Split(pWorkRng, xDelim)
For xIndex = LBound(arr) To UBound(arr) Step 1
SumNums = SumNums + VBA.Val(arr(xIndex))
Next
End Function

Using this, from a cell (etc. F9) filled with characters, 1 White & 2 Red, excel can read 3. However, I need to connect with my other function which based on the value calculated on the same cell (F9), it does the number of times of clicks.

Private Sub CommandButton1_Click()
With Me.CommandButton1
.Tag = Val(.Tag) + 1
If .Tag = SumNums.Cells(9, 6).Value Then
rows(9).Interior.ColorIndex = 10
.Tag = 0
End If
End With
End Sub

But the code ^ there don't work. I also want the row that excel reads to be automatically colour index = 10. Instead of me specifying rows(9).
Thankss.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
How about
VBA Code:
If .Tag = SumNums(Cells(9, 6)) Then
 
Upvote 0
Solution
Yep that changes work, but how do I code it such that the cells it reads will be highlighted automatically
 
Upvote 0
What's wrong with doing it like you have?
At some point you need to tell it which cell/row to look at.
 
Upvote 0
Oh I apologise. :biggrin: Because my future plan was that when I scan a product code & it matches, it will highlight the entire row that match. So if I specify it currently with rows(9). I am scared to face problems later on. Hope you understand ?
 
Upvote 0
oh actually, I plan to use the sumnums function to scan through a specific entire column, so that the excel will know how many time the scan should take place. Which cell to look at is based on the scanned product. Eg. the barcode scanned matches with the row 5 column 4 of the data, then that row should be highlighted after the scanned frequency matches with the sumnums of that row.
 
Upvote 0
I'm afraid you have now lost me completely.
 
Upvote 0
hahahaha, um let my try to explain myself.

Specific Number / Bags
9699 1 White 1 Red

From the second column, I want to use the sumnums function to determine the number of times the specific number have to be scanned. When, the specific number matches with scanned number, it will be considered as 1 time. Before the entire row gets highlighted the determined number of scanned time have to be accomplished. did i explain it better ahhahaa?
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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