Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to modify code to run macro form another sheet?

Posted on 2015-01-30
4
Medium Priority
?
162 Views
Last Modified: 2015-01-30
Sub Button4_Click()

' ****** Copies the frame contents from HPSM to Excel
Dim IE As Object
Dim shellWins As New ShellWindows

For Each IE In shellWins
    If IE.LocationName = "HP Service Manager" Then
        On Error Resume Next
        IE.ExecWB OLECMDID_SELECTALL, OLECMDEXECOPT_DODEFAULT
        IE.ExecWB OLECMDID_COPY, OLECMDEXECOPT_DODEFAULT
        Dim MyData As DataObject
        Set MyData = New DataObject
        MyData.GetFromClipboard
        Columns("A:E").Select
        Selection.ClearContents
        Cells(1, 1).Select
        ActiveSheet.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:= _
                False, NoHTMLFormatting:=True
    End If
Next

Set shellWins = Nothing
Set IE = Nothing

On Error Resume Next

' ****** Sorts results by FST login
    Columns("A:A").Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A1"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A1:A1000")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

' ****** Clears unneeded colums from results
    Columns("B:E").Select
    Selection.ClearContents
    Cells(1, 1).Select
    
    Dim i As Integer
    i = 1

' ****** Breaks apart copied FST & count into 2 columns
' BEFORE: Assigned: Benjamin.Pyne (5 items)
' AFTER: Benjamin.Pyne | 5
    Do While Cells(i, 1).Value <> ""
        If Left(Cells(i, 1).Value, 8) <> "Assigned" Then
            Cells(i, 1).ClearContents
        Else
            Dim uName As String
            Dim qCount As Double
            
            Cells(i, 1).Value = Replace(Cells(i, 1).Value, "Assigned: ", "")
            uName = Split(Cells(i, 1).Value, " (")(0)
            qCount = Replace(Split(Cells(i, 1).Value, " (")(1), " items)", "")
            Cells(i, 1) = uName
            Cells(i, 2) = Val(qCount)
        End If
        i = i + 1
    Loop

' ****** Adds header Row
    Range("A1:B1").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("A1").Value = "FST"
    Range("B1").Value = "Count"

End Sub

Open in new window

0
Comment
Question by:kbay808
  • 2
  • 2
4 Comments
 
LVL 28

Expert Comment

by:omgang
ID: 40580722
The code is being called from a button click.  Do you want to add a button to a different worksheet and call the same code?  Or do you want the code to process a different worksheet?  It's explicitly referencing Sheet1 now.
OM Gang
0
 

Author Comment

by:kbay808
ID: 40580809
I want to assign the macro to a button on a different sheet.  The problem is that the macro will run on that sheet an not on the target sheet.
0
 
LVL 28

Accepted Solution

by:
omgang earned 2000 total points
ID: 40580882
OK
So change the code so that it references a specific sheet instead of the current one

Change
        Columns("A:E").Select
        Selection.ClearContents
        Cells(1, 1).Select
        ActiveSheet.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:= _
                False, NoHTMLFormatting:=True

To
        Sheets("Sheet1").Activate
        ActiveSheet.Columns("A:E").Select
        Selection.ClearContents
        ActiveSheet.Cells(1, 1).Select
        ActiveSheet.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:= _
                False, NoHTMLFormatting:=True

Then when you call the macro from a button on another sheet it will still process on Sheet1.
OM Gang
0
 

Author Closing Comment

by:kbay808
ID: 40580963
It worked!!!  Thanks
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question