Function FormatNumber(num As String) As String
' 这个函数用于将数字格式化为两位数,不足两位的在前面补零
FormatNumber = Right("00" & num, 2)
End Function
Sub QueryFrontArea()
' 查询前区的子过程
Dim ws1 As Worksheet, ws2 As Worksheet
Dim num1 As String, num2 As String, num3 As String, num4 As String, num5 As String
Dim i As Integer
' 设置要操作的两个工作表
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("查询")
' 从查询表中获取前区的数字,并不做格式化处理
num1 = ws2.Range("C3").Value
num2 = ws2.Range("D3").Value
num3 = ws2.Range("E3").Value
num4 = ws2.Range("F3").Value
num5 = ws2.Range("G3").Value
' 循环遍历数据表中的每一行
For i = 2 To ws1.Cells(Rows.Count, 1).End(xlUp).Row
' 判断前区的数字是否匹配
If ws1.Cells(i, 3).Value = num1 And ws1.Cells(i, 4).Value = num2 And ws1.Cells(i, 5).Value = num3 And ws1.Cells(i, 6).Value = num4 And ws1.Cells(i, 7).Value = num5 Then
' 如果匹配,则将对应的信息写入查询表中的指定单元格
ws2.Range("E5").Value = ws1.Cells(i, 1).Value
ws2.Range("E6").Value = ws1.Cells(i, 2).Value
Exit Sub ' 已经找到匹配,可以退出循环了
End If
Next i
' 如果循环结束还没有匹配到,就写入未查询到的提示信息
ws2.Range("E5").Value = "未查询到前区"
ws2.Range("E6").Value = "未查询到前区"
End Sub
Sub QueryAll()
' 查询所有的子过程
Dim ws1 As Worksheet, ws2 As Worksheet
Dim num1 As String, num2 As String, num3 As String, num4 As String, num5 As String, num6 As String, num7 As String
Dim i As Integer
' 设置要操作的两个工作表
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("查询")
' 从查询表中获取所有的数字,并不做格式化处理
num1 = ws2.Range("C3").Value
num2 = ws2.Range("D3").Value
num3 = ws2.Range("E3").Value
num4 = ws2.Range("F3").Value
num5 = ws2.Range("G3").Value
num6 = ws2.Range("H3").Value
num7 = ws2.Range("I3").Value
' 循环遍历数据表中的每一行
For i = 2 To ws1.Cells(Rows.Count, 1).End(xlUp).Row
' 判断所有数字是否匹配
If ws1.Cells(i, 3).Value = num1 And ws1.Cells(i, 4).Value = num2 And ws1.Cells(i, 5).Value = num3 And ws1.Cells(i, 6).Value = num4 And ws1.Cells(i, 7).Value = num5 And ws1.Cells(i, 8).Value = num6 And ws1.Cells(i, 9).Value = num7 Then
' 如果匹配,则将对应的信息写入查询表中的指定单元格
ws2.Range("E5").Value = ws1.Cells(i, 1).Value
ws2.Range("E6").Value = ws1.Cells(i, 2).Value
Exit Sub ' 已经找到匹配,可以退出循环了
End If
Next i
' 如果循环结束还没有匹配到,就写入未查询到的提示信息
ws2.Range("E5").Value = "未查询到"
ws2.Range("E6").Value = "未查询到"
End Sub
Sub QueryFrontThree()
' 查询前三位的子过程
Dim ws1 As Worksheet, ws2 As Worksheet
Dim num1 As String, num2 As String, num3 As String
Dim i As Integer
Dim result As String
' 设置要操作的两个工作表
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("查询")
' 从查询表中获取前三位的数字,并不做格式化处理
num1 = ws2.Range("C3").Value
num2 = ws2.Range("D3").Value
num3 = ws2.Range("E3").Value
' 初始化查询结果为空字符串
result = ""
' 循环遍历数据表中的每一行
For i = 2 To ws1.Cells(Rows.Count, 1).End(xlUp).Row
' 判断前三位的数字是否匹配
If ws1.Cells(i, 3).Value = num1 And ws1.Cells(i, 4).Value = num2 And ws1.Cells(i, 5).Value = num3 Then
' 如果匹配,则将对应的信息添加到查询结果中
If result = "" Then
result = ws1.Cells(i, 1).Value & " - " & ws1.Cells(i, 2).Value
Else
result = result & ", " & ws1.Cells(i, 1).Value & " - " & ws1.Cells(i, 2).Value
End If
End If
Next i
' 将查询结果写入查询表中的指定单元格
ws2.Range("C7").Value = result
If result = "" Then
ws2.Range("D7").Value = "未查询到前三位中奖"
Else
ws2.Range("D7").Value = ""
End If
End Sub
Sub QueryFrontFour()
' 查询前四位的子过程
Dim ws1 As Worksheet, ws2 As Worksheet
Dim num1 As String, num2 As String, num3 As String, num4 As String
Dim i As Integer
Dim result As String
' 设置要操作的两个工作表
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("查询")
' 从查询表中获取前四位的数字,并不做格式化处理
num1 = ws2.Range("C3").Value
num2 = ws2.Range("D3").Value
num3 = ws2.Range("E3").Value
num4 = ws2.Range("F3").Value
' 初始化查询结果为空字符串
result = ""
' 循环遍历数据表中的每一行
For i = 2 To ws1.Cells(Rows.Count, 1).End(xlUp).Row
' 判断前四位的数字是否匹配
If ws1.Cells(i, 3).Value = num1 And ws1.Cells(i, 4).Value = num2 And ws1.Cells(i, 5).Value = num3 And ws1.Cells(i, 6).Value = num4 Then
' 如果匹配,则将对应的信息添加到查询结果中
If result = "" Then
result = ws1.Cells(i, 1).Value & " - " & ws1.Cells(i, 2).Value
Else
result = result & ", " & ws1.Cells(i, 1).Value & " - " & ws1.Cells(i, 2).Value
End If
End If
Next i
' 将查询结果写入查询表中的指定单元格
ws2.Range("C8").Value = result
If result = "" Then
ws2.Range("D8").Value = "未查询到前四位中奖"
Else
ws2.Range("D8").Value = ""
End If
End Sub
Function FormatNumber(num As String) As String
' 这是一个自定义的函数,用于格式化输入的数字。
' 它将输入的数字转化为两位数的字符串形式,并返回结果。
FormatNumber = Right("00" & num, 2)
End Function
Sub QueryFrontArea()
' 声明变量
Dim ws1 As Worksheet, ws2 As Worksheet
Dim num1 As String, num2 As String, num3 As String, num4 As String, num5 As String
Dim i As Integer
' 设置变量
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("查询")
' 使用FormatNumber函数对单元格的值进行格式化
num1 = FormatNumber(ws2.Range("C3").Value)
num2 = FormatNumber(ws2.Range("D3").Value)
num3 = FormatNumber(ws2.Range("E3").Value)
num4 = FormatNumber(ws2.Range("F3").Value)
num5 = FormatNumber(ws2.Range("G3").Value)
' 循环遍历Sheet1中的数据
For i = 2 To ws1.Cells(Rows.Count, 1).End(xlUp).Row
' 检查条件是否满足
If ws1.Cells(i, 3).Value = num1 And ws1.Cells(i, 4).Value = num2 And ws1.Cells(i, 5).Value = num3 And ws1.Cells(i, 6).Value = num4 And ws1.Cells(i, 7).Value = num5 Then
' 如果条件满足,则将Sheet1中对应行的第一个单元格的值复制到查询Sheet的C4单元格
ws2.Range("C4").Value = ws1.Cells(i, 1).Value
' 退出子程序
Exit Sub
End If
Next i
' 如果未找到匹配的数据,将 "未查询到前区" 写入查询Sheet的C4单元格
ws2.Range("C4").Value = "未查询到前区"
End Sub
Sub QueryAll()
' 声明变量
Dim ws1 As Worksheet, ws2 As Worksheet
Dim num1 As String, num2 As String, num3 As String, num4 As String, num5 As String, num6 As String, num7 As String
Dim i As Integer
' 设置变量
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("查询")
' 使用FormatNumber函数对单元格的值进行格式化
num1 = FormatNumber(ws2.Range("C3").Value)
num2 = FormatNumber(ws2.Range("D3").Value)
num3 = FormatNumber(ws2.Range("E3").Value)
num4 = FormatNumber(ws2.Range("F3").Value)
num5 = FormatNumber(ws2.Range("G3").Value)
num6 = FormatNumber(ws2.Range("H3").Value)
num7 = FormatNumber(ws2.Range("I3").Value)
' 循环遍历Sheet1中的数据
For i = 2 To ws1.Cells(Rows.Count, 1).End(xlUp).Row
' 检查条件是否满足
If ws1.Cells(i, 3).Value = num1 And ws1.Cells(i, 4).Value = num2 And ws1.Cells(i, 5).Value = num3 And ws1.Cells(i, 6).Value = num4 And ws1.Cells(i, 7).Value = num5 And ws1.Cells(i, 8).Value = num6 And ws1.Cells(i, 9).Value = num7 Then
' 如果条件满足,则将Sheet1中对应行的第一个单元格的值复制到查询Sheet的C4单元格
ws2.Range("C4").Value = ws1.Cells(i, 1).Value
' 退出子程序
Exit Sub
End If
Next i
' 如果未找到匹配的数据,将 "未查询到" 写入查询Sheet的C4单元格
ws2.Range("C4").Value = "未查询到"
End Sub
Private Sub CommandButton1_Click()
' 当CommandButton1被点击时,调用QueryFrontArea子程序
Call QueryFrontArea
End Sub
Private Sub CommandButton2_Click()
' 当CommandButton2被点击时,调用QueryAll子程序
Call QueryAll
End Sub
Function FormatNumber(num As String) As String
' 这是一个自定义的函数,用于格式化输入的数字。
' 它将输入的数字转化为两位数的字符串形式,并返回结果。
FormatNumber = Right("00" & num, 2)
End Function
Sub QueryFrontArea()
' 声明变量
Dim ws1 As Worksheet, ws2 As Worksheet
Dim num1 As String, num2 As String, num3 As String, num4 As String, num5 As String
Dim i As Integer
' 设置变量
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("查询")
' 使用FormatNumber函数对单元格的值进行格式化
num1 = FormatNumber(ws2.Range("C3").Value)
num2 = FormatNumber(ws2.Range("D3").Value)
num3 = FormatNumber(ws2.Range("E3").Value)
num4 = FormatNumber(ws2.Range("F3").Value)
num5 = FormatNumber(ws2.Range("G3").Value)
' 循环遍历Sheet1中的数据
For i = 2 To ws1.Cells(Rows.Count, 1).End(xlUp).Row
' 检查条件是否满足
If ws1.Cells(i, 3).Value = num1 And ws1.Cells(i, 4).Value = num2 And ws1.Cells(i, 5).Value = num3 And ws1.Cells(i, 6).Value = num4 And ws1.Cells(i, 7).Value = num5 Then
' 如果条件满足,则将Sheet1中对应行的第一个单元格的值复制到查询Sheet的C4单元格
ws2.Range("C4").Value = ws1.Cells(i, 1).Value
' 退出子程序
Exit Sub
End If
Next i
' 如果未找到匹配的数据,将 "未查询到前区" 写入查询Sheet的C4单元格
ws2.Range("C4").Value = "未查询到前区"
End Sub
Sub QueryAll()
' 声明变量
Dim ws1 As Worksheet, ws2 As Worksheet
Dim num1 As String, num2 As String, num3 As String, num4 As String, num5 As String, num6 As String, num7 As String
Dim i As Integer
' 设置变量
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("查询")
' 使用FormatNumber函数对单元格的值进行格式化
num1 = FormatNumber(ws2.Range("C3").Value)
num2 = FormatNumber(ws2.Range("D3").Value)
num3 = FormatNumber(ws2.Range("E3").Value)
num4 = FormatNumber(ws2.Range("F3").Value)
num5 = FormatNumber(ws2.Range("G3").Value)
num6 = FormatNumber(ws2.Range("H3").Value)
num7 = FormatNumber(ws2.Range("I3").Value)
' 循环遍历Sheet1中的数据
For i = 2 To ws1.Cells(Rows.Count, 1).End(xlUp).Row
' 检查条件是否满足
If ws1.Cells(i, 3).Value = num1 And ws1.Cells(i, 4).Value = num2 And ws1.Cells(i, 5).Value = num3 And ws1.Cells(i, 6).Value = num4 And ws1.Cells(i, 7).Value = num5 And ws1.Cells(i, 8).Value = num6 And ws1.Cells(i, 9).Value = num7 Then
' 如果条件满足,则将Sheet1中对应行的第一个单元格的值复制到查询Sheet的C4单元格
ws2.Range("C4").Value = ws1.Cells(i, 1).Value
' 退出子程序
Exit Sub
End If
Next i
' 如果未找到匹配的数据,将 "未查询到" 写入查询Sheet的C4单元格
ws2.Range("C4").Value = "未查询到"
End Sub
Private Sub CommandButton1_Click()
' 当CommandButton1被点击时,调用QueryFrontArea子程序
Call QueryFrontArea
End Sub
Private Sub CommandButton2_Click()
' 当CommandButton2被点击时,调用QueryAll子程序
Call QueryAll
End Sub