Range returning info from wrong sheet

G

gtslabs

I have multiple sheets in a workbook that are similar. They track new
projects as they are received.
They all have a command button to call a macro that is in each sheet.
Every day I make copies of this worksheet as new projects arrive and
they are all located in the same workbook. There probably is a better
place to have this code stored but it is in each sheet.

The problem I am having is that the range() values are not always
referencing the information on the current sheet where the command
button was clicked.
At times it looks at the previous sheets cell information and puts it
in the current sheets output. I have to exit excel and restart to get
the correct range information.

How can I make it reference the current called sheet only?

Also where would be a better place to store this code - In a module?
And if so I assume I need to tell the command button on click to
reference the module macro. But how do I tell it to look at the range
info from the sheet the button was clicked?


here is part of the code:


Private Sub CommandButton2_Click()

Dim R, C As Integer

Dim client_contact, client, project, project_number, boring, depth,
sample, lab_Id, test, date_rec As String

client = Range("C4")
project = Range("c5")
project_number = Range("p11")
date_rec = Range("ak12")

Worksheets("input").Range("b3..b6").Value = "" 'clear range
Worksheets("input").Range("b10..k13").Value = "" 'clear range

Worksheets("input").Range("b3").Value = client
Worksheets("input").Range("b4").Value = project
Worksheets("input").Range("b5").Value = project_number
Worksheets("input").Range("b6").Value = date_rec

Etc.......
 
D

Don Guillett

Why not have one macro in a REGULAR module instead of the sheet module and
assign to a regular button or a shape from the drawing toolbar OR a custom
button on your toolbar. Then

Worksheets("input").Range("b3..b6,b10:k13").clearcontents
with activesheet 'notice the dot ( . ) =.range
Worksheets("input").Range("b3").Value = .range("c4")
'etc
end with
 
T

Tom Ogilvy

Private Sub CommandButton2_Click()

Dim R, C As Integer

Dim client_contact, client, project, project_number, boring, depth,
sample, lab_Id, test, date_rec As String

client = me.Range("C4")
project = me.Range("c5")
project_number = me.Range("p11")
date_rec = me.Range("ak12")

Worksheets("input").Range("b3..b6").Value = "" 'clear range
Worksheets("input").Range("b10..k13").Value = "" 'clear range

Worksheets("input").Range("b3").Value = client
Worksheets("input").Range("b4").Value = project
Worksheets("input").Range("b5").Value = project_number
Worksheets("input").Range("b6").Value = date_rec
 

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