excel VBA写一段代码:看datamap sheet中TYPE列是否等于single,如果是的话在DATA sheet里找到对应QUESTION ID列值对应的列并按照datamap sheet中Total列的百分比生成answer code列中的值
时间: 2024-06-06 07:08:06 浏览: 133
datamap-it:Datamap是一个多平台磁盘空间和数据增长监视工具-开源
以下是Excel VBA代码示例:
Sub GenerateAnswerCode()
'定义变量
Dim datamapSheet As Worksheet
Dim dataSheet As Worksheet
Dim lastRow As Integer
Dim i As Integer
Dim j As Integer
Dim questionID As Integer
Dim answerCode As String
Dim total As Double
Dim percentage As Double
'设置工作表
Set datamapSheet = ThisWorkbook.Sheets("datamap")
Set dataSheet = ThisWorkbook.Sheets("DATA")
'获取最后一行
lastRow = datamapSheet.Cells(datamapSheet.Rows.Count, "A").End(xlUp).Row
'循环datamap sheet
For i = 2 To lastRow
'判断TYPE列是否等于single
If datamapSheet.Cells(i, "B").Value = "single" Then
'获取QUESTION ID列值
questionID = datamapSheet.Cells(i, "A").Value
'获取Total列值
total = datamapSheet.Cells(i, "C").Value
'循环DATA sheet查找对应列
For j = 2 To dataSheet.Cells(1, dataSheet.Columns.Count).End(xlToLeft).Column
'判断是否为对应列
If dataSheet.Cells(1, j).Value = "QUESTION " & questionID Then
'计算百分比
percentage = total / 100
'循环数据行并生成answer code列值
For k = 2 To dataSheet.Cells(dataSheet.Rows.Count, j).End(xlUp).Row
'计算answer code
answerCode = Format(dataSheet.Cells(k, j).Value * percentage, "#0.00")
'将answer code写入对应单元格
dataSheet.Cells(k, j + 1).Value = answerCode
Next k
End If
Next j
End If
Next i
End Sub
注意:以上代码仅供参考,具体实现需要根据实际情况进行调整。
阅读全文