excel marco

J

jkuch248

I need to be able to enter data in one cell and run a macro where
excel would move that data to a worksheet of the same name.

Example: enter 1234 into cell A1. run macro. Cell A1 contents are
transferred to worksheet 1234
 
G

Guest

so sheet 1234 would eventually end up with a bunch of cells containing 1234

Right click on the sheet tab where you will enter data in cell A1 and select
view code. Paste in code like this:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim sh as Worksheet, rng as Range
If Target.Address = "$A$1" then
on Error resume Next
set sh = worksheets(Target.value)
On error goto 0
if not sh is nothing then
if sh.Name = me.Name then exit sub
set rng = sh.Cells(rows.count,1).End(xlup)(2)
rng.value = target.value
on Error goto ErrHandler
Application.EnableEvents = False
Target.ClearContents
end if
end if
ErrHandler:
Application.EnableEvents = True
End Sub

Adapt to suit your real needs.
 
J

JMay

Copy these two code blocks into a standard module:

Sub Foo()
Dim Temp As String
If ActiveCell = Range("a1") Then
Temp = Range("A1").Value
If SheetExists(Temp) Then
Range("A1").Copy Destination:=Sheets(Temp).Range("A1")
Else
Worksheets.Add(After:=Sheets(Sheets.Count)).Name = Temp
Range("A1").Value = Temp
End If
End If
End Sub

Private Function SheetExists(sname) As Boolean
' Returns TRUE if sheet exists in the active workbook
Dim x As Object
On Error Resume Next
Set x = ActiveWorkbook.Sheets(sname)
If Err = 0 Then SheetExists = True _
Else SheetExists = False
End Function

In sheet1 - cell A1 enter jkuck
Run macro Foo

Hope this helps,
Jim May
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top