Results 1 to 4 of 4

Thread: [RESOLVED] Help in writing data to an open workbook in specific columns

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    20

    Resolved [RESOLVED] Help in writing data to an open workbook in specific columns

    In my code below, I am searching the active sheet with the data in an array. When I find the data in the active sheet, I want to paste its address in column A, and the value in column B on my "validation.xls" file, on sheet1. I feel the best place to do this is in my Do loop but I am unsure on how to write to my open validation.xls file, how to write the address in column a, the value in column B, and on the next Do iteration, do the same on the next available row on my validation.xls - sheet1. Could anyone help me with that? Thanks!

    Code:
    Public Sub ec_es_validation()
        Dim MyData As DataObject
        Dim myStr As String
        Dim myStrArray As Variant
        Dim i As Long, x As Long
        Dim wBook As Workbook
        Dim c As Range, Search_Range As Range
          
        fileExists = False
        
        Set MyData = New DataObject
        MyData.GetFromClipboard
        myStr = MyData.GetText(1)
        
        wbOpen = 1
        'Does the validation.xls exist?
        If Not Dir("C:\Temp\validation.xls", vbDirectory) = vbNullString Then 'yes file exists
            'checks to see if the document is open
            On Error Resume Next
            Set wBook = Workbooks("C:\Temp\validation.xls")
            'is file open?
            If wBook Is Nothing Then 'Not open
                Workbooks.Open "C:\Temp\validation.xls"
                Set wBook = Nothing
                On Error GoTo 0
            End If
        Else 'validation.xls does not exist
            'creates the validation.xls spreadsheet
            Set NewBook = Workbooks.Add
            With NewBook
                .Title = "validation"
                .SaveAs Filename:="C:\Temp\validation.xls"
            End With
        End If
       
        myStrArray = Split(myStr, vbNewLine)
        
        For i = LBound(myStrArray) To UBound(myStrArray)
            'myStrArray(i)
            With Search_Range
                Set c = Cells.Find(What:=myStrArray(i), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
                If Not c Is Nothing Then
                    Set Find_Range = c
                        FirstAddress = c.Address
                        FirstColumn = c.Column
                    Do
                        Set Find_Range = Union(Find_Range, c)
                        'write to validate.xls - sheet1 on the next unused cell in column a
                        Set c = Cells.FindNext(c)
                    Loop While Not c Is Nothing And c.Address <> FirstAddress
                End If
            End With
        Next i
        
    End Sub

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    20

    Re: Help in writing data to an open workbook in specific columns

    Also as a side note, when i create my validation.xls file and try to open it manually i am getting a "your file is in a different format that what is specified by the extension. " What am I doing wrong? I have office 2k7 on my computer. Thanks!

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Help in writing data to an open workbook in specific columns

    depending if the workbook exists or is already open you end up with2 objects or no object change this portion then the workbook is always wbook
    vb Code:
    1. If Not Dir("C:\Temp\validation.xls", vbDirectory) = vbNullString Then 'yes file exists
    2.         'checks to see if the document is open
    3.         On Error Resume Next
    4.         Set wBook = Workbooks("C:\Temp\validation.xls")
    5.         'is file open?
    6.         If wBook Is Nothing Then 'Not open
    7.             Set wBook = Workbooks.Open "C:\Temp\validation.xls"
    8.             On Error GoTo 0
    9.         End If
    10.     Else 'validation.xls does not exist
    11.         'creates the validation.xls spreadsheet
    12.         Set wBook = Workbooks.Add
    13.         With wBook
    14.             .Title = "validation"
    15.             .SaveAs Filename:="C:\Temp\validation.xls"
    16.         End With
    17.     End If
    18.     nextrow = wbook.sheets("sheet1").range("A65535").end(xlup).row + 1
    you can now use the wbook object for all your results

    vb Code:
    1. wbook.sheets("sheet1").range("a" & nextrow) = c.address
    2. wbook.sheets("sheet1").range("b" & nextrow) = c.value
    3. nextrow = nextrow + 1

    Also as a side note, when i create my validation.xls file and try to open it manually i am getting a "your file is in a different format that what is specified by the extension. " What am I doing wrong? I have office 2k7 on my computer. Thanks!
    i guess it is because you are using an xls file extension when you use saveAs, you need to either use 2007 extension or specify the correct file format for xls file
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    20

    Re: [RESOLVED] Help in writing data to an open workbook in specific columns

    That did it! I have another question but I will post another topic. Thanks again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width