Hi All,
I'm Trying my best to make this VBA code perform in a way that was not intended when written, Admittedly I'm a novice here,
I'll post the code below I'm sure it's only the Worksheet_SelectionChange Sub that needs addressing I'm just missing something,
I simply would like it to copy the text form B5 and then paste into the application, I really don't expect a solution here as the it won't be visible to you but you may see something I don't,
This code would normally perform the task of copying the information from the application and then post it into the work book, I just want to be able to click a cell and have the number appear in the application,
Appreciate any help given again I'm just throwing this one to the wind I don't expect a solution
I'm Trying my best to make this VBA code perform in a way that was not intended when written, Admittedly I'm a novice here,
I'll post the code below I'm sure it's only the Worksheet_SelectionChange Sub that needs addressing I'm just missing something,
I simply would like it to copy the text form B5 and then paste into the application, I really don't expect a solution here as the it won't be visible to you but you may see something I don't,
This code would normally perform the task of copying the information from the application and then post it into the work book, I just want to be able to click a cell and have the number appear in the application,
Appreciate any help given again I'm just throwing this one to the wind I don't expect a solution
VBA Code:
'Constants
Const CO_HepFileName = "SYA2_Mainframe"
Const CO_DataSheet = "Data 3"
Const CO_Status = "Update Status"
Const CO_Message = "Update Message"
'Hummingbird application and host objects
Private moHostApp As HostExApplication
Private moHostExp As HostExHost
'Sheet row variables
Private mlDataRow As Long
Private miStatusCol As Long
Private miMessageCol As Long
Private msPartNumber As String
'Data record variables
'Data record variable for checking
Private msStatus As String
Private msMessage As String
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("A2:A3000")) Is Nothing Then
Application.ScreenUpdating = False
Worksheets("Data 3").Range("B5") = Range(Target.Address).Value
If MsgBox("Do you wish to run the Hummingbird script?", vbYesNo) = vbNo Then
Exit Sub
End If
'Try and connect to the Hummingbird session
If Not ConnectToSession Then
Exit Sub
End If
With moHostExp
For I = 3 To cntr
msPlant = Worksheets("Data 3").Range("B5")
moHostExp.CursorRC 2, 71
moHostExp.RunCmd "Erase-Input"
moHostExp.PutText msPlant, 2, 71
moHostExp.RunCmd "Enter"
WaitForScreen
.RunCmd "Home"
.Keys "GCMMSGABA"
.RunCmd "Enter"
WaitForScreen
msPartNumber = Worksheets("Data 3").Range("B5")
.CursorRC 3, 8
.RunCmd "ERASE-LINE"
.PutText msPartNumber, 3, 8
.RunCmd "Enter"
WaitForScreen
Application.ScreenUpdating = True
Next
End With
End If
End Sub
'Writes a message and Status to the data sheet
Private Sub WriteLog(sStatus As String, sMsg As String)
With Worksheets(CO_DataSheet)
.Cells(mlDataRow, miStatusCol) = sStatus
.Cells(mlDataRow, miMessageCol) = sMsg
End With
End Sub
'Get the message from the bottom of the screen
Private Function ScreenMessage() As String
On Error Resume Next
ScreenMessage = Trim(moHostExp.TextRC(24, 2, 80))
End Function
'Connect to the Hummingbird Host Explorer session
Private Function ConnectToSession() As Boolean
On Error GoTo Error_Handler
Set moHostApp = New HostExApplication
Set moHostExp = moHostApp.CurrentHost
If moHostExp Is Nothing Then
MsgBox "There is no '" & CO_HepFileName & "' Hummingbird session running.", vbCritical
ConnectToSession = False
Else
ConnectToSession = True
End If
Exit Function
Error_Handler:
ConnectToSession = False
End Function
'Disconnect from the Hummingbird Host Explorer session
Private Function DisconnectFromSession()
On Error Resume Next
Set moHostExp = Nothing
Set moHostApp = Nothing
End Function
'Wait for the screen to refresh after an action key has been pressed (e.g. "Enter" of "Pf8")
Private Sub WaitForScreen()
'Two loops, just to make sure...
While moHostExp.Keyboard
Wend
While moHostExp.Keyboard
Wend
End Sub
'Try to find and set variables pointing to the status and message columns in the data sheet. It creates these columns if they
'do not already exist.
Private Function SetStatusColumns() As Boolean
Dim iCol As Integer
miStatusCol = 0
miMessageCol = 0
'Try and find update status and message columns
With Worksheets(CO_DataSheet)
iCol = 1
While .Cells(1, iCol) <> ""
If .Cells(1, iCol) = CO_Status Then
miStatusCol = iCol
End If
If .Cells(1, iCol) = CO_Message Then
miMessageCol = iCol
End If
iCol = iCol + 1
Wend
'If an update status column does not exist then add it to the end
If miStatusCol = 0 Then
miStatusCol = AddHeading(CO_Status)
End If
'If an update message column does not exist then add it to the end
If miMessageCol = 0 Then
miMessageCol = AddHeading(CO_Message)
End If
End With
End Function
Private Function AddHeading(sHeading As String) As Integer
Dim iCol As Integer
'Find first non blank column heading
iCol = 1
While Worksheets(CO_DataSheet).Cells(1, iCol) <> ""
iCol = iCol + 1
Wend
'Add the heading and set background colour
Worksheets(CO_DataSheet).Cells(1, iCol) = sHeading
Worksheets(CO_DataSheet).Cells(1, iCol).Interior.ColorIndex = 15
'Set the borders
With Worksheets(CO_DataSheet).Cells(1, iCol).Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Worksheets(CO_DataSheet).Cells(1, iCol).Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Worksheets(CO_DataSheet).Cells(1, iCol).Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Worksheets(CO_DataSheet).Cells(1, iCol).Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
AddHeading = iCol
End Function
Private Sub Workbook_Open()
On Error Resume Next
Application.CommandBars("Hummingbird").Controls(1).OnAction = "ThisWorkBook.RunScript"
End Sub