VBA to search columns to find a number of variables

daithiboy

Board Regular
Joined
Jul 1, 2016
Messages
77
Hi folks,

I'm back again for more genius advice on how would I achieve the following...

I would like to run a macro that will search for a number of variables and assign them for later calculation. For instance, as simplified version:

I have a sheet with a number of pivot tables on it. I would like the macro to search Column "A" for the term "Total Attendance" and assign the integer value that is one column to the right as a variable ("TotAtt"). Then search column "D" for "New" and, as before, assign the value to the right as a variable ("New").

The hope would be that I could then use the macro to do some basic arithmetic and toss the results out to certain cells on an attendance worksheet e.g. if the variables above have been defined and assigned then I could do the following, this code is a complete roughwork and is probably completey wrong:

Code:
Dim Revisits, TotAtt, New as integer
TotAtt = Search column A for "Total Attendance" and offset one cell to the right
New = Search Column D for "New" and offset one cell to the right
Revisits = TotAtt - New

sheets("Attendance").range("A5").value = Revisits
Sheets("Attendance").Range("B5").value = TotAtt
Sheets("Attendance").Range("C5".value = New

Any thoughts? Does that make any sense at all?

Many thanks in advance,
Dave
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Maybe something like
Code:
Dim TotAtt As Range, myNew As Range
Dim Revisits As Long
Set TotAtt = Range("A:A").find("Total Attendance", , , xlWhole, , , False, False).Offset(, 1)
Set myNew = Range("D:D").find("New", , , xlWhole, , , False, False).Offset(, 1)
If TotAtt Is Nothing Or myNew Is Nothing Then Exit Sub
Revisits = TotAtt.Value - myNew.Value

Sheets("Attendance").Range("A5").Value = Revisits
Sheets("Attendance").Range("B5").Value = TotAtt.Value
Sheets("Attendance").Range("C5").Value = myNew.Value
 
Upvote 0
Glad you got it sorted & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

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