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 November 22nd, 2007, 10:16 PM
Authorized User
 
Join Date: Jun 2005
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need help in Excel VBA codes

Hi,

I need help to do the following:

I have 100 rows of text messages on a sheet named "B_Msg".
I need to transfer every row of these text messages to another sheet "Msg" but before each message, I need to add a row in front with a text called "Name: " and another 2 rows behind this message (1st row contains text "##" and the 2nd row is a blank row).

An example of the end result would be:
Name:
my first message
##

Name:
my second message
##

I tried using For Next but it doesn't work. anyone can help?


 
Old November 24th, 2007, 10:26 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Here you have it:

Sub Copy_Message()

Dim oSht As Worksheet 'Sheet - B_Msg Containing message
Dim oNewSht As Worksheet ' New sheet wehere the message needs to be copied
Dim i1 As Long ' Counter
Dim sMsg As String ' Message in oSht - B_Msg
Dim rNewRange As Range ' Range in the New sheet

Set oSht = Worksheets("B_Msg")
Set oNewSht = Worksheets.Add
Set rNewRange = oNewSht.Range("A2")

For i1 = 2 To oSht.Range("A:A").Cells.SpecialCells(xlCellTypeLas tCell).Row
    ' The above can be modified to suit your range
    sMsg = oSht.Cells(i1, 1).Value
    rNewRange.Value = "Name : "
    Set rNewRange = rNewRange.Offset(1, 0)
    rNewRange.Value = sMsg
    Set rNewRange = rNewRange.Offset(1, 0)
    rNewRange.Value = "##"
    Set rNewRange = rNewRange.Offset(2, 0)


Next i1



End Sub

Cheers
Shasur

http://www.dotnetdud.blogspot.com

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old November 27th, 2007, 05:53 AM
Authorized User
 
Join Date: Jun 2005
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Shasur,

thanks for your help. Your code worked wonderfully!

Thanks.

 
Old April 25th, 2014, 05:28 AM
Registered User
 
Join Date: Apr 2014
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Smile rows filled

Hi All

This is my first ever post, I am also very new to VBA.

I need a code with some explanation and guidance of where to put the code. I tend to get confused, if a code must go in the userform, thisworkbook or in a module.

My question is as follows, let's say i have an excel database and i feed information thru a Userform, now let's say I come to the end of the row in the worksheet and want the the data to automatically go to the next worksheet and start updating from there, still keeping all other information in the first sheet, also if possible, some help with a code to retrieve any information from both sheets.

I thanks you in advance for your support.

Many Thanks

Regards

Mielo786





Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel VBA ever Excel VBA 4 May 28th, 2015 09:19 PM
Code works in Excel VBA but not Access VBA fossx Access VBA 2 May 21st, 2007 08:00 AM
Converting excel data to Access using excel VBA ShaileshShinde VB Databases Basics 1 April 26th, 2006 07:57 AM
VBA Excel jayanp Access VBA 3 December 16th, 2004 03:18 AM
Excel VBA to SQL & back to VBA edesousa Excel VBA 1 June 1st, 2004 02:39 AM





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