Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Appending an excel file

Status
Not open for further replies.

DustoBOE

Technical User
Dec 12, 2013
25
US
' Make Excel visible on the screen
objExcel.Visible = True

' Create a new Workbook
Set objWorkBook = objExcel.Workbooks.Open("C:\Users\*******\Desktop\testcopy.xlsx")

Do

Sess0.Screen.WaitHostQuiet (g_HostSettleTime)
gs = Sess0.Screen.GetString (03, 13, 06) 'Gets sup name
objWorkBook.WorkSheets("Sheet1").Cells(r,1).Value = gs 'Transfers case number to Excel
gs = Sess0.Screen.GetString (14, 19, 11) 'auditor
objWorkBook.WorkSheets("Sheet1").Cells(r,2).Value = gs 'Transfers auditor to Excel
gs = Sess0.Screen.GetString (14, 48, 10) 'Gets aud name
objWorkBook.WorkSheets("Sheet1").Cells(r,3).Value = gs 'Transfers supervisor to Excel


Sess0.Screen.Sendkeys("")
Sess0.Screen.WaitHostQuiet (g_HostSettleTime)
r=r+1
Loop Until objWorkBook.WorkSheets("Sheet1").Cells(r,1).Value = ""

' Excel will remain open after this Sub ends.
' To close out Excel, unremark the following 4 lines of code. .
objExcel.ActiveWorkBook.Save
'objWorkBook.Close
'objExcel.Quit
'set objWorkBook = Nothing
'set objExcel = Nothing




So I'm working with the above code. I simply want to capture some info on a screen, and paste it into different columns. However, the excel file will be added to. So what I want to do it to check each row for a blank cell in Column 1, and if the cell is blank, then paste the information in that row. It the cell already has data, then move to the next row. I can Get info to the workbook, but I'm not sure how to check for a new empty row in the workbook to pate the info into.

Thanks in advance!!
 
That loop in there is not needed, I was trying a buch of stuff and can't edit the post

the following lines should not be in there...
DO..
r=r+1..
Loop....
 
hi,

Code:
'
    Dim r As Long
    
    ' Make Excel visible on the screen
    objExcel.Visible = True
    
    ' Create a new Workbook
    Set objWorkBook = objExcel.Workbooks.Open("C:\Users\*******\Desktop\testcopy.xlsx")
    
    '[b]find the NEXT empty row in column A
    With objWorkBook.Worksheets("Sheet1")
        r = .Cells(.Cells.Rows.Count, 1).End(xlUp).Offset(1).Row
    End With
    
    Do
    'do you stop at at red traffic light and ALWAYS wait x seconds???
    'of course not! So why do you do that here?
    'you wait at the traffic light until you get verification that you can safely go.
        '[s]Sess0.Screen.WaitHostQuiet (g_HostSettleTime)[/s]
      
    'similarly you wait for confirmation that the asynchronous screen is at rest![/b]
        Sess0.Screen.MoveRelative 1, 1
        Do Until Sess0.Screen.WaitForCursor
            DoEvents
        Loop

        gs = Sess0.Screen.GetString(3, 13, 6)    'Gets sup name
        objWorkBook.Worksheets("Sheet1").Cells(r, 1).Value = gs 'Transfers case number to Excel
        gs = Sess0.Screen.GetString(14, 19, 11)  'auditor
        objWorkBook.Worksheets("Sheet1").Cells(r, 2).Value = gs 'Transfers auditor to Excel
        gs = Sess0.Screen.GetString(14, 48, 10)  'Gets aud name
        objWorkBook.Worksheets("Sheet1").Cells(r, 3).Value = gs 'Transfers supervisor to Excel
        
        
        Sess0.Screen.SendKeys ("")
        '[s]Sess0.Screen.WaitHostQuiet (g_HostSettleTime)[/s]
        r = r + 1
    Loop Until objWorkBook.Worksheets("Sheet1").Cells(r, 1).Value = ""

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
oops,

I failed to indicate that the WaitForCursor requires a set of screen coordinates (row, col) that indicate THAT SCREEN's rest coordinates.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 


