Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 9th, 2008, 06:37 PM
Authorized User
 
Join Date: Jun 2008
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to mona_upm84
Default Copy whole sentence that contains "Alarm"

Hi all again,

I need help about this situation:
In alarm.xls, there are 2 sheets. First sheet called "Sheet1" and contains 500 lines of messages in the first column. Second sheet called "Sheet2" contain only a button in cell A1 called "Generate."

I want to copy all messages contains "Alarm" from first column of "Sheet1" into the first column of "Sheet2" by using this "Generate" button.

Please help me????


Regards,
Mona

 
Old September 9th, 2008, 07:07 PM
Registered User
 
Join Date: Aug 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Mona:

Try this...make sure you have declared Option Compare Text in order to make things not case sensitive. Assign "Generate" as the macro to your button.


Option Explicit
Option Compare Text
Sub Generate()
    Dim objSheet1 As Worksheet
    Dim objSheet2 As Worksheet

    Dim lngMaxRow As Long

    Dim i As Integer
    Dim j As Integer
    Dim intPos As Integer
'***
'* . set up worksheet objects
'***
    Set objSheet1 = ThisWorkbook.Worksheets("Sheet1")
    Set objSheet2 = ThisWorkbook.Worksheets("Sheet2")
'***
'* . clear all cells beginning in row 2, column 1 on
'* destination sheet (if there is data
'***
    With objSheet2
        lngMaxRow = .Rows.Count
        lngMaxRow = .Cells(lngMaxRow, 1).End(xlUp).Row
        If lngMaxRow >= 2 Then
            .Range(objSheet2.Cells(2, 1), objSheet2.Cells(lngMaxRow, 1)).Clear
        End If
    End With
'***
'* . determine maximum row in column 1 of source sheet
'* . check each cell to see if it contains "alarm" and copy it
'* . start copy in row 2
'***
    With objSheet1
        lngMaxRow = .Rows.Count
        lngMaxRow = .Cells(lngMaxRow, 1).End(xlUp).Row
        j = 1
        For i = 1 To lngMaxRow
            intPos = InStr(1, .Cells(i, 1).Value, "alarm")
            If intPos > 0 Then
                j = j + 1
                objSheet2.Cells(j, 1).Value = .Cells(i, 1).Value
            End If
        Next i
    End With
'***
'* . display count of matches
'***
    MsgBox (j - 1 & " Messages generated as 'alarm'...")

End Sub
 
Old September 9th, 2008, 07:19 PM
Authorized User
 
Join Date: Jun 2008
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to mona_upm84
Default

Thanks LBAKER,

That coding really helps me... It runs smoothly. I will go though the coding right now to make sure i understand the process of the "Generate" button.

Thank you so much for your instant reply :D

Cheers!!

Regards,
Mona

 
Old September 10th, 2008, 01:03 PM
Registered User
 
Join Date: Aug 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Mona:

You are welcome. FYI, I use the .end(xlUp) method a lot. It basically simulates accessing the last row in the worksheet and striking the Ctrl "up arrow" key to find the last row on the worksheet that contains data. You might also want to check out the .end(xlDown) to get first row with data, .end(xlToLeft) to get last column with data, .end(xlToRight) to get first column with data.

Because the maximum number of rows/columns on a sheet are different between Excel versions, the .rows.count and columns.count will guarantee success in any version.

Lenora





Similar Threads
Thread Thread Starter Forum Replies Last Post
search a text and return the sentence sankar2chat SQL Server 2000 9 July 17th, 2008 02:27 PM
Alarm Program nilgol1 Apache Tomcat 1 April 23rd, 2007 11:29 PM
a bold word in a selected sentence Birger XSLT 4 January 1st, 2007 07:02 PM
Setting an alarm/alert hayley Classic ASP Basics 17 January 20th, 2005 02:20 AM
Pick a sentence out of a memo field stryker43 Crystal Reports 1 October 14th, 2004 05:23 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.