Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I've tried changing this around so many different ways based on what I've found online and just can't get it to work. The line "xlWkSht.Rows(Trim(Str(i)) & ":" & Trim(Str(i))).Select" is where the error occurs. Any help would be appreciated. Here's my code:

VB
Dim xlApp As Excel.Application
Dim xlWkBk As Excel.Workbook
Dim xlWkSht As Excel.Worksheet

Set xlApp = CreateObject("Excel.Application")
Set xlWkBk = GetObject(strSaveToPathAndFileName)
Set xlWkSht = xlWkBk.Worksheets(1)


'Find the last row
Dim ExcelLastCell As Object, iLastDataRow As Integer
Set ExcelLastCell = xlWkSht.Cells.SpecialCells(xlLastCell)
iLastDataRow = ExcelLastCell.row

Dim i As Integer
For i = 1 To iLastDataRow
    If Trim(xlWkSht.Cells(i, 1)) <> "" Then
        If Trim(xlWkSht.Cells(i, 8).Value) = "UNKNOWN" Then

            xlWkSht.Activate
            xlWkSht.Rows(Trim(Str(i)) & ":" & Trim(Str(i))).Select
            'xlWkSht.Range("A" & Trim(Str(i)), ":J" & Trim(Str(i))).Select
            xlWkBk.Selection.Font.ColorIndex = 3
            xlWkBk.Selection.Font.Bold = True

        End If
    End If
Next i
Posted
Updated 23-Feb-12 8:02am
v2

Have you tried taking the Str off of Str(i)? If I add Str in VBA it errors but it works without it. The other thing you can try since you are only selecting 1 rows is to do Rows(i).Select
 
Share this answer
 
Comments
avianrand 23-Feb-12 15:52pm    
Thanks, but neither of those solutions work. I've tried many variations on the "i" and even just hard coded things like Range("A1:B2"), etc. The problem doesn't seem to be in the way I'm referencing the cells but in the way I'm referencing the workbook or worksheet or something like that.
ZurdoDev 23-Feb-12 16:16pm    
I don't know if it would make a difference but when I have done it in the past I believe xlApp has an OpenWorkbook method, or something like that, instead of using the GetObject() method. So, .Activate does not throw an error? Also, you may want to try and actually .Open() the workbook. Microsoft's documentation says that Office was never intended to be fully automated so some things really on the UI a lot and it may need to be actually open on the screen for Select to work.
ryanb31's suggestion about an Open method, prompted further investigation. This is the code that finally works well:

VB
Dim xlApp As Excel.Application
Dim xlWkBk As Excel.Workbook
Dim xlWkSht As Excel.Worksheet


Set xlApp = CreateObject("Excel.Application")
'Set xlWkBk = GetObject(strSaveToPathAndFileName)
'Set xlWkSht = xlWkBk.Worksheets(1)

xlApp.Workbooks.Open strSaveToPathAndFileName
Set xlWkSht = xlApp.Worksheets(1)

'Find the last row
Dim ExcelLastCell As Object, iLastDataRow As Integer
Set ExcelLastCell = xlWkSht.Cells.SpecialCells(xlLastCell)
iLastDataRow = ExcelLastCell.row

'xlApp.Visible = True

Dim i As Integer
For i = 1 To iLastDataRow
    If Trim(xlWkSht.Cells(i, 1)) <> "" Then
        If Trim(xlWkSht.Cells(i, 8).Value) = "UNKNOWN" Then

            xlWkSht.Activate
            'xlWkSht.Rows(Trim(Str(i)) & ":" & Trim(Str(i))).Select
            xlWkSht.Rows(i).Select
            xlApp.Selection.Font.ColorIndex = 3
            xlApp.Selection.Font.Bold = True

        End If
    End If
Next i

xlWkSht.Cells(1, 1).Select
xlApp.ActiveWorkbook.Save
xlApp.ActiveWorkbook.Close
xlApp.Workbooks.Close

Set xlWkSht = Nothing
Set xlWkBk = Nothing
Set xlApp = Nothing
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900