Solved

Macro Excel VBA to verify if cell not empty

Posted on 2014-12-30
12
295 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 48

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 48

Expert Comment

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

then try the code above
0
 
LVL 33

Accepted Solution

by:
Norie earned 500 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 33

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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 33

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 33

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Excel 2013 Problem 12 48
Excel - Increse columns in range by 5 6 21
Convert .PDF 6 42
Dynamic Excel Countdown Graphic 21 20
Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

758 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now