|
-
Mar 17th, 2010, 08:35 AM
#1
Thread Starter
Junior Member
[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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|