CommandButton showing same total for 3 sheets

Puggwash63

Board Regular
Joined
Jul 27, 2009
Messages
174
I have worked out how to get scores from 3 seperate worksheets to show up on a Worksheet Graph when clicking on the Update Graph buttong, BUT when I thought this was Ok, I have noticed that all 3 worksheet scores are the same even though sheet 1 will show 76, sheet 2 will show 64 & sheet 3 will show 87.
It looks like sheet 2 & 3 are using sheet 1 score but I do not know where th fault lies.
I am using a Macro to genereate the socre onto the Worksheet Graph but do not know if sections need to be changed to show sheet 2 & 3 scores

Does anyone know how to help me with the final part of the puzzle
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
any chance of sharing this macro code...?
 
Upvote 0
sijpie,

This is the code I have got: Can you tell if there is something wrong

Private Sub ClearButton_Click()
Worksheets("AuditForm").Activate
Range("H11:L15,H17:L21,H23:L27,H29:L33,H35:L39").Select
Selection.ClearContents
ComboBox1.Activate


End Sub
Private Sub ComboBox1_Change()
End Sub


Private Sub ComboBox1_Click()



End Sub
Private Sub ComboBox1_DropButt*******()


End Sub
Private Sub ComboBox2_Change()
End Sub
Private Sub ComboBox3_Change()
End Sub
Private Sub UpdateButton_Click()
Set currentCell = Worksheets("5-S Assessment Charts").Range("B13")
While currentCell <> ComboBox1.Text

Set currentCell = currentCell.Offset(0, 1)
Wend
Dim rw As Integer
Dim col As Integer
rw = currentCell.Row + 1
col = currentCell.Column
Worksheets("5-S Assessment Charts").Cells(rw, col) = Worksheets("AuditForm").Range("Total").Value
Set currentCell = Worksheets("5-S Assessment Charts").Range("A20")
End Sub
Private Sub UpdateButton_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
End Sub
Private Sub UpdateButton_GotFocus()
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub
 
Upvote 0
<TABLE class=tborder id=post2656721 cellSpacing=0 cellPadding=6 width="100%" align=center border=0><TBODY><TR vAlign=top><TD class=alt1 id=td_post_2656721 style="BORDER-RIGHT: #ffffff 1px solid">Puggwash63, will that macro work for what I am trying to accomplish:

I have 3 reports that get emailed out to 3 different companies, at the end of the day they fill out the reports and send back to me... what I am wanting to do in excel is when I get those individual reports I want to be able to merge and update the specific cells to the main report...
Individual Reports
EXAMPLE: Company A: 200 emp
Company B: 15 Trained
Company C: 25 ret
MAIN Report:
Company A: 20 emps (now I am wanting to merge and update Company A to 220 emp into main report)
<!-- / message --></TD></TR><TR><TD class=alt2 style="BORDER-RIGHT: #ffffff 1px solid; BORDER-TOP: #ffffff 0px solid; BORDER-LEFT: #ffffff 1px solid; BORDER-BOTTOM: #ffffff 1px solid">Appreciate any help...
thx
</TD><TD class=alt1 style="BORDER-RIGHT: #ffffff 1px solid; BORDER-TOP: #ffffff 0px solid; BORDER-LEFT: #ffffff 0px solid; BORDER-BOTTOM: #ffffff 1px solid" align=right><!-- controls --></TD></TR></TBODY></TABLE>
 
Upvote 0
OK, I understand you are starting to learn VBA. It helps to get some order in your workIn the VBA editor go to the menu Tools/Options...
On the Editor tab ensure that the 'Require Variable Declaration' is ticked. (Basically everything here should be ticked to make your life easy)

What this does is when you open a new module at the top it will auto insert:
Code:
Option Explicit
For your existing modules copy 'Option Explicit' to the very top line.
Now the editor will complain every time you try to run a subroutine where a variable hasn't been declared. This will ensure that typos are caught.

the indent your code.
Only your
Sub mysub()
End Sub
are left alligned. The rest is at least indented once.
Within every loop or if section an extra indentation is required.
This make error finding so much easier.

Then you have a lot of empty subs in your code. Delete them for clarity.
Also separate your subs with some empty lines to make it more readable. Your code should look something like:
Code:
Private Sub ClearButton_Click()
    Worksheets("AuditForm").Activate
    Range("H11:L15,H17:L21,H23:L27,H29:L33,H35:L39").Select
    Selection.ClearContents
ComboBox1.Activate

End Sub
 
Private Sub UpdateButton_Click()
    Dim rw As Integer
    Dim col As Integer
    Dim currentCell As Range
    
    Set currentCell = Worksheets("5-S Assessment Charts").Range("B13")
    While currentCell <> ComboBox1.Text
        Set currentCell = currentCell.Offset(0, 1)
    Wend
    rw = currentCell.Row + 1
    col = currentCell.Column
    Worksheets("5-S Assessment Charts").Cells(rw, col) = Worksheets("AuditForm").Range("Total").Value
    Set currentCell = Worksheets("5-S Assessment Charts").Range("A20")
