【问题标题】:Pass data depending on Column and Row根据列和行传递数据
【发布时间】:2018-02-23 03:51:41
【问题描述】:

我正在尝试将 1 或 0 从一张“5s Audit”转移到另一张“Feb Aisle Score”取决于使用提交按钮执行的通过或失败,问题是我需要的位置通过或失败转移到将取决于日期或区域的特定单元格,如下拉列表中显示的图像(日期/区域)。

Sheet1 Date / Area

需要向下传递数据的工作表已修复,以将日期(1-31 月的每一天的数字)作为列,将区域作为行。如下图。

Sheet2 Data

我试图做的是看看我是否可以使用 Dim 语句来启动它,但我一生都无法弄清楚以哪种方式将区域称为行,将日期称为列。

我已经设置了从“Dim Loc1 As String”开始的所有区域单元格和从“Dim Day1 As Double”开始的天数,并引用了每个单元格位置,包括 Sheet 1 中的前 2 个条件。

Sub Submit_Button()

Dim Loc1 As String
Dim Loc2 As String
Dim Loc3 As String
Dim Loc4 As String
Dim Loc5 As String
Dim Loc6 As String
Dim Loc7 As String
Dim Loc8 As String
Dim Loc9 As String
Dim Loc10 As String
Dim Loc11 As String
Dim Loc12 As String
Dim Loc13 As String
Dim Loc14 As String
Dim Loc15 As String
Dim Loc16 As String
Dim Loc17 As String
Dim Loc18 As String
Dim Loc19 As String
Dim Loc20 As String
Dim Loc21 As String
Dim Loc22 As String
Dim Loc23 As String
Dim Loc24 As String
Dim Loc25 As String
Dim Loc26 As String
Dim Loc27 As String
Dim Loc28 As String
Dim Loc29 As String
Dim Loc30 As String
Dim Loc31 As String
Dim Loc32 As String
Dim Loc33 As String
Dim Loc34 As String
Dim Loc35 As String
Dim Loc36 As String



Dim Day1 As Double
Dim Day2 As Double
Dim Day3 As Double
Dim Day4 As Double
Dim Day5 As Double
Dim Day6 As Double
Dim Day7 As Double
Dim Day8 As Double
Dim Day9 As Double
Dim Day10 As Double
Dim Day11 As Double
Dim Day12 As Double
Dim Day13 As Double
Dim Day14 As Double
Dim Day15 As Double
Dim Day16 As Double
Dim Day17 As Double
Dim Day18 As Double
Dim Day19 As Double
Dim Day20 As Double
Dim Day21 As Double
Dim Day22 As Double
Dim Day23 As Double
Dim Day24 As Double
Dim Day25 As Double
Dim Day26 As Double
Dim Day27 As Double
Dim Day28 As Double
Dim Day29 As Double
Dim Day30 As Double
Dim Day31 As Double





Dim day As Double
Dim auditor As String
Dim area As String
Dim passorfail As String




day = Sheets("5S Audit").Range("c10").Value
auditor = Sheets("5S Audit").Range("f10").Value
area = Sheets("5S Audit").Range("j10").Value
passorfail = Sheets("5S Audit").Range("p5").Value

'Aisle LOC
Loc1 = Sheets("Feb Aisle Score").Range("B4").Value
Loc2 = Sheets("Feb Aisle Score").Range("B5").Value
Loc3 = Sheets("Feb Aisle Score").Range("B6").Value
Loc4 = Sheets("Feb Aisle Score").Range("B7").Value
Loc5 = Sheets("Feb Aisle Score").Range("B8").Value
Loc6 = Sheets("Feb Aisle Score").Range("B9").Value
Loc7 = Sheets("Feb Aisle Score").Range("B10").Value
Loc8 = Sheets("Feb Aisle Score").Range("B11").Value
Loc9 = Sheets("Feb Aisle Score").Range("B12").Value
Loc10 = Sheets("Feb Aisle Score").Range("B13").Value
Loc11 = Sheets("Feb Aisle Score").Range("B14").Value
Loc12 = Sheets("Feb Aisle Score").Range("B15").Value
Loc13 = Sheets("Feb Aisle Score").Range("B16").Value
Loc14 = Sheets("Feb Aisle Score").Range("B17").Value
Loc15 = Sheets("Feb Aisle Score").Range("B18").Value
Loc16 = Sheets("Feb Aisle Score").Range("B19").Value
Loc17 = Sheets("Feb Aisle Score").Range("B20").Value
Loc18 = Sheets("Feb Aisle Score").Range("B21").Value
Loc19 = Sheets("Feb Aisle Score").Range("B22").Value
Loc20 = Sheets("Feb Aisle Score").Range("B23").Value
Loc21 = Sheets("Feb Aisle Score").Range("B24").Value
Loc22 = Sheets("Feb Aisle Score").Range("B25").Value
Loc23 = Sheets("Feb Aisle Score").Range("B26").Value
Loc24 = Sheets("Feb Aisle Score").Range("B27").Value
Loc25 = Sheets("Feb Aisle Score").Range("B28").Value
Loc26 = Sheets("Feb Aisle Score").Range("B29").Value
Loc27 = Sheets("Feb Aisle Score").Range("B30").Value
Loc28 = Sheets("Feb Aisle Score").Range("B31").Value
Loc29 = Sheets("Feb Aisle Score").Range("B32").Value
Loc30 = Sheets("Feb Aisle Score").Range("B33").Value
Loc31 = Sheets("Feb Aisle Score").Range("B34").Value
Loc32 = Sheets("Feb Aisle Score").Range("B35").Value
Loc33 = Sheets("Feb Aisle Score").Range("B36").Value
Loc34 = Sheets("Feb Aisle Score").Range("B37").Value
Loc35 = Sheets("Feb Aisle Score").Range("B38").Value
Loc36 = Sheets("Feb Aisle Score").Range("B39").Value



