Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 21

Thread: Solved: Userform writing to worksheet

  1. #1

    Solved: Userform writing to worksheet

    Hi, I currently have a userform which when completed a user clicks submit, which then copies the information to the open worksheet

    Is it possible to send this information instead to a different workbook?

    rather than saying
    With Worksheets("Data").Range("A1")
    .Offset(RowCount, 0).Value = "Change"

    Can i say for example With worksheet{insert filename here}?

    Is it that simple?

    Thanks

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    With Workbooks("thebook.xls").Worksheets("Data").Range("A1")
    .Offset(RowCount, 0).Value = "Change"
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    does workbooks("thebook.xls") have to be open?

    i'm on a network, does that make any difference? The filename is
    \\areaa\folder1\folder2\folder3\thebook.xls

    Thanks

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Yes it does. That way, network is irrelevant.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    you can guess what i'm going to ask now...

    how can i?
    open the workbook
    make the changes via the userform (I know this step)
    save changes
    close the saved workbook

    all without the user knowing.

    Thanks

  6. #6
    VBAX Tutor Benzadeus's Avatar
    Joined
    Dec 2008
    Location
    Belo Horizonte, Brazil
    Posts
    271
    Location
    [VBA]Sub OpenChangeSaveClose()
    Dim _
    wb As Workbook

    Set wb = Workbooks.Open("\\areaa\folder1\folder2\folder3\thebook.xls")

    With wb.Worksheets("Data").Range("A1")
    .Offset(RowCount, 0).Value = "Change"
    wb.Save
    wb.Close
    End With

    Set wb = Nothing
    End Sub[/VBA]

  7. #7
    lovely, thanks - I think it's working...
    although i'm having probelms with my rowcount

    originally the code was;

    RowCount = Worksheets("Data").Range("A1").CurrentRegion.Rows.Count
    With Worksheets("Data").Range("A1")
    .Offset(RowCount, 0).Value = "Change"

    In the new code;

    Dim _
    wb As Workbook
    Set wb = Workbooks.Open("filename.xls")
    RowCount = Worksheets("Data").Range("A1").CurrentRegion.Rows.Count
    With wb.Worksheets("Data").Range("A1")
    .Offset(RowCount, 1).Value = "Change"

    I'm having trouble - it gives me an error

    I presume rowcount helps to add the data to the next free row in the workbook?

    I'm stumped.

  8. #8

    Smile

    hmm, I moved that rowcount piece of code to before open the new workbook

    seems to have done the trick!

  9. #9
    Final question about this damn userform - the boss better be happy!

    how can i make it so all the user sees is the userform?

    i.e. I send them an attachment, or they use sharepoint to open

    and all the user sees is the userform - they don't even know it's excel

    do-able?

    thanks again

  10. #10
    VBAX Tutor Benzadeus's Avatar
    Joined
    Dec 2008
    Location
    Belo Horizonte, Brazil
    Posts
    271
    Location
    On VBA, on the Workbook level,

    [VBA]
    Private Sub Workbook_Open()
    frmNameOfTheUserForm.Show
    End Sub
    [/VBA]

  11. #11
    oops!
    I thought i'd fixed the rowcount thing...

    now what happens is;
    workbook opens
    data gets written to A2:A20

    do another
    workbook opens
    data gets written to A2:A20 - i.e. it overwrites previous...

    what am i doing wrong

    Thanks

  12. #12
    Quote Originally Posted by Benzadeus
    On VBA, on the Workbook level,

    [vba]
    Private Sub Workbook_Open()
    frmNameOfTheUserForm.Show
    End Sub
    [/vba]
    thanks Benzadeus - this is what i have already - the user can still see the sheet behind though, anyway of hiding this?

    thanks

  13. #13
    making a right pigs ear of this thread sorry

    I think I know what's happening
    RowCount = Worksheets("Data").Range("A1").CurrentRegion.Rows.Count

    this I believ assigns a value to rowcount from the data sheet of the next available row

    the trouble is - the workbook that opens has information already in column A - i need it to ignore this, and look at column B instead.

    does that make sense?

  14. #14
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by elsteshepard
    thanks Benzadeus - this is what i have already - the user can still see the sheet behind though, anyway of hiding this?

    thanks
    [vba]

    Application.Hide
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  15. #15
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by elsteshepard
    making a right pigs ear of this thread sorry

    I think I know what's happening
    RowCount = Worksheets("Data").Range("A1").CurrentRegion.Rows.Count

    this I believ assigns a value to rowcount from the data sheet of the next available row

    the trouble is - the workbook that opens has information already in column A - i need it to ignore this, and look at column B instead.

    does that make sense?
    [vba]

    With Worksheets("Data")

    NextFreeRow = .Cells(.Rows.Count, "B").End(xlUp).Row +1
    End With
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  16. #16
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You can also use a function if you need to determine the last cell for various routines. This will take the column letter or number.

    [VBA]
    Sub Test()
    MsgBox NxtCel(6).Row
    MsgBox NxtCel("F").Row
    End Sub

    Function NxtCel(Col) As Range
    Set NxtCel = Cells(Rows.Count, Col).End(xlUp).Offset(1)
    End Function

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  17. #17
    Quote Originally Posted by xld
    [vba]

    With Worksheets("Data")

    NextFreeRow = .Cells(.Rows.Count, "B").End(xlUp).Row +1
    End With
    [/vba]
    sorry to be a complete idiot
    The code i've got so far;

    Set wb = Workbooks.Open("book.xls")

    With wb.Worksheets("Data").Range("A1")
    .Offset(rowcount, 1).Value = "Change"
    etc.
    etc.
    End with

    rowcount currently is defined as
    rowcount = Worksheets("Data").Range("A1").CurrentRegion.Rows.Count

    do I change the definition of rowcount to be NextFreeRow?

    If so, I did this, and with the current code, it entered the data in every other row

    What am I missing - is there another way
    simply put, I need to enter details from a userform into the next available row BUT column A is pre-populated.

    I need help (in more ways than this!)

  18. #18
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Set wb = Workbooks.Open("book.xls")

    With wb.Worksheets("Data")

    NextFreeRow = .Cells(.Rows.Count, "B").End(xlUp).Row + 1
    .Cells(NextFreeRow, "A").Value = "Change"
    etc.
    etc.
    End with
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  19. #19
    I'm getting an error
    Variable not defined for nextfreerow

    NextFreeRow = .Cells(.Rows.Count, "B").End(xlUp).Row + 1
    .Cells(NextFreeRow, "A").Value = "Change"

    etc. just to be clear is

    .cells(nextfreerow, "B").Value = anothertxtbox1.value
    .cells(nextfreerow, "C").Value = anothertxtbox2.value

    etc.

  20. #20
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Just declare it then

    [vba]

    Dim NextFreeRow As Long
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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