Link particular cell to the Macro

LLT

Board Regular
Joined
Nov 4, 2009
Messages
51
Dear Expert out there,

Need a great help from you. Currently I have created a macro which has a input prompt whereby user need to key in the sheet name as shown below. If I do not want the input prompt and instead want the user to key in the sheet name in a particular excel cell and then link it to my macro, how am I suppose to do it?

Thanks in advance for your help!

[Dim lr As Long, r As Long
Dim ws As Worksheet
Dim wsName As String

wsName = InputBox("Input your sheet name")
On Error Resume Next
Set ws = Sheets(wsName)
On Error GoTo 0
If ws Is Nothing Then
MsgBox wsName & " is not a valid sheet name for this workbook"
Else
With ws
lr = .Cells(.Rows.Count, "C").End(xlUp).Row
For r = lr To 1 Step -1
' Classify - REPATRIATION
If .Range("C" & r) Like "*REPAT*" Or .Range("C" & r) Like "*SOUTH SHADY*" Then
.Range("E" & r).Copy Destination:=.Range("F" & r)
End If]
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
try this...

Code:
Dim ws As Worksheet
Dim wsName As String

wsName = range("A1") '<<<<  change to the cell you are using to store sheet name

On Error Resume Next
Set ws = Sheets(wsName)


ps i see you tried to use the code []...
to start a code block use the 'code' keyword inside []
and to finish use the '/code' keyword inside []
 
Upvote 0
Hi,

Using the Worksheet_Change event maybe something like this
(user-entry in A1)

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim wkExists As Boolean, wk As Worksheet
    
    If Not Intersect(Target, Range("A1")) Is Nothing Then
        wkExists = False
        For Each wk In ThisWorkbook.Sheets
            If UCase(wk.Name) = UCase(Range("A1").Value) Then
                wkExists = True
                Exit For
            End If
        Next wk
        
        If wkExists Then
            MsgBox "blah blah"
            'Call your macro here
        Else
            MsgBox Range("A1").Text & " is not a valid sheet name for this workbook"
        End If
    End If
    
End Sub

HTH

M.
 
Upvote 0
Thanks so much, it works!
Currently I have created the macro such that it it will copy the data to a specific sheet. If now I also want user to input the destination sheet into the excel cell. Then how does it works then? Thanks in advance for your help.

Dim lr As Long, r As Long
Dim ws As Worksheet
Dim wsName As String

wsName = Sheets("Instruction").Range("H4")
On Error Resume Next
Set ws = Sheets(wsName)
On Error GoTo 0
If ws Is Nothing Then
MsgBox wsName & " is not a valid sheet name for this workbook"
Else
With ws

.UsedRange.Copy Destination:=Sheets("Cashbook").Range("A" & Rows.Count).End(xlUp).Offset(1)
End With

End If
End Sub
 
Upvote 0
Hi,

Some ideas (untested)

Assuming the destination-sheet name is entered in A2, maybe you can inside the If wkExists then

1. check if the sheet exists, exactly as we did previously with A1-entry

2. Call your macro with a parameter (destination-sheet-name) like
Call MyMacro(Range("A2").Value)

Also you have to change your macro to receive the parameter, something like this

Sub MyMacro(wkDest as String)

and use wkDest in

.UsedRange.Copy Destination:=Sheets(wkDest).Range("A" & Rows.Count).End(xlUp).Offset(1)

HTH

M.
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,261
Members
452,901
Latest member
LisaGo

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