Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 508
  • Last Modified:

Macro Excel VBA to verify if cell not empty

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
mldaigle1
Asked:
mldaigle1
  • 6
  • 4
  • 2
1 Solution
 
Rgonzo1971Commented:
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
 
mldaigle1Author Commented:
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
 
Rgonzo1971Commented:
if check while cell Axx <> ""

then try the code above
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
NorieCommented:
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
 
mldaigle1Author Commented:
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
 
NorieCommented:
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
 
mldaigle1Author Commented:
ah ok, just need to repeat the code, incremente the offset by one and adjust the vlookup isn't it?
0
 
NorieCommented:
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
 
mldaigle1Author Commented:
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
 
NorieCommented:
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
 
mldaigle1Author Commented:
let me try this and i'll get back to you by tomorrow

:)

thanks for your help
0
 
mldaigle1Author Commented:
this worked too

thank you so much for your help!

Happy New Year!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now