excel vba将数据放入错误的单元格
我有一个输入表(“按月”),其中用户将数据输入到一些单元格中,然后将数据排序到两个单独的电子表格(“ordersbyLOGdate”和“ordersbySHIPdate”)——您可以猜到这些电子表格包含相同的数据,但排序方式不同(按日志日期和发货日期) 我可以很好地读取和存储数据,但是,当对数据进行排序并将其放入电子表格时,它并没有到达我想要的位置,有人能看到我在这里遗漏了什么吗excel vba将数据放入错误的单元格,vba,excel,Vba,Excel,我有一个输入表(“按月”),其中用户将数据输入到一些单元格中,然后将数据排序到两个单独的电子表格(“ordersbyLOGdate”和“ordersbySHIPdate”)——您可以猜到这些电子表格包含相同的数据,但排序方式不同(按日志日期和发货日期) 我可以很好地读取和存储数据,但是,当对数据进行排序并将其放入电子表格时,它并没有到达我想要的位置,有人能看到我在这里遗漏了什么吗 Sub Button1_Click() Dim countR As Long Dim countLoop As Lo
Sub Button1_Click()
Dim countR As Long
Dim countLoop As Long
countLoop = 1
countR = firstBlankRow(ThisWorkbook.Worksheets("by month"))
countR = countR - 1
Dim colL As String
Dim company As String
Dim orderNumb As String
Dim oDate As Date
Dim total As Double
Dim orderStatus As String
Dim shipMethod As String
Dim sDate As Date
Dim orderStock As String
For i = 2 To countR 'countR is the first row down with nothing in it (leng = 0) and then - 1 (to get the next row up)... that's how many rows have inputs in them that need to be stored
ThisWorkbook.Worksheets("by month").Activate
company = Range("A" & i).Value
orderNumb = Val(Range("B" & i).Value)
oDate = Range("C" & i).Value
total = Val(Range("D" & i).Value)
orderStatus = (Range("E" & i).Value)
shipMethod = Range("I" & Count).Value
sDate = Range("J" & i).Value
orderStock = Range("K" & i).Value
Dim LL As Long
LL = Range("D" & Rows.Count).End(xlUp).Row + 1 + 1
ThisWorkbook.Worksheets("ordersbyLOGdate").Activate
Dim rowN As Integer
rowN = 2
Do Until Range("C" & rowN).Value >= oDate Or rowN = 10000 '10,000 stops infinite row checking
rowN = rowN + 1
Loop 'once loop finishes we should have found a place to insert data, insert a row and place data inside the row
If Range("C" & rowN).Value = oDate Then
Range("A" & rowN).EntireRow.Insert
Range("A" & rowN).Value = company
Range("B" & rowN).Value = orderNumb
Range("C" & rowN).Value = oDate
Range("D" & rowN).Value = total
Range("E" & rowN).Value = orderStatus
Range("I" & rowN).Value = shipMethod
Range("J" & rowN).Value = sDate
Range("K" & rowN).Value = orderStock
End If
If Range("C" & rowN).Value > oDate Then
Debug.Print ("compare date is GREATER than oDate, - 1 from rowN and insert data there")
Range("A" & rowN).EntireRow.Insert
Range("A" & rowN).Value = company
Range("B" & rowN).Value = orderNumb
Range("C" & rowN).Value = oDate
Range("D" & rowN).Value = total
Range("E" & rowN).Value = orderStatus
Range("I" & rowN).Value = shipMethod
Range("J" & rowN).Value = sDate
Range("K" & rowN).Value = orderStock
End If
If rowN = 10000 Then
MsgBox ("ERROR")
Exit Sub
End If
ThisWorkbook.Worksheets("ordersbySHIPdate").Activate
rowN = 2
Do Until Range("C" & rowN).Value >= sDate Or rowN = 10000
rowN = rowN + 1
Loop
If Range("C" & rowN).Value = sDate Then
Range("A" & rowN).EntireRow.Insert
Range("A" & rowN).Value = company
Range("B" & rowN).Value = orderNumb
Range("C" & rowN).Value = oDate
Range("D" & rowN).Value = total
Range("E" & rowN).Value = orderStatus
Range("I" & rowN).Value = shipMethod
Range("J" & rowN).Value = sDate
Range("K" & rowN).Value = orderStock
End If
If Range("C" & rowN).Value > sDate Then
Range("A" & rowN).EntireRow.Insert
Range("A" & rowN).Value = company
Range("B" & rowN).Value = orderNumb
Range("C" & rowN).Value = oDate
Range("D" & rowN).Value = total
Range("E" & rowN).Value = orderStatus
Range("I" & rowN).Value = shipMethod
Range("J" & rowN).Value = sDate
Range("K" & rowN).Value = orderStock
End If
If rowN = 10000 Then
MsgBox ("ERROR")
Exit Sub
End If
Next
ThisWorkbook.Worksheets("ordersbyLOGdate").Activate 'start sorting data into its proper place
rowN = 2 'start at the first row of data, a heading is placed in row 1
Dim check As Boolean
check = True
Dim blankRows As Integer
blankRows = 0
Dim startR As Long
Dim endR As Long
startR = 0
endR = 0
Do Until blankRows = 15
If Range("J" & rowN).Value <> "" Then
blankRows = 0
If check = True Then
startR = rowN
endR = Range("D" & rowN).End(xlDown).Row
endR = endR - 1
Range("D" & rowN).Formula = "=SUM(D" & startR & ":D" & endR & ")"
check = False
End If
rowN = rowN + 1
Else
blankRows = blankRows + 1
If check = False Then
check = True
End If
End If
Loop
check = True
blankRows = 0
startR = 0
endR = 0
rowN = 2
ThisWorkbook.Worksheets("ordersbySHIPdate").Activate
Do Until blankRows = 15
If Range("J" & rowN).Value <> "" Then
blankRows = 0
If check = True Then
startR = rowN
endR = Range("D" & rowN).End(xlDown).Row
endR = endR - 1
Range("D" & rowN).Formula = "=SUM(D" & startR & ":D" & endR & ")"
check = False
End If
rowN = rowN + 1
Else
blankRows = blankRows + 1
If check = False Then
check = True
End If
End If
Loop
ThisWorkbook.Worksheets("by month").Activate
MsgBox ("DONE!")
End Sub
Function Col_Letter(lngCol As Long) As String
Dim vArr
vArr = Split(Cells(1, lngCol).Address(True, False), "$")
Col_Letter = vArr(0)
End Function
Function firstBlankRow(ws As Worksheet) As Long
Dim rw As Range
For Each rw In ws.UsedRange.Rows
If rw.Address = ws.Range(rw.Address).SpecialCells(xlCellTypeBlanks). _
Address Then
firstBlankRow = rw.Row
Exit For
End If
Next
If firstBlankRow = 0 Then
firstBlankRow = ws.Cells.SpecialCells(xlCellTypeLastCell). _
Offset(1, 0).Row
End If
End Function
子按钮1\u单击()
暗数与长数相同
暗淡的倒数像长的一样
countLoop=1
countR=firstBlankRow(此工作簿.工作表(“按月”))
countR=countR-1
作为字符串的Dim colL
把公司当作字符串
像字符串一样麻木
标注日期
总计为双倍
将订单状态设置为字符串
将方法设置为字符串
日期
Dim orderStock作为字符串
对于i=2到countR,countR是第一行,其中没有任何内容(leng=0),然后是-1(到下一行)。。。这就是有多少行中有需要存储的输入
此工作簿。工作表(“按月”)。激活
公司=范围(“A”&i).值
orderNumb=Val(范围(“B”和“i”).Value)
oDate=范围(“C”和i).值
总计=Val(范围(“D”和i).值)
orderStatus=(范围(“E”&i).Value)
shipMethod=范围(“I”和计数)。值
sDate=范围(“J”和i).值
orderStock=范围(“K”&i).值
我会尽我所能
LL=范围(“D”和行数)。结束(xlUp)。行数+1+1
此工作簿。工作表(“ordersbyLOGdate”)。激活
Dim rowN作为整数
罗文=2
直到范围(“C”&rowN)结束。值>=oDate或rowN=10000'10000停止无限行检查
罗文=罗文+1
循环“循环完成后,我们应该找到一个插入数据的位置,插入一行并将数据放在该行中
如果范围(“C”&rowN).Value=oDate,则
范围(“A”&rowN).EntireRow.Insert
范围(“A”&rowN)。值=公司
范围(“B”和rowN)。值=orderNumb
范围(“C”和rowN)。值=oDate
范围(“D”和rowN)。值=总计
范围(“E”和rowN)。值=订单状态
范围(“I”和rowN)。值=shipMethod
范围(“J”和rowN)。值=sDate
范围(“K”和rowN)。值=订单库存
如果结束
如果范围(“C”&rowN).Value>oDate,则
打印(“比较日期大于oDate,-1,从rowN开始,并在其中插入数据”)
范围(“A”&rowN).EntireRow.Insert
范围(“A”&rowN)。值=公司
范围(“B”和rowN)。值=orderNumb
范围(“C”和rowN)。值=oDate
范围(“D”和rowN)。值=总计
范围(“E”和rowN)。值=订单状态
范围(“I”和rowN)。值=shipMethod
范围(“J”和rowN)。值=sDate
范围(“K”和rowN)。值=订单库存
如果结束
如果rowN=10000,则
MsgBox(“错误”)
出口接头
如果结束
此工作簿。工作表(“ordersbySHIPdate”)。激活
罗文=2
直到范围(“C”&rowN)为止。值>=sDate或rowN=10000
罗文=罗文+1
环
如果范围(“C”&rowN).Value=sDate,则
范围(“A”&rowN).EntireRow.Insert
范围(“A”&rowN)。值=公司
范围(“B”和rowN)。值=orderNumb
范围(“C”和rowN)。值=oDate
范围(“D”和rowN)。值=总计
范围(“E”和rowN)。值=订单状态
范围(“I”和rowN)。值=shipMethod
范围(“J”和rowN)。值=sDate
范围(“K”和rowN)。值=订单库存
如果结束
如果范围(“C”和rowN).Value>sDate,则
范围(“A”&rowN).EntireRow.Insert
范围(“A”&rowN)。值=公司
范围(“B”和rowN)。值=orderNumb
范围(“C”和rowN)。值=oDate
范围(“D”和rowN)。值=总计
范围(“E”和rowN)。值=订单状态
范围(“I”和rowN)。值=shipMethod
范围(“J”和rowN)。值=sDate
范围(“K”和rowN)。值=订单库存
如果结束
如果rowN=10000,则
MsgBox(“错误”)
出口接头
如果结束
下一个
此工作簿。工作表(“ordersbyLOGdate”)。激活“开始将数据排序到适当的位置”
rowN=2'从数据的第一行开始,在第1行中放置一个标题
作为布尔值的Dim检查
检查=正确
将空白行设置为整数
blankRows=0
黯淡的星体,如同漫长的星体
像长一样长
startR=0
endR=0
直到blankRows=15为止
如果范围(“J”和rowN).Value为“”,则
blankRows=0
如果check=True,则
startR=rowN
endR=范围(“D”和rowN).End(xlDown).Row
endR=endR-1
范围(“D”&rowN)。公式=“=SUM(D”&startR&“:D”&endR&”)
检查=错误
如果结束
罗文=罗文+1
其他的
blankRows=blankRows+1
如果check=False,则
检查=正确
如果结束
如果结束
环
检查=正确
blankRows=0
startR=0
endR=0
罗文=2
此工作簿。工作表(“ordersbySHIPdate”)。激活
直到blankRows=15为止
如果范围(“J”和rowN).Value为“”,则
blankRows=0
如果check=True,则
startR=rowN
endR=范围(“D”和rowN).End(xlDown).Row
endR=endR-1
范围(“D”&rowN)。公式=“=SUM(D”&startR&“:D”&endR&”)
检查=错误
如果结束
rowN=行+1
其他的
blankRows=blankRows+1
如果check=False,则
检查=正确
如果结束
如果结束
环
此工作簿。工作表(“按月”)。激活
MsgBox(“完成!”)
端接头
函数列字母(与字符串一样长)
暗变
vArr=拆分(单元格(1,lngCol).Address(真,假),“$”)
Col_字母=vArr(0)
端函数
函数firstBlankRow(ws作为工作表)的长度
变暗rw As范围
对于ws.UsedRange.Rows中的每个rw
如果rw.Address=ws.Range(rw.Addre
ActiveWorkbook.Worksheets("ordersbyLOGdate").Activate
ActiveWorkbook.Worksheets("ordersbyLOGdate").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("ordersbyLOGdate").Sort.SortFields.Add Key:=Range("C1:C" & rowN) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("ordersbyLOGdate").Sort
.SetRange Range("A1:K" & rowN)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Sub Button1_Click()
Dim colL As String, company As String, orderNumb As String
Dim orderStatus As String, shipMethod As String, orderStock As String
Dim countR As Long, countLoop As Long, LL As Long
Dim startR As Long, endR As Long
Dim oDate As Date, sDate As Date
Dim total As Double
Dim wb As Workbook, wsMonth As Worksheet
Dim i As Integer, x As Integer, lastRow As Integer, rowN As Integer
Dim check As Boolean
Dim blankRows As Integer
Set wb = ThisWorkbook
Set wsMonth = wb.Worksheets("by month")
Set wsLog = wb.Worksheets("ordersbyLOGdate")
Set wsShip = wb.Worksheets("ordersbySHIPdate")
countR = wsMonth.Cells(wsMonth.Rows.Count, 1).End(xlUp).Row
countLoop = 1
For i = 2 To countR
company = wsMonth.Range("A" & i)
orderNumb = Val(wsMonth.Range("B" & i))
oDate = wsMonth.Range("C" & i)
total = Val(wsMonth.Range("D" & i))
orderStatus = wsMonth.Range("E" & i)
shipMethod = wsMonth.Range("I" & Count)
sDate = wsMonth.Range("J" & i)
orderStock = wsMonth.Range("K" & i)
LL = wsMonth.Range("D" & wsMonth.Rows.Count).End(xlUp).Row + 2
rowN = 2
lastRow = wsLog.Cells(wsLog.Rows.Count, 3).End(xlUp).Row
Do Until wsLog.Range("C" & rowN) >= oDate
If rowN > lastRow Then
MsgBox "ERROR"
Exit Sub
End If
rowN = rowN + 1
Loop
If wsLog.Range("C" & rowN) >= oDate Then
If wsLog.Range("C" & rowN) > oDate Then
Debug.Print "compare date is GREATER than oDate, - 1 from rowN and insert data there"
End If
wsLog.Rows(rowN).Insert
wsLog.Range("A" & rowN) = company
wsLog.Range("B" & rowN) = orderNumb
wsLog.Range("C" & rowN) = oDate
wsLog.Range("D" & rowN) = total
wsLog.Range("E" & rowN) = orderStatus
wsLog.Range("I" & rowN) = shipMethod
wsLog.Range("J" & rowN) = sDate
wsLog.Range("K" & rowN) = orderStock
End If
rowN = 2
lastRow = wsShip.Cells(wsShip.Rows.Count, 3).End(xlUp).Row
Do Until wsShip.Range("C" & rowN) >= sDate
If rowN > lastRow Then
MsgBox "ERROR"
Exit Sub
End If
rowN = rowN + 1
Loop
If wsShip.Range("C" & rowN) >= sDate Then
wsShip.Rows(rowN).Insert
wsShip.Range("A" & rowN) = company
wsShip.Range("B" & rowN) = orderNumb
wsShip.Range("C" & rowN) = oDate
wsShip.Range("D" & rowN) = total
wsShip.Range("E" & rowN) = orderStatus
wsShip.Range("I" & rowN) = shipMethod
wsShip.Range("J" & rowN) = sDate
wsShip.Range("K" & rowN) = orderStock
End If
Next
MysteryFunk (wsLog)
MysteryFunk (wsShip)
wsMonth.Activate
MsgBox ("DONE!")
End Sub
Function MysteryFunk(sheetName As Workheet)
Dim rowN As Long, blankRows As Long, startR As Long, endR As Long
Dim check As Boolean
rowN = 2
check = True
blankRows = 0
startR = 0
endR = 0
Do Until blankRows = 15
If ws.Range("J" & rowN) <> "" Then
blankRows = 0
If check = True Then
startR = rowN
endR = ws.Range("D" & rowN).End(xlDown).Row
endR = endR - 1
ws.Range("D" & rowN).Formula = "=SUM(D" & startR & ":D" & endR & ")"
check = False
End If
rowN = rowN + 1
Else
blankRows = blankRows + 1
If check = False Then
check = True
End If
End If
Loop
End Function
Function Col_Letter(lngCol As Long) As String
Dim vArr
vArr = Split(Cells(1, lngCol).Address(True, False), "$")
Col_Letter = vArr(0)
End Function