作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一张表,其中包含访问、已完成的应用程序和批准,每行作为邮政编码,我试图将表转换为每行是访问的表。由于我在 Excel 中,我试图在 VBA 中编写一个宏来执行此操作,但这给了我一些不准确的地方。这是我的 table :
Zip Visits Applications Approvals
75229 3 2 1
|
|
v
Zip Visits Applications Approvals
75229 1 0 0
75229 1 1 0
75229 1 1 1
Sub TestMacro1()
Dim n As Integer
Dim i As Integer
Dim StartCell As Range
Dim PrintCell As Range
For n = 0 To 5000
Set StartCell = Range("A2").Offset(n, 0)
Set PrintCell = Range("F10000").End(xlUp)
For i = 1 To StartCell.Offset(0, 1).Value
PrintCell.Offset(i, 0) = StartCell.Value
PrintCell.Offset(i, 1) = 1
If i <= StartCell.Offset(0, 2).Value Then
PrintCell.Offset(i, 2) = 1
Else
PrintCell.Offset(i, 2) = 0
End If
If i <= StartCell.Offset(0, 3).Value Then
PrintCell.Offset(i, 3) = 1
Else
PrintCell.Offset(i, 3) = 0
End If
Next i
Next n
End Sub
最佳答案
要求您必须工作表(您可以将它们命名为您想要的任何名称并相应地修复代码) 编辑:此代码查找每次访问的平均值(以及之后的其余部分),并根据您的示例将它们均匀地分布在单元格中。测试工作!
Sub TestMacro1()
Dim LastRow As Long
Dim CurRow As Long
Dim DestRow As Long
Dim ChkRow As Long
Dim CurWS As Worksheet
Dim DestWS As Worksheet
Dim Visits As Integer
Dim Apps As Integer
Dim Approvals As Integer
Dim AvgApps As Integer
Dim AvgApprovals As Integer
Dim Zip As String
Dim AppsRemain As Integer
Dim ApprovalsRemain As Integer
Set CurWS = ActiveWorkbook.Sheets("Test")
Set DestWS = ActiveWorkbook.Sheets("DestTest")
LastRow = CurWS.Range("A" & CurWS.Rows.Count).End(xlUp).Row
For CurRow = 2 To LastRow
Zip = CurWS.Cells(CurRow, 1).Value 'Assumes Zip is in Column A (1)
Visits = CurWS.Cells(CurRow, 2).Value 'Assumes Visits is in Col B (2)
Apps = CurWS.Cells(CurRow, 3).Value 'Assumes Apps is in Col C (3)
Approvals = CurWS.Cells(CurRow, 4).Value 'Assumes Approvals is in Col D (4)
AvgApps = Apps \ Visits
AvgApprovals = Approvals \ Visits
AppsRemain = Apps Mod Visits
ApprovalsRemain = Approvals Mod Visits
DestRow = DestWS.Range("A" & DestWS.Rows.Count).End(xlUp).Row + 1
For ChkRow = Visits To 1 Step -1
DestWS.Cells(DestRow + ChkRow - 1, 1).Value = Zip 'Assumes Zip is in Column A (1)
DestWS.Cells(DestRow + ChkRow - 1, 2).Value = 1 'Assumes Visits is in Col B (2)
If AppsRemain > 0 Then
DestWS.Cells(DestRow + ChkRow - 1, 3).Value = AvgApps + 1 'Assumes Apps is in Col C (3)
AppsRemain = AppsRemain - 1
Else
DestWS.Cells(DestRow + ChkRow - 1, 3).Value = AvgApps 'Assumes Apps is in Col C (3)
End If
If ApprovalsRemain > 0 Then
DestWS.Cells(DestRow + ChkRow - 1, 4).Value = AvgApprovals + 1 'Assumes Approvals is in Col D (4)
ApprovalsRemain = ApprovalsRemain - 1
Else
DestWS.Cells(DestRow + ChkRow - 1, 4).Value = AvgApprovals 'Assumes Approvals is in Col D (4)
End If
Next ChkRow
Next CurRow
End Sub
关于vba - 循环遍历行并分离出每个 "visit",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27068604/
假设一个数据框 df 有三列 c1, c2, c3。 df=pd.DataFrame() df['c1']=[1,2,3,3,4] df['c2']=["a1","a2","a2","a2","a1"
我已经成功让 Webpack 和 CommonsChunkPlugin 将我的代码分成两部分 - 一个包含我的代码库,另一个包含从 node_modules 导入的所有内容。这是相对容易的一点。 我试
我是一名优秀的程序员,十分优秀!