Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA用于将两个日期之间的数据从不同的产品表提取到主表_Excel_Vba - Fatal编程技术网

Excel VBA用于将两个日期之间的数据从不同的产品表提取到主表

Excel VBA用于将两个日期之间的数据从不同的产品表提取到主表,excel,vba,Excel,Vba,我试图从不同的表中提取产品销售取决于日期(两个日期之间),在主表中,我有两列:A为日期;B销售数字。我是VBA新手,试图从学习过的Youtube视频中编写代码,到目前为止,我只能检索A列的值,即日期,但我无法获取销售值和日期。我在这里写代码,请你检查一下,让我知道错误发生的地方和原因,你的建议可能会对我有所帮助。提前谢谢。:) Private子命令按钮1\u单击() 将sh标注为工作表,Psh标注为字符串,x标注为日期,y标注为日期 表8.列(“A”).NumberFormat=“m/d/yyy

我试图从不同的表中提取产品销售取决于日期(两个日期之间),在主表中,我有两列:A为日期;B销售数字。我是VBA新手,试图从学习过的Youtube视频中编写代码,到目前为止,我只能检索A列的值,即日期,但我无法获取销售值和日期。我在这里写代码,请你检查一下,让我知道错误发生的地方和原因,你的建议可能会对我有所帮助。提前谢谢。:)

Private子命令按钮1\u单击()
将sh标注为工作表,Psh标注为字符串,x标注为日期,y标注为日期
表8.列(“A”).NumberFormat=“m/d/yyyy”
Psh=Me.Shproducts的Shproducts是命令按钮名
设置sh=工作表(Psh)
x=CDate(Me.TextBox1)“开始日期
y=CDate(Me.TextBox2)的结束日期
调暗i为长,Myrange为范围,Lastrow为长
对于i=3到sh.Range(“F”和Rows.Count).End(xlUp).Row

如果sh.Cells(i,1)>=x和sh.Cells(i,1),我还没有测试它,但是您可能想要这样做。因此,这是基于您的代码:

Private Sub CommandButton1_Click()

  Dim sh As Worksheet, Psh As String, x As Date, y As Date
  Dim i As Long, Myrange As Range, Lastrow As Long
  Dim destinationRange As Range, c As Range, sourceRange As Range
  Dim counter As Long
  Dim currentDate As Date, currentSales As Double

  Sheet8.Columns("A").NumberFormat = "m/d/yyyy"

  Psh = Me.Shproducts ' Shproducts is Command Button Name
  Set sh = Worksheets(Psh)
  x = CDate(Me.TextBox1) ' Start Date
  y = CDate(Me.TextBox2) ' End Date

  Set destinationRange = Sheet8.Range("A100000").End(xlUp).Offset(1, 0) 
  Set sourceRange = Range(sh.Range("F3"), sh.Range("F3").End(xlDown))

  For Each c In sourceRange
    currentDate = c.Value
    currentSales = c.Offset(0, 1).Value
    If currentDate >= x And currentDate <= y Then
      destinationRange.Offset(counter, 0).Value = currentDate
      destinationRange.Offset(counter, 1).Value = currentSales
      counter = counter + 1
    End If
  Next i

End Sub
Private子命令按钮1\u单击()
将sh标注为工作表,Psh标注为字符串,x标注为日期,y标注为日期
调暗i为长,Myrange为范围,Lastrow为长
Dim destinationRange作为范围,c作为范围,sourceRange作为范围
昏暗的柜台一样长
Dim currentDate为日期,currentSales为双精度
表8.列(“A”).NumberFormat=“m/d/yyyy”
Psh=Me.Shproducts的Shproducts是命令按钮名
设置sh=工作表(Psh)
x=CDate(Me.TextBox1)“开始日期
y=CDate(Me.TextBox2)的结束日期
设置destinationRange=Sheet8.范围(“A100000”).结束(xlUp).偏移量(1,0)
设置sourceRange=Range(sh.Range(“F3”)、sh.Range(“F3”).End(xlDown))
对于sourceRange中的每个c
currentDate=c.值
currentSales=c.偏移量(0,1).值
如果currentDate>=x且currentDate
Private Sub CommandButton1_Click()

  Dim sh As Worksheet, Psh As String, x As Date, y As Date
  Dim i As Long, Myrange As Range, Lastrow As Long
  Dim destinationRange As Range, c As Range, sourceRange As Range
  Dim counter As Long
  Dim currentDate As Date, currentSales As Double

  Sheet8.Columns("A").NumberFormat = "m/d/yyyy"

  Psh = Me.Shproducts ' Shproducts is Command Button Name
  Set sh = Worksheets(Psh)
  x = CDate(Me.TextBox1) ' Start Date
  y = CDate(Me.TextBox2) ' End Date

  Set destinationRange = Sheet8.Range("A100000").End(xlUp).Offset(1, 0) 
  Set sourceRange = Range(sh.Range("F3"), sh.Range("F3").End(xlDown))

  For Each c In sourceRange
    currentDate = c.Value
    currentSales = c.Offset(0, 1).Value
    If currentDate >= x And currentDate <= y Then
      destinationRange.Offset(counter, 0).Value = currentDate
      destinationRange.Offset(counter, 1).Value = currentSales
      counter = counter + 1
    End If
  Next i

End Sub