Results 1 to 11 of 11

Thread: Urgent!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    332

    Urgent!

    I need to modify locations of data in an excel worksheet!

    For example, I am utilising 4 columns (A,B,C,D)
    In row 1, I have A storing "Test1", B storing "Test2"
    On the 2nd row, I have C storing "Test3", D storing "Test4"

    When I execute my VB code, the data in row 1 column B has to be moved below A, and D below C.

    How do I modify locations? And how should i access the worksheet? Any article which can show me how to pin point hte locations?

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    In it's simplest form, this is the general idea to do this:

    VB Code:
    1. Private Sub CommandButton1_Click()
    2.     Dim rngCopyRange As Range
    3.     Dim rngPasteRange As Range
    4.    
    5.     rngCopyRange = Range("B1").Value
    6.     rngPasteRange = Range("A2").Value
    7.    
    8.     rngPasteRange = rngCopyRange
    9.     rngCopyRange = ""
    10. End Sub

    You can cell on the range object & specify Letter & number characters to rference 1 or more cells ("A1", "A1:B5" etc.)
    VB Code:
    1. Private Sub CommandButton1_Click()
    2.     Range("B1").Value = Range("A2").Value
    3.     Range("A2").Value = ""
    4. End Sub
    OR...

    You can call on the cells object & specify a numerical value for the row, then another number for the column - so cells(1,1) would be the same as range("A1") if that makes sense:
    VB Code:
    1. Private Sub CommandButton1_Click()
    2.     Cells(2, 1).Value = Cells(1, 2).Value
    3.     Cells(1, 2).Value = ""
    4. End Sub
    Last edited by alex_read; Sep 19th, 2003 at 07:02 AM.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    For the last point there, to specify a worksheet you can either call on the sheet id, or the sheet name:

    Msgbox Worksheets(1).Range("A1").value

    Or

    Msgbox Worksheets("Sheet1").Range("A1").value

    Will give you the same result. I would normally select the worksheet I want to work with first, then call on the range etc. objects without putting the worksheets(1). call before them. i.e. -
    VB Code:
    1. Private Sub CommandButton1_Click()
    2.     Worksheets(1).select
    3.  
    4.     Range("B1").Value = Range("A2").Value
    5.     Range("A2").Value = ""
    6. End Sub

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    332
    thanx, very helpful, will try and see how it goes...

    i know how to create an object for excel worksheets and save 'em...but how do i open an existing file, not as in physically open it, but i want to gain access to it to perform the aforementioned solutions onto the problem...

  5. #5
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    When you first create your excel object, use the following:

    objXLApp.Workbooks.add "C:\Path\MyFile.xls"

    Where the objXLApp is your created Excel.Application object reference. Hope this helps!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    332
    is there a way of detecting the end of an excel worksheet
    say i want to check row by row, how do i know when to stop?

  7. #7
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Suppose these should all really go into new posts, but what the hell. Okay there's an Excel object called 'UsedRows' which is incredibly useful for this!

    You can call on the rows.count function to retreive the last row which has an entry in it (note thi starts at 0):
    MsgBox ActiveSheet.UsedRange.Rows.Count +1

    The columns.count function to find the last column with an entry in:
    MsgBox Worksheets(1).UsedRange.Columns.Count

    Or you can retrieve the actual cell reference of the last cell which has an entry in bu using the SpecialCells call:
    MsgBox Cells.SpecialCells(xlCellTypeLastCell).Address
    Last edited by alex_read; Sep 19th, 2003 at 09:04 AM.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    332
    yeah, but this assumes i have to have defined a range earlier on...
    anyway working without a range?
    say my range would be the whole document, until the row where there is no more data...is that possible?

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    332
    this is just a snip of my code so far...

    Dim path As String
    Dim workSheetName As String
    Dim oApp As Excel.Application
    Dim oWrkBk As Excel.Workbook
    Dim oSheet As Excel.Worksheet
    Dim intRow As Integer
    Dim intCol As Integer

    oApp = CreateObject("Excel.Application")
    oApp.Visible = False
    path = App.path & "\testSheet.xls"
    oWrkBk = oApp.Workbooks.Add(path)


    i get an error msg stating "Invalid use of property" with this line :

    oApp = CreateObject("Excel.Application")

    wat's wrong with it ?

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    332
    okay, fixed that problem...
    i think i was using vb.net syntax not vb 6

    i have a code to saveas a file...
    wat if i dun want the confirmation dialog box to replace a file to pop up, is there a way to just forced the overwrite?

    wat about deleting a file?

  11. #11
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    okay point 1 - lookup the Displayalerts function to supress all of Excel's warning messages:
    VB Code:
    1. objXLApp.Displayalerts = false
    2.     ' Save Code Here
    3. objXLApp.Displayalerts = true

    Point 2 - you need the kill statement to delete a file:
    VB Code:
    1. Kill "C:\Path\MyXLFile.xls"

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

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