Also, IMNSHO, the Attachmate Extra! VB Editor/Language is analogous to a Yugo, while the Excel VBA Editor/Language is like driving a fully loaded Cadillac. Not to mention that nearly all the data I manipulate in Extra, start or end up in Excel.

PS: I don't drive a Yugo!

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Hi Skip, thanks for the input.

I'm new to all this programming and have only taken one VB course so I'm trying my best to figure all this stuff out.

As far as the wqaitforcursor, that's a good idea, I'll work on that.

I still don't understand how to get to the next row. That bolded tidbit of code you wrote throws a bug.

With objWorkBook.Worksheets("Sheet1")
r = .Cells(.Cells.Rows.Count, 1).End(xlUp).Offset(1).Row
End With
 

yea, I thought of that and then promptly forgot to follow up (forgive me for my senility at 71).

" throws a bug."
Code:
With objWorkBook.Worksheets("Sheet1")
r = .Cells(.Cells.Rows.Count, 1).End([highlight]-4162[/highlight]).Offset(1).Row 
End With

Excel_VBA_Help said:
XlDirection Enumeration
Specifies the direction in which to move.
Version Information
Version Added: Excel 2007
[pre]
Name Value Description
xlDown -4121 Down.
xlToLeft -4159 To left.
xlToRight -4161 To right.
xlUp [highlight]-4162[/highlight] Up. [/pre]

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
I can tell this editor is not very good. And if you did drove a Yugo, I would not think any less of you LoL!

Will attachmate read that xl enmueration. Because I cant get it to work :(
 
No, not unless you can attach the dynamic link library (.dll) for the Excel version you have.

You must use the [highlight]numeric valu[/highlight]e that the xlUP Excel Application Constant represents.

In the Excel VBA Editor, one can attach the Attachmate Extra! 6.5 Object Library via Tools > Reference.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Is the only way to accomplish what I want through ExcelVBA?
 
Did [highlight]this[/highlight] not work?

Code:
With objWorkBook.Worksheets("Sheet1")
   r = .Cells(.Cells.Rows.Count, 1).End([highlight]-4162[/highlight]).Offset(1).Row 
End With

One CAN perform the task in Extra VB.

However, it is my contention, that one can perform that same task much easier, and perhaps better, in Excel VBA, at least that is my experience.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
no, with the numeric value, still throwing an error.
 
I'd use excel VBA, but I don't really know how to interact with it.
 
It is quite simple. In ANY Microsoft Office Application, alt+F11 toggles between the VB Editor and the application GUI.

Check VBA Help.

In Excel, each procedure is best coded in a Module, that must be INSERTED in the Project Explorer ctr+r.

When you save the workbook (as a Macro Enable Workbook .xlsm) the code is saved with it.

Post VBA questions in forum707. This forum is good for Extra Object questions.

If you code in Excel VBA, you only need CreatObject() etc. for the Extra application, the Excel application is implied in Excel VBA.

Post your specific relevant questions here or in forum707.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
DustoBOE,

see if this makes any sense to you. it's basically checking the cell value.
Code:
do
if trim(objWorkBook.WorkSheets("Sheet1").Cells(r,1).Value) = "" then
objWorkBook.WorkSheets("Sheet1").Cells(r,1).Value = gs
exit do
else
'continue testing for the next row 
end if
loop
 

Sure, but I might do it like this
Code:
'
    Do
        With objWorkBook.Worksheets("Sheet1").Cells(r, 1)
            If Trim(.Value) = "" Then
                .Value = gs
                Exit Do
            Else
            'continue testing for the next row
                r = r + 1
            End If
        End With
    Loop

Or you could get there much more directly, assuming that DATA in column A begins in row 2...
Code:
'
   with objWorkBook.Worksheets("Sheet1")
      .Cells(.Cells(2, 1).End(xlDown).row + 1, 1). value = gs
   end with

The End(xlDown) is like performing these keystrokes on your Excel sheet, where [END] is the END Key...
[tt]
[END] [Down Arrow]
[/tt]


Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Man, both those worked, thanks Skip and Remy.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top