|
-
Sep 19th, 2003, 02:30 AM
#1
Thread Starter
Hyperactive Member
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?
-
Sep 19th, 2003, 06:52 AM
#2
In it's simplest form, this is the general idea to do this:
VB Code:
Private Sub CommandButton1_Click()
Dim rngCopyRange As Range
Dim rngPasteRange As Range
rngCopyRange = Range("B1").Value
rngPasteRange = Range("A2").Value
rngPasteRange = rngCopyRange
rngCopyRange = ""
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:
Private Sub CommandButton1_Click()
Range("B1").Value = Range("A2").Value
Range("A2").Value = ""
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:
Private Sub CommandButton1_Click()
Cells(2, 1).Value = Cells(1, 2).Value
Cells(1, 2).Value = ""
End Sub
Last edited by alex_read; Sep 19th, 2003 at 07:02 AM.
-
Sep 19th, 2003, 07:06 AM
#3
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:
Private Sub CommandButton1_Click()
Worksheets(1).select
Range("B1").Value = Range("A2").Value
Range("A2").Value = ""
End Sub
-
Sep 19th, 2003, 08:16 AM
#4
Thread Starter
Hyperactive Member
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...
-
Sep 19th, 2003, 08:26 AM
#5
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!
-
Sep 19th, 2003, 08:41 AM
#6
Thread Starter
Hyperactive Member
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?
-
Sep 19th, 2003, 08:56 AM
#7
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.
-
Sep 19th, 2003, 09:11 AM
#8
Thread Starter
Hyperactive Member
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?
-
Sep 19th, 2003, 09:16 AM
#9
Thread Starter
Hyperactive Member
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 ?
-
Sep 19th, 2003, 10:15 AM
#10
Thread Starter
Hyperactive Member
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?
-
Sep 19th, 2003, 11:21 PM
#11
okay point 1 - lookup the Displayalerts function to supress all of Excel's warning messages:
VB Code:
objXLApp.Displayalerts = false
' Save Code Here
objXLApp.Displayalerts = true
Point 2 - you need the kill statement to delete a file:
VB Code:
Kill "C:\Path\MyXLFile.xls"
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
|