'Days
Day1 = Sheets("Feb Aisle Score").Range("C3").Value
Day2 = Sheets("Feb Aisle Score").Range("D3").Value
Day3 = Sheets("Feb Aisle Score").Range("E3").Value
Day4 = Sheets("Feb Aisle Score").Range("F3").Value
Day5 = Sheets("Feb Aisle Score").Range("G3").Value
Day6 = Sheets("Feb Aisle Score").Range("H3").Value
Day7 = Sheets("Feb Aisle Score").Range("I3").Value
Day8 = Sheets("Feb Aisle Score").Range("J3").Value
Day9 = Sheets("Feb Aisle Score").Range("K3").Value
Day10 = Sheets("Feb Aisle Score").Range("L3").Value
Day11 = Sheets("Feb Aisle Score").Range("M3").Value
Day12 = Sheets("Feb Aisle Score").Range("N3").Value
Day13 = Sheets("Feb Aisle Score").Range("O3").Value
Day14 = Sheets("Feb Aisle Score").Range("P3").Value
Day15 = Sheets("Feb Aisle Score").Range("Q3").Value
Day16 = Sheets("Feb Aisle Score").Range("R3").Value
Day17 = Sheets("Feb Aisle Score").Range("S3").Value
Day18 = Sheets("Feb Aisle Score").Range("T3").Value
Day19 = Sheets("Feb Aisle Score").Range("U3").Value
Day20 = Sheets("Feb Aisle Score").Range("V3").Value
Day21 = Sheets("Feb Aisle Score").Range("W3").Value
Day22 = Sheets("Feb Aisle Score").Range("X3").Value
Day23 = Sheets("Feb Aisle Score").Range("Y3").Value
Day24 = Sheets("Feb Aisle Score").Range("Z3").Value
Day25 = Sheets("Feb Aisle Score").Range("AA3").Value
Day26 = Sheets("Feb Aisle Score").Range("AB3").Value
Day27 = Sheets("Feb Aisle Score").Range("AC3").Value
Day28 = Sheets("Feb Aisle Score").Range("AD3").Value
Day29 = Sheets("Feb Aisle Score").Range("AE3").Value
Day30 = Sheets("Feb Aisle Score").Range("AF3").Value
Day31 = Sheets("Feb Aisle Score").Range("AG3").Value


End Sub

我们将不胜感激。

Dim mydate As String, myaisle As String, myscore As Integer
Dim i As Long
Dim coord1 As Integer, coord2 As Integer

mydate = Worksheets("5s Audit").Range("C10").Value
myaisle = Worksheets("5s Audit").Range("F10").Value
myscore = Worksheets("Score Transfer").Range("C1").Value

For i = 4 To 39
    If Worksheets("Score Transfer").Cells(i, 1).Value = myaisle Then
        coord1 = i
    End If
Next i

For i = 2 To 32
    If Worksheets("Score Transfer").Cells(3, i).Value = mydate Then
        coord2 = i
    End If
Next i

Worksheets("Score Transfer").Cells(coord1, coord2).Value = myscore
编辑:添加了随我的电子表格位置提供的代码 dwirony。

【问题讨论】:

  • 嗨,Erick - 听起来您已经开始尝试自己编写一些东西了。您能否包括该代码 sn-p (在您的问题结束之前?)。有总比没有好。
  • 谢谢 dwirony,我已经添加了我开始使用的代码 sn-p。

标签: vba excel multidimensional-array


【解决方案1】:

因此,这可以通过将日期和通道值分配给变量来非常简单地完成,然后只需在数据表中查找它们即可;我不得不即兴创作范围,因为您没有将它们包含在图片中 - 您必须根据需要修改此代码。

Sub MoveOneorZero()

Dim mydate As String, myaisle As String, myscore As Integer
Dim i As Long
Dim coord1 As Integer, coord2 As Integer

mydate = Worksheets("Sheet2").Range("C3").Value
myaisle = Worksheets("Sheet2").Range("I3").Value
myscore = Worksheets("Sheet2").Range("H1").Value

For i = 4 To 22
    If Worksheets("Sheet1").Cells(i, 1).Value = myaisle Then
        coord1 = i
    End If
Next i

For i = 2 To 32
    If Worksheets("Sheet1").Cells(3, i).Value = mydate Then
        coord2 = i
    End If
Next i

Worksheets("Sheet1").Cells(coord1, coord2).Value = myscore

End Sub

Sheet1 示例:

Sheet2 示例:

[]

运行宏后:

2[]3

【讨论】:

  • 谢谢 dwirony,很抱歉没有提供信息,我可以使用相同的结构做相反的事情吗?就像我可以使用命令按钮执行而不是根据位置将最后一张图像上的总分移动到所需的日期和区域一样吗?示例:总分 = 1 并将其填入日期 9 的区域 Aisle H 1st 中?
  • @ErickU9314 是的,我已经编辑了代码(通过将末尾附近的行更改为Worksheets("Sheet1").Cells(coord1, coord2).Value = myscore
  • 我已经进行了您显示的更改并将它们附加到我的工作表名称但您在上面提供给我的最后一行似乎有错误,它给出了(错误 1004“应用程序定义或对象定义的错误”)。
  • 我已经添加了带有工作表名称的代码片段,并修复了我的电子表格,如图 3 所示以匹配,因此所有字段都与整数和字符串匹配。
  • 非常感谢,正如您所说,我发现我错过了 1 个单元格,这就是错误的原因。我要再次感谢 dwirony 的帮助,我非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-17
  • 2016-10-12
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多