Solved

Macro Excel VBA to verify if cell not empty

Posted on 2014-12-30
12
304 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 49

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 49

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
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…

911 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

20 Experts available now in Live!

Get 1:1 Help Now