世界热推荐:实例35-两表匹配,实例36-根据输入值自动填充数据 Excel表格VBA编程实例 代码分享
实例35-两表匹配
Private Sub CommandButton匹配1_Click()
'判断参数不为空
(相关资料图)
Dim mc1 As Long
Dim mc2 As Long
With ThisWorkbook.Worksheets("操作界面")
If .Cells(2, "C").Value <> "" Then
mc1 = .Cells(2, "C").Value
Else
MsgBox "请输入表1匹配列"
Exit Sub
End If
If .Cells(6, "C").Value <> "" Then
mc2 = .Cells(6, "C").Value
Else
MsgBox "请输入表2匹配列"
Exit Sub
End If
End With
'清除匹配结果
With ThisWorkbook.Worksheets("匹配结果") '清除原列表数据
.UsedRange.ClearFormats
.UsedRange.ClearContents
End With
'获取表1表2最大列号行号
Dim cmax1 As Long
Dim cmax2 As Long
cmax1 = ThisWorkbook.Worksheets("表1").UsedRange.Cells(ThisWorkbook.Worksheets("表1").UsedRange.Count).Column
cmax2 = ThisWorkbook.Worksheets("表2").UsedRange.Cells(ThisWorkbook.Worksheets("表2").UsedRange.Count).Column
Dim rmax1 As Long
Dim rmax2 As Long
rmax1 = ThisWorkbook.Worksheets("表1").UsedRange.Cells(ThisWorkbook.Worksheets("表1").UsedRange.Count).Row
rmax2 = ThisWorkbook.Worksheets("表2").UsedRange.Cells(ThisWorkbook.Worksheets("表2").UsedRange.Count).Row
Dim i, j
Dim addrow As Long
addrow = 1
Dim matchtext1 As String
Dim matchtext2 As String
Dim a1 As Integer '判断循环时是否匹配成功
With ThisWorkbook.Worksheets("匹配结果") '清除原列表数据
For i = 1 To rmax2
a1 = 0
With ThisWorkbook.Worksheets("表2")
If .Cells(i, mc2) <> "" Then
matchtext2 = .Cells(i, mc2)
.Range(.Cells(i, 1), .Cells(i, cmax2)).Copy ThisWorkbook.Worksheets("匹配结果").Cells(addrow, 1)
With ThisWorkbook.Worksheets("表1")
For j = 1 To rmax1
If .Cells(j, mc1) <> "" Then
matchtext1 = .Cells(j, mc1)
If matchtext1 = matchtext2 Then
.Range(.Cells(j, 1), .Cells(j, cmax1)).Copy ThisWorkbook.Worksheets("匹配结果").Cells(addrow, cmax2 + 1)
a1 = 1
addrow = addrow + 1
End If
End If
Next j
End With
If a1 = 0 Then
addrow = addrow + 1
End If
End If
End With
Next i
End With
ThisWorkbook.Worksheets("匹配结果").Activate
End Sub
Private Sub CommandButton匹配2_Click()
'判断参数不为空
Dim mc1 As Long
Dim mc2 As Long
With ThisWorkbook.Worksheets("操作界面")
If .Cells(2, "C").Value <> "" Then
mc1 = .Cells(2, "C").Value
Else
MsgBox "请输入表1匹配列"
Exit Sub
End If
If .Cells(6, "C").Value <> "" Then
mc2 = .Cells(6, "C").Value
Else
MsgBox "请输入表2匹配列"
Exit Sub
End If
End With
'清除匹配结果
With ThisWorkbook.Worksheets("匹配结果") '清除原列表数据
.UsedRange.ClearFormats
.UsedRange.ClearContents
End With
'获取表1表2最大列号
Dim cmax1 As Long
Dim cmax2 As Long
cmax1 = ThisWorkbook.Worksheets("表1").UsedRange.Cells(ThisWorkbook.Worksheets("表1").UsedRange.Count).Column
cmax2 = ThisWorkbook.Worksheets("表2").UsedRange.Cells(ThisWorkbook.Worksheets("表2").UsedRange.Count).Column
Dim rmax1 As Long
Dim rmax2 As Long
rmax1 = ThisWorkbook.Worksheets("表1").UsedRange.Cells(ThisWorkbook.Worksheets("表1").UsedRange.Count).Row
rmax2 = ThisWorkbook.Worksheets("表2").UsedRange.Cells(ThisWorkbook.Worksheets("表2").UsedRange.Count).Row
Dim i, j
Dim addrow As Long
addrow = 1
Dim matchtext1 As String
Dim matchtext2 As String
Dim a1 As Integer '判断循环时是否匹配成功
With ThisWorkbook.Worksheets("匹配结果") '清除原列表数据
For i = 1 To rmax1
a1 = 0
With ThisWorkbook.Worksheets("表1")
If .Cells(i, mc1) <> "" Then
matchtext1 = .Cells(i, mc1)
.Range(.Cells(i, 1), .Cells(i, cmax1)).Copy ThisWorkbook.Worksheets("匹配结果").Cells(addrow, 1)
With ThisWorkbook.Worksheets("表2")
For j = 1 To rmax2
If .Cells(j, mc2) <> "" Then
matchtext2 = .Cells(j, mc2)
If matchtext1 = matchtext2 Then
.Range(.Cells(j, 1), .Cells(j, cmax2)).Copy ThisWorkbook.Worksheets("匹配结果").Cells(addrow, cmax1 + 1)
a1 = 1
addrow = addrow + 1
End If
End If
Next j
End With
If a1 = 0 Then
addrow = addrow + 1
End If
End If
End With
Next i
End With
ThisWorkbook.Worksheets("匹配结果").Activate
End Sub
实例36-根据输入值自动填充数据
Private Sub Worksheet_Change(ByVal Target As Range)
With ThisWorkbook.Worksheets("出库表")
If Target.Column = 3 And Target.Row >= 6 And Target.Row <= 10 Then
Dim row1 As Long
row1 = Target.Row
If Target <> "" Then
Dim i
For i = 1 To ThisWorkbook.Worksheets("商品列表").Cells(1000000, 1).End(xlUp).Row
If Target.Value = ThisWorkbook.Worksheets("商品列表").Cells(i, 1) Then
.Cells(row1, 4) = ThisWorkbook.Worksheets("商品列表").Cells(i, 2)
.Cells(row1, 5) = ThisWorkbook.Worksheets("商品列表").Cells(i, 4)
Exit Sub
End If
Next i
MsgBox "未找到对应商品"
Target = ""
.Cells(row1, 4) = ""
.Cells(row1, 5) = ""
Else
.Cells(row1, 4) = ""
.Cells(row1, 5) = ""
End If
End If
End With
End Sub
关键词:
为您推荐
-
实例35-两表匹配PrivateSubCommandButton匹配1_Click()& 39;判断参数不为空Dimmc1AsLongDimmc2AsLongWithThisWorkbook Worksheets("操作界面")
23-03-11
-
1、总资产周转率是企业一定时期的销售收入净额与平均资产总额之比,它是衡量资产投资规模与销售水平之间配比情况的指标。2、总资产周转率计算公
23-03-11
-
2023年3月1日,国家卫健委、国家医保局等11部门联合发文《关于通报表扬2020-2021年度全国平安医院建设表现突出
23-03-10
-
全球央行连续13年增持黄金后,1月份净增规模又“爆表”,各国央行继续疯狂购金,释放出什么信号?世界黄金协会汇编的最新数据显示,今年1月份
23-03-10
-
自从小艾的妈妈从河内飞到胡志明市之后,两人是打的火热。小艾的妈妈很直接问包哥量地球,如果她同包哥量地球在一起了,两人可不可以生一个娃
23-03-10
-
今天小编肥嘟来为大家解答以上的问题。麦哲伦海峡和德雷克海峡的区别,麦哲伦海峡相信很多小伙伴还不知道,现在让我们一起来看看吧!1、麦哲伦
23-03-09
-
“我在余杭百丈建了一座别墅花了200万除去自己的房间别墅里还有10个空房间就我和丈夫两个人在别墅里很孤单,我想找一些老伙
23-03-09
-
生益电子:东莞证券股份有限公司关于生益电子股份有限公司使用闲置募集资金进行现金管理之核查意见
23-03-09
-
其中樊振东、林高远、梁靖崑等参加了WTT球星挑战赛果阿站的队员3月6日晚从印度抵达新加坡,马龙、王楚钦、孙颖莎、陈梦等队员昨天从北京出发,
23-03-09
-
□金陵晚报 紫金山新闻记者邢虹1月27日晚8:30,著名翻译家、作家杨苡在南京鼓楼医院逝世,享年103岁。近日,“在文字中与杨苡重逢:杨苡作品共
23-03-09
-
3月8日,广发稳健回报混合A最新单位净值为0 8853元,累计净值为0 8853元,较前一交易日下跌0 2%。历史数据显示该基金近1个月下跌1 49%,近3个
23-03-09
-
武穴市武穴街道:串起商圈暖“新”链激活治理新动能---武穴街道坚持党建引领,强化阵地建设,做实暖“新”服务,广泛凝聚新就业群体、服务新业
23-03-08