ªð¦^¦Cªí ¤W¤@¥DÃD µo©«

"Vlookup" and "Match" in VBA

¥»©«³Ì«á¥Ñ am0251 ©ó 2011-6-27 18:14 ½s¿è

ÁÂÁÂHsieh¤j¤jªº¥X¤â¬Û§U......¥i¬OÁÙ¬O¤£¦æ.........,¥i¥H¦A§i¶D§Ú,§Ú¤S¿ù¦b­þ¸Ì©O?¸U¤G¤À·PÁÂ!
  1. Private Sub Start_Click()

  2. Dim x As Integer, y As Integer, z As Integer, S1 As Integer, S2 As Integer, myRowCount As Integer
  3. Dim ShipDate As Double
  4. Dim ItemName As String

  5. z = 2

  6. myRowCount = Range("A1").CurrentRegion.Rows.Count

  7. ShipDate = Format(Calendar1.Value, "d-mmm-yy")

  8. For i = 2 To myRowCount

  9. ItemName = Worksheets("Sheet1").Range("A" & z).Value

  10. x = Application.Match(ItemName, Sheet2.Range("A1:A" & myRowCount), 0)
  11. y = Application.Match(ShipDate, Sheet2.[B1:AF1], 0)

  12. S1 = Workbooks("Book1").Worksheets("Sheet2").Cells(x, y).Value
  13. S2 = Workbooks("Book1").Worksheets("Sheet1").Cells(z, 2).Value

  14. S1 = S1 + S2

  15. z = z + 1

  16. Next i

  17. Unload Me

  18. MsgBox "Done"

  19. End Sub
½Æ»s¥N½X
50 ¦r¸`¥H¤º
¤£¤ä«ù¦Û©w¸q Discuz! ¥N½X

TOP

¥»©«³Ì«á¥Ñ Hsieh ©ó 2011-6-26 22:54 ½s¿è

¦^´_ 21# am0251
¬JµM¤w¸g«Å§i¦¨DOUBLE«¬ºA
ShipDate = Format(Calendar1.Value, "d-mmm-yy")
³o¼Ë¤SÂন¤F¦r¦ê¡A·íµM«¬ºA¤£²Å
ª½±µShipDate =Calendar1.Value
¥u°w¹ï»yªk¸ÑÄÀ¡A¨ä¥L¤£¤F¸Ñ§Aªº¥Øªº¬O¬Æ»ò?µLªkµ¹§A¥ô¦ó·N¨£
¾Ç®üµL²P_¤£®¢¤U°Ý

TOP

¬O³o¼Ëªº,§Úªº³]­p¬O·Q§â"SHEET1"ªº¸ê®Æ«ö¤é´Á,¦WºÙ,¨C­¶ªº¦WºÙ,©ñ¨ì¹ïÀ³ªº¦ì¸m,¥i±¤¥\¤O¤£°÷,¤d®t¸U¿ù,§Æ±æ¦U¦ì°ª¤â«üÂI¤@¤U~~ÁÂÁÂ!! test.rar (30.27 KB)
50 ¦r¸`¥H¤º
¤£¤ä«ù¦Û©w¸q Discuz! ¥N½X

TOP

¦^´_ 23# am0251
  1. Private Sub Start_Click()
  2. Dim mydate#
  3. mydate = Calendar1.Value
  4. Set d = CreateObject("Scripting.Dictionary")
  5. With Sheet1
  6. sh = .[E1] '¤u§@ªí¦WºÙ
  7. For Each a In .Range(.[A2], .Cells(.Rows.Count, 1).End(xlUp))
  8.    d(a.Value) = a.Offset(, 1).Value
  9. Next
  10. With Sheets(sh)
  11.   k = Application.Match(mydate, .Rows(1), 0)
  12.    If IsError(k) Then MsgBox "The day is not find!": Exit Sub
  13.       For Each a In .Range(.[A2], .Cells(.Rows.Count, 1).End(xlUp))
  14.          .Cells(a.Row, k) = d(a.Value)
  15.       Next
  16. End With
  17. End With
  18. Unload Me
  19. MsgBox "Done"
  20. End Sub
½Æ»s¥N½X
¾Ç®üµL²P_¤£®¢¤U°Ý

TOP

