简体   繁体   中英

Using VBA to link spreadsheet that has data

I am trying to use VBA to point at criteria in a spreadsheet. I would just like to point the code at a spreadsheet to pull the criteria rather than constantly having to update the code for new criteria.

Last = Cells(Rows.Count, "H").End(xlUp).Row
For i = Last To 1 Step -1
    If (Cells(i, "D").Value) <> 0 And (Cells(i, "A").Value) Like "*00000*" Then
        Cells(i, "N") = "'000"
    ElseIf (Cells(i, "A").Value) Like "002000*" Then
        Cells(i, "N") = "211"
    End If

I would just like to make it so that I can just update the spreadsheet with new criteria and have the code recognize it automatically.

You can do something like this:

Dim crit1, crit2

crit1 = Sheets("Criteria").Range("A1").value '*00000*
crit2 = Sheets("Criteria").Range("A2").value '002000*

If (Cells(i, "D").Value) <> 0 And (Cells(i, "A").Value) Like crit1 Then
    Cells(i, "N") = "'000"
ElseIf (Cells(i, "A").Value) Like crit2 Then
    Cells(i, "N") = "211"
End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM