×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Using VB to process data in Excel

Using VB to process data in Excel

Using VB to process data in Excel

(OP)
I want to write an app using VB that will allow me to open an XLS file and delete unecessary rows and columns. Also I would like to be able to arrange the columns in one...let say I have a table in excel with different years. I would like to take all the different data for the years and put it in one column.

Can anyone help me start?

Thanks.
Replies continue below

Recommended for you

RE: Using VB to process data in Excel

You'll have to learn the Excel Object Model. It's complex but you can do most anything with it.

Start by getting into Excel and using the Macro recorder to do things like selecting & deleting rows. Then inspect the VBA code it generated. Use Help to look-up the specifics.
Once you understand the Excel object stuff you will be able to open an Excel file from VB and process it behind the scenes.

Following is an excerpt of a macro I used to delete rows. It's not elegant but it works.



Range("A1").Select
intLastRow = LastRow

' Delete blank lines
bDel = True
Do Until bDel = False
bDel = False
Range("A1").Select
For i = 1 To intLastRow
If RowBlank(i) Then
Selection.Cells(i, 1).Select
Selection.EntireRow.Delete (xlShiftUp)
Range("A1").Select
intLastRow = intLastRow - 1
bDel = True
i = intLastRow + 1
ElseIf Cells(i, 1) Like "*TOTAL*" Then
Selection.Cells(i, 1).Select
Selection.EntireRow.Delete (xlShiftUp)
Range("A1").Select
intLastRow = intLastRow - 1
bDel = True
i = intLastRow + 1
End If
Next i
Loop

Function LastRow() As Long
Dim i As Long
For i = 1 To 65000
If RowBlank(i) _
And RowBlank(i + 1) _
And RowBlank(i + 2) _
And RowBlank(i + 3) _
And RowBlank(i + 4) _
And RowBlank(i + 5) Then
LastRow = i - 1
i = 65000
End If
Next i

End Function

Function RowBlank(i) As Boolean
If Cells(i, 1) = "" _
And Cells(i, 2) = "" _
And Cells(i, 3) = "" _
And Cells(i, 4) = "" _
And Cells(i, 5) = "" _
And Cells(i, 6) = "" _
And Cells(i, 7) = "" _
And Cells(i, 8) = "" _
And Cells(i, 9) = "" _
And Cells(i, 10) = "" Then
RowBlank = True
Else
RowBlank = False
End If
End Function

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close