§¹¬üªº¸Ñ¨M~~!!¤£¹L¥i¥H¦A±Ð§Ú¦h¤@­Ó¥\¯à¶Ü?´N¬O·Q¨C¦¸§â¸ê®Æ°e¨ì¥Ø¼Ð¦ì¸m®É¤£¥Î"="¦Ó¬O¬Û¥[,¨Ò¦p,"SHEET1" ªº"AAA"ªº¸ê®Æ¬O"1",¦ý¥Ø¼Ð¦ì¸m¥»¨Ó´N¦³"2"ªº¸Ü,¹B§@¤§«á´N¥i¥HÅܦ¨"3",¨º´N¬O§Úªº¥Ø¤F..ÁÂÁÂ!!
50 ¦r¸`¥H¤º
¤£¤ä«ù¦Û©w¸q Discuz! ¥N½X

TOP

.Cells(a.Row, k) = .Cells(a.Row, k) +d(a.Value)
¾Ç®üµL²P_¤£®¢¤U°Ý

TOP

¥i¬d¬Ýexcel»¡©ú¤º¤½¦¡¬d¸ß

TOP

¬Ý¤F¤W­±ªº©«¤l¡A¹ï©ó¨ç¼Æ¨Ï¥Î©óVBA ÁÙ¬O¤@ª¾¥b¸Ñ
¤£ª¾¥i§_±N±`¥Îªº¨ç¼Æ²Õ¦X¹B¥Î©ó VBA §e²{¡A°t¦X¦Û©w¸q¨ç¼Æ¨Ï¥Î
¦ýÀ³¸Ó¬O¹ï©ó¸ê®Æ§ÎºAÁÙ¤£¤F¸Ñ¡A¦A¥[¤W¨ç¼Æªº¼gªk§ó¬O­è¥Î
©Ò¥H¼g¤F¤@­ÓÀ³¸Ó¬O¿ù»~¤£¤Ö¡A§ï¤F¼Æ¦¸³£¶Ã¤FÃÐ
¦pªþ¥ó¡A§Æ±æ¦³¥ý¶i¥i¥H¨ó§U¤@¤U¬Ý¬O§_¥i¦æ
¨ç¼Æ²Õ¦X¬°
=IF(ISNA(VLOOKUP($A2,³]³Æ²M³æ!$A$1:$B$20,MATCH(D$1,³]³Æ²M³æ!A$1:B$1,0),0)),"µL³]³Æ¸ê®Æ",(VLOOKUP($A2,³]³Æ²M³æ!$A$1:$B$20,MATCH(D$1,³]³Æ²M³æ!A$1:B$1,0),0)))
§Æ±æ§e²{
=vlmatch($A2,E$1,µL³]³Æ¸ê®Æ,³]³Æ²M³æ!$A$1:$B$20,³]³Æ²M³æ!$A$1:$B$1)
vlmatch ¬°¦Û©w¸q¨ç¼Æ¡A¥u»Ý¿é¤J $A2,E$1,µL³]³Æ¸ê®Æ,³]³Æ²M³æ!$A$1:$B$20,³]³Æ²M³æ!$A$1:$B$1§Y¥i
§ï¤F¼Æ¦¸¡A³Ì«á¿ù»~¡A¼g¤F¦p¤U
  1. Public Function VLMatch(tt$, KK$, NN$, DALL As Range, dt As Range) As String

  2. Dim i%, j$, k As Boolean
  3. i = Evaluate("Match(kk, dt, 0)")
  4. j = Evaluate("VLookup(tt, DALL, i, 0)")
  5. k = Evaluate("IsNA(j)")
  6. If k = False Then
  7.   VLMatch = NN
  8.   Else
  9.   VLMatch = j
  10.   End If

  11. End Function
½Æ»s¥N½X
¦Û©w¸q¨ç¼Æ VLOOK+Match.rar (108.68 KB)

TOP

¦^´_ 28# Happkkevin
  1. Public Function VLMatch(tt$, KK$, NN$, DALL As Range, dt As Range) As String
  2.     'Match  §ä¨ì­È«h¶Ç¦^¼Æ¦r,¨S§ä¨ì­È«h¶Ç¦^¿ù»~­È
  3.     'VLookup   §ä¨ì¶Ç¦^¦r¦ê,¨S§ä¨ì¶Ç¦^¿ù»~­È
  4.     Dim i As Variant, j As Variant  'Åܼƻݳ]©w¬°Variant¸ê®Æ«¬ºA
  5.     'Variant¸ê®Æ«¬ºA¬O©Ò¦³¨S³Q©ú½T«Å§i¬°¥ô¤@¨ä¥L«¬ºA
  6.     i = Application.Match(KK, dt, 0)  'vba¤¤¨Ï¥Î¤u§@ªí¨ç¼Æ->  Application.¤u§@ªí¨ç¼Æ
  7.     '½Ð¬Ývba¤¤¥i¨Ï¥Îªº¤u§@ªí¨ç¼Æ
  8.     j = Application.VLookup(tt, DALL, i, 0)
  9.     '***   Evaluate("VLookup(tt, DALL, i, 0)")=[VLookup(tt, DALL, i, 0)]
  10.     '¤£±µ¨üÅÜ¼Æ tt, DALL, i   ***
  11.     If IsError(j) Then   'j¶Ç¦^¿ù»~­È
  12.         VLMatch = NN
  13.     Else:    'j¶Ç¦^¦r¦ê
  14.         If j = "" Then VLMatch = NN Else VLMatch = j
  15.     End If
  16. End Function
½Æ»s¥N½X

TOP

¥»©«³Ì«á¥Ñ Happkkevin ©ó 2011-7-17 12:00 ½s¿è
¦^´_  Happkkevin
GBKEE µoªí©ó 2011-7-17 08:45


GBKEE ª©¤j¡A¯uªº¬O¤Ó¼F®`Åo
¨C¨C§Úªá¤F³\¤[®É¶¡­×¥¿¥u­n¸g¹L¦¹³B¥ý¶iªº«ü¾É
¤£¥u°ÝÃD¸Ñ¨M¡A§óÅý§Ú¾Ç¨ì³\¦h
¦Ü©óVBA¥i¥Î¨ç¼Æ¤§«e§Ú¨S¦³½T»{¥u¬O if µo¥Í°ÝÃD¤~³B²z
»¡©ú¤¤ §Úªº³nÅéÃö©ó vlookup »P match ¨S¦³»¡©ú¡A¦ý§Ú·QÀ³¸Ó¬O»P¨ç¼Æ¬Û¦P
·PÁ§Aªº«ü¾É
­×¥¿¬°§Aªº»yªk¥[¤W­×¥¿ NN¿é¤J""§Y²Å¦X§Úªº»Ý¨D
=vlmatch($A2,E$1,"µL³]³Æ¸ê®Æ",³]³Æ²M³æ!$A$1:$B$20,³]³Æ²M³æ!$A$1:$B$1)

TOP

        ÀR«ä¦Û¦b : ª¾ÃÑ­n¥Î¤ßÅé·|¡A¤~¯àÅܦ¨¦Û¤vªº´¼¼z¡C
ªð¦^¦Cªí ¤W¤@¥DÃD