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 July 14th, 2006, 12:49 PM
Authorized User
 
Join Date: Feb 2005
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Finding Adjacent Empty Cell

Hello all,

lets say you have 1 column (A) of data and it goes to row 5, You insert a new Column A and shift everything to the right. So now you have a blank Column A and a Column B that has 5 rows of data.

How do I find the cell in Column A that would match the last row of data in Column B which would be 5, So that i can fill Column A with data

I was thinking i can find the last cell of data in Column B. could i than move the cell selector over to the left by 1 and fill up? And if so how do i do that?

currently this is what i have but it's not working out so well
Code:
Columns("A:A").Select
    Application.CutCopyMode = False
    Selection.Insert Shift:=xlToRight



    Columns("B:B").Select
    Selection.Find(What:="", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate

    lastCell = ActiveCell.Address
    'RangeObject.Insert (xlShiftToRight)

    Range("A1:" & lastCell).Select



Thanks in advance. I hope it is clear

Oh i'm using Excel Xp

Thanks
Keith

 
Old July 14th, 2006, 12:54 PM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
Default

This is how I would do it, but there could very well be a better way...

Code:
i = 1
While Columns("B" & i).Value = ""
    i = i + 1
Wend

Columns("A" & i).Select

Selection.Value = "Whatever you want to put in here!:"
HTH

Mike

Mike
EchoVue.com
 
Old July 14th, 2006, 02:08 PM
Authorized User
 
Join Date: Feb 2005
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Mike,

Thanks so much for that. It's amazingly simple, my way was um slightly complex and very dumb.

When i step through the code though i'm getting

Run-time error '1004':
Application-defined or object-defined error

I used

Debug.Print i = 1
While Columns("B" & i).Value = ""
Debug.Print i = i + 1
    Wend

    Columns("A" & i).Select

    Selection.Value = "Whatever you want to put in here!:"

and as I stepped into the loop it said false in the intermediate window and gave me an error

Run-time error '13':
Type mismatch

I'm not sure what's going wrong

Thanks,
Keith
 
Old July 14th, 2006, 03:04 PM
Authorized User
 
Join Date: Feb 2005
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

So i've made some changes to make the error go away.

I've done a little looking into and changed the loop to look like this. Only cause this compiles

Code:
i = 1
Do While Not IsEmpty("B" & i)
debug.print i    
    i = i + 1
Loop
Columns("A" & i).Select
It doesn't error, But when i run it, the loop runs till it crashes. I put the Debug.print i in and it counts up the rows till it crashes. Even though there is information in col b it never stops when it reaches an empty cell.
here is my table
A B
gg gg
dd dd
dd dd
vv vv
ff ff
hh hh
jj jj

Am i not understanding the way IsEmpty works?

 
Old July 14th, 2006, 03:17 PM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
Default

I Have never use isEmpty before, I'm afraid. If you were to go back to the previous code. On which lines are you getting the errors.

Error 13, generally means it doesn't know which sheet to to.

Try instead of Columns("A" & i)


 While ActiveSheet.Range("A" & i).Value <> ""


Let me know how that goes.

Mike


Mike
EchoVue.com
 
Old July 14th, 2006, 03:28 PM
Authorized User
 
Join Date: Feb 2005
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That was the ticket. I missed the active.sheet but it works perfectly.

Thanks for you help.

Keith
 
Old July 14th, 2006, 03:36 PM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Anytime! Have a great weekend!

Mike

Mike
EchoVue.com
 
Old July 19th, 2006, 06:20 AM
SMI SMI is offline
Authorized User
 
Join Date: Jul 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dear all
I guess the approach could be using one line of code as follows:

Range("B1").End(xlDown).Offset(0, -1).Value = "what ever you want"

Try this and let me know if you have any problem.

SMI

Genius is 99% perspiration and 1% inspiration





Similar Threads
Thread Thread Starter Forum Replies Last Post
cell is empty while using For Each cell In Range jase2007 Excel VBA 4 April 5th, 2012 10:20 PM
[2005] GridView - empty cell shows no border snufse ASP.NET 2.0 Basics 2 July 29th, 2008 02:49 PM
[VB2005]Finding last cell of the column in excel bellaelysium Visual Basic 2005 Basics 2 June 22nd, 2008 11:33 PM
Using - "group-adjacent" ROCXY XSLT 4 January 4th, 2006 11:09 AM
Using Two "group-adjacent" ROCXY XSLT 0 January 4th, 2006 08:29 AM





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