Accessing a sheet with different vars

lippa

New Member
Joined
Sep 3, 2011
Messages
2
Hello,

Im using Excel 2003 with XP.

I need to write some VBA code to update specific cells within a worksheet and am not sure if it should be a sub or a function (I think function and will refer to it as such, but could well be wrong)

The function will be called from a sub where all the variables are declared.

The function needs to update a specific worksheet which will be defined by a string var P_Name.

It will then need to find the location of 1-4 specific attributes in the range a1:g15 (defined by the constant string variables Name, Age, Location and Join) and update the cell to the right with the variables New_Name, New_Age, New_Location and New_Join)

All the variables will have been declared and their values updated in the various subs that call the function.

I dont know if this helps but I imagine it would look something like this :-

Code:
Function Update_Sheet(P_Name,Attribute,NewValue)

Dim Search_Range as Range
Dim Attribute_Location as Range

Sheets(P_Name).Select

Search_Range = Range("A1:G15")

     'Attribute_Location = Find Attribute in Search_Range
     'Range(Attribute_Location).offset(0,1).value = NewValue

End Function

Where Attribute would be 1 of the 4 possible atrributes to update and NewValue its new value.

I realise doing it this way would mean having to call the function 1-4 times depending on how many attributes need updating and not sure if i should do it that way.

Thanks in advance for any help :)
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Link:Subroutine versus Function

Cannot use Attribute as a variable name. It is a reserved name in VBA.

Here's a simple example of using the .Find method to find a match within a range. You will, no doubt, have to modify the code to suit your needs.


Code:
Sub Test()
    If Update_Sheet("Sheet2", "Jim", "Bob") Then
        MsgBox "Update successful."
    Else
        MsgBox "Update failed."
    End If
End Sub


Function Update_Sheet(P_Name As String, Attrib, NewValue) As Boolean

    Dim Found As Range
    
    On Error GoTo Failed
    
    Set Found = Sheets(P_Name).Range("A1:G15").Find(What:=Attrib, _
                                                    LookIn:=xlValues, _
                                                    LookAt:=xlWhole, _
                                                    SearchOrder:=xlByRows, _
                                                    SearchDirection:=xlNext, _
                                                    MatchCase:=False)
    If Not Found Is Nothing Then
        Found.Offset(, 1) = NewValue
        Update_Sheet = True
    End If
    Exit Function
    
Failed:
    Update_Sheet = False
    
End Function
 
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,558
Members
452,928
Latest member
101blockchains

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