Merge 2 codes into 1

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,738
Office Version
  1. 2007
Platform
  1. Windows
Hi,
Can you please advise the way to merge the following codes.

Then i will have the 1 button the press to run both codes.

Many Thanks for all your help.

Code:
Sub LEADERBOARD()    '' leaderboard Macro'    Range("C1:F17").Copy Range("I1")
    Worksheets("HONDA SHEET").Range("C1:D17").Copy Worksheets("SOLD ITEMS").Range("C2:D19")
    Worksheets("HONDA SHEET").Range("E1:F17").Copy Worksheets("SOLD ITEMS").Range("C19:D35")


    ActiveWorkbook.Worksheets("SOLD ITEMS").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("SOLD ITEMS").Sort.SortFields.Add Key:=Range("D2"), _
    SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
    xlSortTextAsNumbers
    With Worksheets("SOLD ITEMS").Sort
        .SetRange Range("C2:D35")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With


    With Worksheets("SOLD ITEMS").Range("C2:D35").Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        End With
    Application.Goto Sheets("SOLD ITEMS").Range("A5")
    
    End Sub

Then code below added after the above code,

Code:
Private Sub CommandButton1_Click()Dim myStr As String
Dim x As Integer
Dim myRange As Range


Set myRange = Sheets("SOLD ITEMS").Range("C2:D35")




myData = myRange.Value




For x = 1 To UBound(myData, 1)
    myStr = myStr & myData(x, 1) & vbTab & myData(x, 2) & vbCrLf
Next x




MsgBox myStr




End Sub
 
The message box table is shown fine.

The problem is what looks like a copy of the range C1:F17 also being copied & shown.

See post #5 for the item that needs not to be shown.
 
Last edited:
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Do you have two monitors?
If so, place the VB Editor one screen and the worksheet on the other.
If not, do a split screen so you have the VB Editor on one half and the worksheet on the other.
Then, in the VB Editor, click on the title of the first macro, and press F8. This will allow you to step through your macro, one line at a time, and you can watch and see what happens on the screen at the same time.

So go ahead and do that, and find out what line of code makes that box you do not want to appear. Then you will know what section of code we will need to focus on in order to correct the issue.
 
Upvote 0
Ok
I did that and once the End With is shown in yellow i then see the item appear on the right hand split screen.

Also having said that if i remove this code shown below then the message table is shown on its own without the item we dont want BUT its not in order.

Code:
    With Worksheets("SOLD ITEMS").Sort        .SetRange Range("C2:D35")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply


    End With
 
Upvote 0
OK, back to your message box code, what if you place the range select line just BEFORE the MsgBox line, i.e.

Code:
Range("A1").Select
MsgBox myStr
 
Upvote 0
I assume you mean this is in the Macro10 code if so its the same.

Can the Macro10 code do the sort as opposed to the other code doing the sort ?
The code i mentioned in #23 is the sorting but the issue is not shown hence asking Macro10 to sort it ?
 
Upvote 0
You can try adding the Select statement right under the sort, if you like.
I think we may just to select something in between the sort, and the displaying of the MsgBox.
Where that select happens may not matter, as long as it is somewhere between the two.
 
Upvote 0
OK. I don't think there is anymore that I can do until I am able to download the file.
I should be at a location tonight or this weekend where I can download it and take a look at it.
 
Upvote 0
OK. I downloaded the file, and I cannot recreate the problem you are having.
But I notice that you have frozen panes on that sheet so that you cannot scroll down. Try removing the Freeze Panes and see if that solves your issue.

Otherwise, I have no other suggestions, as I cannot recreate your situation. Your sheet is pretty complex, you may have to enlist the help of a Consultant that you can work with to help you work through it.
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,300
Members
452,633
Latest member
DougMo

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