Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 1913|回复: 2

[已解决]VBA数值范围之间对比后赋值

[复制链接]
发表于 2016-7-3 20:17 | 显示全部楼层 |阅读模式
例如范围A50-55,代码1,范围B51-54,可见范围B在范围A之间,将代码1赋值给范围B。具体见附件表,谢谢大神帮忙啊。
最佳答案
2016-7-3 20:43
自己验证下!
  1. Sub xx()
  2.     Dim arr, i&, n&, n1&, j&
  3.     With Sheet1
  4.         n = .Cells(.Rows.Count, 1).End(xlUp).Row
  5.         arr = .Range("A2:D" & n)
  6.     End With
  7.     Application.ScreenUpdating = False
  8.     With Sheet2
  9.         n1 = .Cells(.Rows.Count, 1).End(xlUp).Row
  10.         .Range("B2:B" & n).ClearContents
  11.         For i = 2 To n1
  12.             For j = 1 To n - 1
  13.                 If Replace(.Cells(i, 1), "英", "") = arr(j, 1) And .Cells(i, 3) >= arr(j, 3) And .Cells(i, 4) <= arr(j, 4) Then
  14.                     .Cells(i, 2) = arr(j, 2)
  15.                     Exit For
  16.                 End If
  17.             Next
  18.         Next
  19.     End With
  20.     Application.ScreenUpdating = True
  21. End Sub
复制代码

赋值求助.zip

22.5 KB, 下载次数: 8

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2016-7-3 20:43 | 显示全部楼层    本楼为最佳答案   
自己验证下!
  1. Sub xx()
  2.     Dim arr, i&, n&, n1&, j&
  3.     With Sheet1
  4.         n = .Cells(.Rows.Count, 1).End(xlUp).Row
  5.         arr = .Range("A2:D" & n)
  6.     End With
  7.     Application.ScreenUpdating = False
  8.     With Sheet2
  9.         n1 = .Cells(.Rows.Count, 1).End(xlUp).Row
  10.         .Range("B2:B" & n).ClearContents
  11.         For i = 2 To n1
  12.             For j = 1 To n - 1
  13.                 If Replace(.Cells(i, 1), "英", "") = arr(j, 1) And .Cells(i, 3) >= arr(j, 3) And .Cells(i, 4) <= arr(j, 4) Then
  14.                     .Cells(i, 2) = arr(j, 2)
  15.                     Exit For
  16.                 End If
  17.             Next
  18.         Next
  19.     End With
  20.     Application.ScreenUpdating = True
  21. End Sub
复制代码

赋值求助.rar

32.75 KB, 下载次数: 8

回复

使用道具 举报

 楼主| 发表于 2016-7-6 08:37 | 显示全部楼层
老司机带带我 发表于 2016-7-3 20:43
自己验证下!

多谢,测试后没有问题
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-24 06:02 , Processed in 0.405016 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表