+ Reply to Thread
Results 1 to 6 of 6

Macro still wont run

  1. #1
    Bobby
    Guest

    Macro still wont run

    Thanks Jim Thomlinson for supplying this code for me.
    However, now I get "Expected Function or Variable" on the OpenText part

    Dim bk As Workbook, rng As Range
    ChDir "C:\Temp"
    Set bk = Workbooks.OpenText(Filename:="C:\Temp\myfile.txt", _
    Origin:=xlWindows, _
    StartRow:=1, _
    DataType:=xlFixedWidth, _
    FieldInfo:=Array(Array(0, 1), _
    Array(8, 1), Array(12, 1), Array(17, 1), _
    Array(21, 1), Array(29, 1), Array(38, 1), _
    Array(46, 1), Array(50, 1), Array(58, 1), _
    Array(67, 1))) 'Missing last bracket
    Set rng = ActiveSheet.Range("A1").CurrentRegion
    Set rng = rng.Offset(1, 0).Resize(rng.Rows.Count - 1)
    Windows("daily.xls").Activate
    Sheets("table").Select
    Worksheets("table").Unprotect
    rng.Copy Destination:=Range("T2")

    What am I missing now?
    Thanks!


  2. #2
    Tom Ogilvy
    Guest

    Re: Macro still wont run

    OpenText doesn't return a reference, so you can't use
    Set bk =

    you can do it this way:

    Dim bk As Workbook, rng As Range
    ChDir "C:\Temp"
    Workbooks.OpenText Filename:="C:\Temp\myfile.txt", _
    Origin:=xlWindows, _
    StartRow:=1, _
    DataType:=xlFixedWidth, _
    FieldInfo:=Array(Array(0, 1), _
    Array(8, 1), Array(12, 1), Array(17, 1), _
    Array(21, 1), Array(29, 1), Array(38, 1), _
    Array(46, 1), Array(50, 1), Array(58, 1), _
    Array(67, 1))
    set bk = ActiveWorkbook
    Set rng = ActiveSheet.Range("A1").CurrentRegion
    Set rng = rng.Offset(1, 0).Resize(rng.Rows.Count - 1)
    Windows("daily.xls").Activate
    Sheets("table").Select
    Worksheets("table").Unprotect
    rng.Copy Destination:=Range("T2")



    --
    Regards,
    Tom Ogilvy

    "Bobby" <[email protected]> wrote in message
    news:[email protected]...
    > Thanks Jim Thomlinson for supplying this code for me.
    > However, now I get "Expected Function or Variable" on the OpenText part
    >
    > Dim bk As Workbook, rng As Range
    > ChDir "C:\Temp"
    > Set bk = Workbooks.OpenText(Filename:="C:\Temp\myfile.txt", _
    > Origin:=xlWindows, _
    > StartRow:=1, _
    > DataType:=xlFixedWidth, _
    > FieldInfo:=Array(Array(0, 1), _
    > Array(8, 1), Array(12, 1), Array(17, 1), _
    > Array(21, 1), Array(29, 1), Array(38, 1), _
    > Array(46, 1), Array(50, 1), Array(58, 1), _
    > Array(67, 1))) 'Missing last bracket
    > Set rng = ActiveSheet.Range("A1").CurrentRegion
    > Set rng = rng.Offset(1, 0).Resize(rng.Rows.Count - 1)
    > Windows("daily.xls").Activate
    > Sheets("table").Select
    > Worksheets("table").Unprotect
    > rng.Copy Destination:=Range("T2")
    >
    > What am I missing now?
    > Thanks!
    >




  3. #3
    Bobby
    Guest

    Re: Macro still wont run

    Thanks Tom
    The next line of my code is
    Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False

    Now I get "PasteSpecial method of Range class failed"

    What am I missing?
    Thanks!




    "Tom Ogilvy" wrote:

    > OpenText doesn't return a reference, so you can't use
    > Set bk =
    >
    > you can do it this way:
    >
    > Dim bk As Workbook, rng As Range
    > ChDir "C:\Temp"
    > Workbooks.OpenText Filename:="C:\Temp\myfile.txt", _
    > Origin:=xlWindows, _
    > StartRow:=1, _
    > DataType:=xlFixedWidth, _
    > FieldInfo:=Array(Array(0, 1), _
    > Array(8, 1), Array(12, 1), Array(17, 1), _
    > Array(21, 1), Array(29, 1), Array(38, 1), _
    > Array(46, 1), Array(50, 1), Array(58, 1), _
    > Array(67, 1))
    > set bk = ActiveWorkbook
    > Set rng = ActiveSheet.Range("A1").CurrentRegion
    > Set rng = rng.Offset(1, 0).Resize(rng.Rows.Count - 1)
    > Windows("daily.xls").Activate
    > Sheets("table").Select
    > Worksheets("table").Unprotect
    > rng.Copy Destination:=Range("T2")
    >
    >
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    > "Bobby" <[email protected]> wrote in message
    > news:[email protected]...
    > > Thanks Jim Thomlinson for supplying this code for me.
    > > However, now I get "Expected Function or Variable" on the OpenText part
    > >
    > > Dim bk As Workbook, rng As Range
    > > ChDir "C:\Temp"
    > > Set bk = Workbooks.OpenText(Filename:="C:\Temp\myfile.txt", _
    > > Origin:=xlWindows, _
    > > StartRow:=1, _
    > > DataType:=xlFixedWidth, _
    > > FieldInfo:=Array(Array(0, 1), _
    > > Array(8, 1), Array(12, 1), Array(17, 1), _
    > > Array(21, 1), Array(29, 1), Array(38, 1), _
    > > Array(46, 1), Array(50, 1), Array(58, 1), _
    > > Array(67, 1))) 'Missing last bracket
    > > Set rng = ActiveSheet.Range("A1").CurrentRegion
    > > Set rng = rng.Offset(1, 0).Resize(rng.Rows.Count - 1)
    > > Windows("daily.xls").Activate
    > > Sheets("table").Select
    > > Worksheets("table").Unprotect
    > > rng.Copy Destination:=Range("T2")
    > >
    > > What am I missing now?
    > > Thanks!
    > >

    >
    >
    >


  4. #4
    Bobby
    Guest

    Re: Macro still wont run

    My original post may be somewhat unclear. My ultimate goal is to paste the
    contents of myfile.txt to '[Daily.xls]table!T2
    Thanks



    "Bobby" wrote:

    > Thanks Tom
    > The next line of my code is
    > Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
    > False, Transpose:=False
    >
    > Now I get "PasteSpecial method of Range class failed"
    >
    > What am I missing?
    > Thanks!
    >
    >
    >
    >
    > "Tom Ogilvy" wrote:
    >
    > > OpenText doesn't return a reference, so you can't use
    > > Set bk =
    > >
    > > you can do it this way:
    > >
    > > Dim bk As Workbook, rng As Range
    > > ChDir "C:\Temp"
    > > Workbooks.OpenText Filename:="C:\Temp\myfile.txt", _
    > > Origin:=xlWindows, _
    > > StartRow:=1, _
    > > DataType:=xlFixedWidth, _
    > > FieldInfo:=Array(Array(0, 1), _
    > > Array(8, 1), Array(12, 1), Array(17, 1), _
    > > Array(21, 1), Array(29, 1), Array(38, 1), _
    > > Array(46, 1), Array(50, 1), Array(58, 1), _
    > > Array(67, 1))
    > > set bk = ActiveWorkbook
    > > Set rng = ActiveSheet.Range("A1").CurrentRegion
    > > Set rng = rng.Offset(1, 0).Resize(rng.Rows.Count - 1)
    > > Windows("daily.xls").Activate
    > > Sheets("table").Select
    > > Worksheets("table").Unprotect
    > > rng.Copy Destination:=Range("T2")
    > >
    > >
    > >
    > > --
    > > Regards,
    > > Tom Ogilvy
    > >
    > > "Bobby" <[email protected]> wrote in message
    > > news:[email protected]...
    > > > Thanks Jim Thomlinson for supplying this code for me.
    > > > However, now I get "Expected Function or Variable" on the OpenText part
    > > >
    > > > Dim bk As Workbook, rng As Range
    > > > ChDir "C:\Temp"
    > > > Set bk = Workbooks.OpenText(Filename:="C:\Temp\myfile.txt", _
    > > > Origin:=xlWindows, _
    > > > StartRow:=1, _
    > > > DataType:=xlFixedWidth, _
    > > > FieldInfo:=Array(Array(0, 1), _
    > > > Array(8, 1), Array(12, 1), Array(17, 1), _
    > > > Array(21, 1), Array(29, 1), Array(38, 1), _
    > > > Array(46, 1), Array(50, 1), Array(58, 1), _
    > > > Array(67, 1))) 'Missing last bracket
    > > > Set rng = ActiveSheet.Range("A1").CurrentRegion
    > > > Set rng = rng.Offset(1, 0).Resize(rng.Rows.Count - 1)
    > > > Windows("daily.xls").Activate
    > > > Sheets("table").Select
    > > > Worksheets("table").Unprotect
    > > > rng.Copy Destination:=Range("T2")
    > > >
    > > > What am I missing now?
    > > > Thanks!
    > > >

    > >
    > >
    > >


  5. #5
    Tom Ogilvy
    Guest

    Re: Macro still wont run

    Dim bk As Workbook, rng As Range
    ChDir "C:\Temp"
    Workbooks.OpenText Filename:="C:\Temp\myfile.txt", _
    Origin:=xlWindows, _
    StartRow:=1, _
    DataType:=xlFixedWidth, _
    FieldInfo:=Array(Array(0, 1), _
    Array(8, 1), Array(12, 1), Array(17, 1), _
    Array(21, 1), Array(29, 1), Array(38, 1), _
    Array(46, 1), Array(50, 1), Array(58, 1), _
    Array(67, 1))
    set bk = ActiveWorkbook
    Set rng = ActiveSheet.Range("A1").CurrentRegion
    Set rng = rng.Offset(1, 0).Resize(rng.Rows.Count - 1)
    rng.copy
    With Workbooks("daily.xls").Sheets("table")
    .Unprotect
    rng.copy
    .Range("T2").PasteSpecial xlValues
    End With
    ' get rid of the text file "workbook"
    Bk.close Savechanges:=False


    Without seeing your code, it would be hard to tell - there is not
    pastespecial in the code you showed. But I suspect what I provided should
    work. And my guess on your problem is that between copying your data and
    selecting the cell to pastespecial to, you are clearing the clipboard with
    one of your actions - this is very easy to do with code - so there is
    nothing to paste. I always like to get the copy and paste right next to
    each other to avoid this. (as shown above)

    --
    Regards,
    Tom Ogilvy




    "Bobby" <[email protected]> wrote in message
    news:[email protected]...
    > Thanks Tom
    > The next line of my code is
    > Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
    > False, Transpose:=False
    >
    > Now I get "PasteSpecial method of Range class failed"
    >
    > What am I missing?
    > Thanks!
    >
    >
    >
    >
    > "Tom Ogilvy" wrote:
    >
    > > OpenText doesn't return a reference, so you can't use
    > > Set bk =
    > >
    > > you can do it this way:
    > >
    > > Dim bk As Workbook, rng As Range
    > > ChDir "C:\Temp"
    > > Workbooks.OpenText Filename:="C:\Temp\myfile.txt", _
    > > Origin:=xlWindows, _
    > > StartRow:=1, _
    > > DataType:=xlFixedWidth, _
    > > FieldInfo:=Array(Array(0, 1), _
    > > Array(8, 1), Array(12, 1), Array(17, 1), _
    > > Array(21, 1), Array(29, 1), Array(38, 1), _
    > > Array(46, 1), Array(50, 1), Array(58, 1), _
    > > Array(67, 1))
    > > set bk = ActiveWorkbook
    > > Set rng = ActiveSheet.Range("A1").CurrentRegion
    > > Set rng = rng.Offset(1, 0).Resize(rng.Rows.Count - 1)
    > > Windows("daily.xls").Activate
    > > Sheets("table").Select
    > > Worksheets("table").Unprotect
    > > rng.Copy Destination:=Range("T2")
    > >
    > >
    > >
    > > --
    > > Regards,
    > > Tom Ogilvy
    > >
    > > "Bobby" <[email protected]> wrote in message
    > > news:[email protected]...
    > > > Thanks Jim Thomlinson for supplying this code for me.
    > > > However, now I get "Expected Function or Variable" on the OpenText

    part
    > > >
    > > > Dim bk As Workbook, rng As Range
    > > > ChDir "C:\Temp"
    > > > Set bk = Workbooks.OpenText(Filename:="C:\Temp\myfile.txt", _
    > > > Origin:=xlWindows, _
    > > > StartRow:=1, _
    > > > DataType:=xlFixedWidth, _
    > > > FieldInfo:=Array(Array(0, 1), _
    > > > Array(8, 1), Array(12, 1), Array(17, 1), _
    > > > Array(21, 1), Array(29, 1), Array(38, 1), _
    > > > Array(46, 1), Array(50, 1), Array(58, 1), _
    > > > Array(67, 1))) 'Missing last bracket
    > > > Set rng = ActiveSheet.Range("A1").CurrentRegion
    > > > Set rng = rng.Offset(1, 0).Resize(rng.Rows.Count - 1)
    > > > Windows("daily.xls").Activate
    > > > Sheets("table").Select
    > > > Worksheets("table").Unprotect
    > > > rng.Copy Destination:=Range("T2")
    > > >
    > > > What am I missing now?
    > > > Thanks!
    > > >

    > >
    > >
    > >




  6. #6
    Bobby
    Guest

    Re: Macro still wont run

    OK I have found that if I step through the macro it works but if I try to run
    it, it crashes. It appears as though it is not picking up the selection to
    paste. I have modified some to include

    Dim bk As Workbook, rng As Range
    ChDir "C:\Temp"
    Workbooks.OpenText Filename:="C:\Temp\myfile.txt", _
    Origin:=xlWindows, _
    StartRow:=1, _
    DataType:=xlFixedWidth, _
    FieldInfo:=Array(Array(0, 1), _
    Array(8, 1), Array(12, 1), Array(17, 1), _
    Array(21, 1), Array(29, 1), Array(38, 1), _
    Array(46, 1), Array(50, 1), Array(58, 1), _
    Array(67, 1))
    set bk = ActiveWorkbook
    Set rng = ActiveSheet.Range("A1").CurrentRegion
    Set rng = rng.Offset(1, 0).Resize(rng.Rows.Count - 1)
    rng.select
    selection.copy
    Windows('daily.xls").Activate
    Sheets("Table").Select
    Worksheets("table").Unprotect
    Range("t2").Select
    ActiveSheet.Paste


    Thanks

    "Tom Ogilvy" wrote:

    > OpenText doesn't return a reference, so you can't use
    > Set bk =
    >
    > you can do it this way:
    >
    > Dim bk As Workbook, rng As Range
    > ChDir "C:\Temp"
    > Workbooks.OpenText Filename:="C:\Temp\myfile.txt", _
    > Origin:=xlWindows, _
    > StartRow:=1, _
    > DataType:=xlFixedWidth, _
    > FieldInfo:=Array(Array(0, 1), _
    > Array(8, 1), Array(12, 1), Array(17, 1), _
    > Array(21, 1), Array(29, 1), Array(38, 1), _
    > Array(46, 1), Array(50, 1), Array(58, 1), _
    > Array(67, 1))
    > set bk = ActiveWorkbook
    > Set rng = ActiveSheet.Range("A1").CurrentRegion
    > Set rng = rng.Offset(1, 0).Resize(rng.Rows.Count - 1)
    > Windows("daily.xls").Activate
    > Sheets("table").Select
    > Worksheets("table").Unprotect
    > rng.Copy Destination:=Range("T2")
    >
    >
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    > "Bobby" <[email protected]> wrote in message
    > news:[email protected]...
    > > Thanks Jim Thomlinson for supplying this code for me.
    > > However, now I get "Expected Function or Variable" on the OpenText part
    > >
    > > Dim bk As Workbook, rng As Range
    > > ChDir "C:\Temp"
    > > Set bk = Workbooks.OpenText(Filename:="C:\Temp\myfile.txt", _
    > > Origin:=xlWindows, _
    > > StartRow:=1, _
    > > DataType:=xlFixedWidth, _
    > > FieldInfo:=Array(Array(0, 1), _
    > > Array(8, 1), Array(12, 1), Array(17, 1), _
    > > Array(21, 1), Array(29, 1), Array(38, 1), _
    > > Array(46, 1), Array(50, 1), Array(58, 1), _
    > > Array(67, 1))) 'Missing last bracket
    > > Set rng = ActiveSheet.Range("A1").CurrentRegion
    > > Set rng = rng.Offset(1, 0).Resize(rng.Rows.Count - 1)
    > > Windows("daily.xls").Activate
    > > Sheets("table").Select
    > > Worksheets("table").Unprotect
    > > rng.Copy Destination:=Range("T2")
    > >
    > > What am I missing now?
    > > Thanks!
    > >

    >
    >
    >


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1