Improve company productivity with a Business Account.Sign Up

x
?
Solved

Macro Excel VBA to verify if cell not empty

Posted on 2014-12-30
12
Medium Priority
?
649 Views
Last Modified: 2014-12-31
Hello,

I need your help in order to:

verify if the cell A1 is empty, if not, apply the vlookup function, if yes, stop.

Not to sure how to explain.  Column A has around 23000 cells with data (next month it could be more, it could be less).
Column B should is a vlookup result if cell A is not empty: if cell Axx not empty, then apply the vlookup, else, nothing
I need to repeat this for many column... check if cell A is empty, if not the apply the vlookup, else, nothing...

Is this possible?  I am trying to automate as much as possible an Excel Spreadsheet that will, at the end, create a "Table" will all pc information i need from different CSV files.
0
Comment
Question by:mldaigle1
  • 6
  • 4
  • 2
12 Comments
 
LVL 55

Expert Comment

by:Rgonzo1971
ID: 40523644
Hi,

Why not integrate in the vlookup formula

=IF ($A1<>"";VLOOKUP(...);"")

or use this code

Sub macro()

Set FirstLine = Range(Range("B1"), Cells(1, Columns.Count).End(xlToLeft))
FirstLine.Offset(1).Resize(Rows.Count - 1).Delete
LastRow = Range("A" & Rows.Count).End(xlUp).Row
FirstLine.Resize(LastRow).FillDown

End Sub

Open in new window


Regards
0
 

Author Comment

by:mldaigle1
ID: 40523689
well, this will work of course, but this is not exactly what i want to know

Let me try to explain again... not to sure if this will be understandable...

is a Macro coudl do this:

check until cell Axx <> ""
          copy vlookup formula in cell Bxx


Is this faisible?
0
 
LVL 55

Expert Comment

by:Rgonzo1971
ID: 40523705
if check while cell Axx <> ""

then try the code above
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
LVL 37

Accepted Solution

by:
Norie earned 2000 total points
ID: 40523771
You could do this by looping but with 23000 rows across multiple columns it's not going to be quick.

It would be pretty straightforward applying the formula to all rows and then doing a paste special values.

For example something like this where I've used a dummy VLOOKUP.
With Range("A2", Range("A" & Rows.COunt).End(xlUp)).Offset(,1)
    .Formula = "=IF(A2="""", """", VLOOKUP(A2, Sheet2!A1:F20000, 6,0))
    .Value = .Value
End With

Open in new window

0
 

Author Comment

by:mldaigle1
ID: 40523805
imnorie, i am so excited!!!  Almost there

I needed to be on the exact sheet to run the macro, but I will run that macro from a main sheet call "Instructions" and I will do the same process for column B to V

column B will check if cell A not empty, then apply the vlookup, else stop
column C will check if cell A not empty, then apply the vlookup, else stop
and so on...
0
 
LVL 37

Expert Comment

by:Norie
ID: 40523820
You don't need to be on the exact sheet, all you need to do is add the appropriate worksheet reference to the code, which I was unable to do because I don't know your sheet names.

Let's say it's Sheet1 you want the VLOOKUPs on, this is the code with the required worksheet reference.
With Sheets("Sheet1")
    With .Range("A2", .Range("A" & Rows.COunt).End(xlUp)).Offset(,1)
        .Formula = "=IF(A2="""", """", VLOOKUP(A2, Sheet2!A1:F20000, 6,0))
        .Value = .Value
    End With
End With

Open in new window

That will apply the code to Sheet1 no matter which sheet is active.

As for doing the other columns that that should be straightforward.

The main thing you need to change is the actual look up though as what I used was just a simple example.
0
 

Author Comment

by:mldaigle1
ID: 40524046
ah ok, just need to repeat the code, incremente the offset by one and adjust the vlookup isn't it?
0
 
LVL 37

Expert Comment

by:Norie
ID: 40524083
You can probably use a loop, but I that really depends on the lookup formula.

Can you post the VLOOKUP you want to use and explain how it will change as it moves across columns?
0
 

Author Comment

by:mldaigle1
ID: 40524093
VLOOKUP(A2,'HC002-SiteCode'!A:B,2,FALSE)
VLOOKUP(A2,'HC003-DomainorWrkGroup'!A:B,2,FALSE)
VLOOKUP(A2,'HC004-LastLogonDomain'!A:B,2,FALSE)
VLOOKUP(A2,'HC006-ChassisType'!A:B,2,FALSE)
VLOOKUP(A2,'HC008-OS_SP'!A:C,3,FALSE)


the data are from 19 imported CSV files... :) and the common info is the pc name... :)
0
 
LVL 37

Expert Comment

by:Norie
ID: 40524193
I take it those formulas would be for columns B to F?

If they are then you could use something like this.
Dim arrRanges As Variant
Dim arrCols As Variant
Dim I As Long
    
    arrRanges = Array("'HC002-SiteCode'!A:B", "'HC003-DomainorWrkGroup'!A:B", "'HC004-LastLogonDomain'!A:B", "'HC006-ChassisType'!A:B", "'HC008-OS_SP'!A:C")

    arrCols = Array(2, 2, 2, 2, 3)

    For I = LBound(arrRanges) To UBound(arrRanges)

        With Sheets("Sheet1")
            With .Range("A2", .Range("A" & Rows.Count).End(xlUp)).Offset(, I + 1)
                .Formula = "=VLOOKUP(A2," & arrRanges(I) & "," & arrCols(I) & ",0)"
               ' .Value = .Value
            End With
        End With
   
    Next I

Open in new window

0
 

Author Comment

by:mldaigle1
ID: 40524207
let me try this and i'll get back to you by tomorrow

:)

thanks for your help
0
 

Author Comment

by:mldaigle1
ID: 40525405
this worked too

thank you so much for your help!

Happy New Year!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

608 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question