End Sub
Which is exactly the same as what you had above. But far easier to read.
All this and I haven't even answered your question.

OK the answer to your riddle lies most likely in the secon last line of your code.
(The last line of your code doesn't do anything usefull, more about that later)
Code:
    Worksheets("5-S Assessment Charts").Cells(rw, col) =  _ 
Worksheets("AuditForm").Range("Total").Value

Before this you have set the currentCell to the cell where the heading is the sameas selected in the ComboBox1.
Then you have calculated the position of the cell below it (rw & col)
Then in this line you set this cell (below the correct heading to the value in Worksheets("AuditForm").Range("Total").Value

So that is the same value regardless of what the user chooses.

This mean that the source worksheet (in this example Worksheets("AuditForm")) needs to change with the selection of your Combobox1

OK. then about the last line. This sets the currentCell to something else, but then the sub exits, and so this is not used.

Anyway, it is good practice (as VBA does not do this automatically) to set any objects you declared to 'Nothing' before you exit.
You know probably how Windows slows down over the course of a day due to deminishing resources. This is one of the reasons.

So last line:
set currentCell = Nothing

lastly youdon't need the rw an col. You can access the cell below currentCell with currentCell.Offset(1,0)

Your code then becomes:
Code:
Option Explicit
 

Private Sub ClearButton_Click()
    Worksheets("AuditForm").Activate
    Range("H11:L15,H17:L21,H23:L27,H29:L33,H35:L39").Select
    Selection.ClearContents
ComboBox1.Activate

End Sub
 
Private Sub UpdateButton_Click()
    Dim rw As Integer
    Dim col As Integer
    Dim currentCell As Range
    
    Set currentCell = Worksheets("5-S Assessment Charts").Range("B13")
    While currentCell <> ComboBox1.Text
        Set currentCell = currentCell.Offset(0, 1)
    Wend
    currentCell.Offset(1, 0) = Worksheets("AuditForm").Range("Total").Value
    Set currentCell = Nothing
End Sub
 
Upvote 0
@sdc1234
<TABLE class=tborder id=post2656721 cellSpacing=0 cellPadding=6 width="100%" align=center border=0><TBODY><TR vAlign=top><TD class=alt1 id=td_post_2656721 style="BORDER-RIGHT: #ffffff 1px solid">Puggwash63, will that macro work for what I am trying to accomplish:

I have 3 reports that get emailed out to 3 different companies, at the end of the day they fill out the reports and send back to me... what I am wanting to do in excel is when I get those individual reports I want to be able to merge and update the specific cells to the main report...
<!-- / message -->
</TD></TR><TR><TD class=alt2 style="BORDER-RIGHT: #ffffff 1px solid; BORDER-TOP: #ffffff 0px solid; BORDER-LEFT: #ffffff 1px solid; BORDER-BOTTOM: #ffffff 1px solid">
</TD><TD class=alt1 style="BORDER-RIGHT: #ffffff 1px solid; BORDER-TOP: #ffffff 0px solid; BORDER-LEFT: #ffffff 0px solid; BORDER-BOTTOM: #ffffff 1px solid" align=right><!-- controls --></TD></TR></TBODY></TABLE>

No not really

searchon this website for posts on importing data or on opening other sheets. I just replied to another post with very similar requirement
http://www.mrexcel.com/forum/showthread.php?p=2657189#post2657189
 
Upvote 0
Sijpie,

If I replace the code you have sent me for all 3 worksheets, do I have to change in the codes, the names for each sheet, ie as the 1st will be AuditForm, will the 2nd be Blue AuditForm, 3rd be Yellow AuditForm.

Will the codes then gentrate the required score for each sheet onto the Graph sheet.

As Im not upto speed with Macro & VBA, what help can you give
 
Upvote 0
What you need to do in your UpdateButton_Click code is to check what the value is that the user selected, and then set the spreadsheet to read from dependong on that value. Something like:
Code:
Private Sub UpdateButton_Click()
    Dim rw As Integer
    Dim col As Integer
    Dim currentCell As Range
    Dim WSheet as String
 
    Select Case ComboBox1.Text
        Case "Selection1"
             WSheet = "AuditForm"
 
        Case "Selection2"
             WSheet = "Safety"
        Case "Selection3"
             WSheet = "Coffee"
        Case "Selection4"
             WSheet = "Tea"
   End Select
    Set currentCell = Worksheets("5-S Assessment Charts").Range("B13")
    While currentCell <> ComboBox1.Text
        Set currentCell = currentCell.Offset(0, 1)
    Wend
    currentCell.Offset(1, 0) = Worksheets(WSheet).Range("Total").Value
    Set currentCell = Nothing
End Sub

Of course you will need to change the Selection1-4 to whatever is in the users combobox. And the worksheet names to wherever they have to come from
 
Upvote 0

Forum statistics

Threads
1,224,550
Messages
6,179,461
Members
452,915
Latest member
hannnahheileen

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