Excel查找比x更小的值>;确定日期/时间并粘贴
我有数据(下图),任务是找到值 >子复制粘贴() > >将LastRow变长,LastCol变长,行变长,列变长,x >只要将wb设置为工作簿,将ws设置为工作表,将ws2设置为工作表 >搜索范围作为范围,单元格作为范围 > >Set wb=thispoolk Set ws=wb.Sheets(“DataHorizontal”)“将工作表 >其中您的数据被设置为ws2=wb.Sheets(“概述”)'工作表,其中 >你想要你的结果 > >LastRow=ws.Cells(Rows.Count,1)。End(xlUp)。Row LastCol=ws.Cells(1, >Columns.Count).End(xlToLeft.Column > >设置SearchRange=ws.Range(ws.Cells(2,2),ws.Cells(LastRow,LastCol)) > >x=27 > >对于SearchRange中的每个单元格 >行=单元格。行 >Column=单元格。Column >如果单元格值<-3或单元格值>3,则 >'输出被放置在两列中,A代表日期,B代表时间 >ws2.Cells(x,5).Value=ws.Cells(行,1).Value'复制粘贴日期 >ws2.Cells(x,6).Value=ws.Cells(1,列).Value'复制粘贴时间 >x=x+1 >如果是下一个单元格,则结束 > >端接头Excel查找比x更小的值>;确定日期/时间并粘贴,excel,powerquery,Excel,Powerquery,我有数据(下图),任务是找到值 >子复制粘贴() > >将LastRow变长,LastCol变长,行变长,列变长,x >只要将wb设置为工作簿,将ws设置为工作表,将ws2设置为工作表 >搜索范围作为范围,单元格作为范围 > >Set wb=thispoolk Set ws=wb.Sheets(“DataHorizontal”)“将工作表 >其中您的数据被设置为ws2=wb.Sheets(“概述”)'工作表,其中 >你想要你的结果 > >LastRow=ws.Cells(Rows.Coun
打开excel,按ALT+F11,在左侧窗口中,右键单击您使用的工作表(工作表名称可在此处找到),选择Add->Modul,然后将以下代码复制到中 调整变量时,只需按播放按钮或按F5 以下是您的解决方案:
Sub Stackoverflow()
Dim wbk1 As Workbook
Dim wbk2 As Workbook
Rem Creating a new workbook to collect data
Set wbk1 = ThisWorkbook
Set wbk2 = Workbooks.Add(xlWBATWorksheet)
Rem dc = datecolumn, the number of column AA
dc = Range("AA" & 1).Column
Rem k is the first row of new workbook, and i is the first row where the data can be found.
k = 1
i = 2
Do
j = Range("AB" & 1).Column
Do
If wbk1.Worksheets("Work1").Cells(i, j).Value < 3 Then
wbk2.Worksheets("Work1").Cells(k, 1) = wbk1.Worksheets("Work1").Cells(i, dc)
wbk2.Worksheets("Work1").Cells(k, 2) = wbk1.Worksheets("Work1").Cells(1, j)
wbk2.Worksheets("Work1").Cells(k, 3) = wbk1.Worksheets("Work1").Cells(i, j)
k = k + 1
End If
j = j + 1
Loop Until wbk1.Worksheets("Work1").Cells(i, j).Value = ""
i = i + 1
Loop Until wbk1.Worksheets("Work1").Cells(i, j).Value = ""
Rem the result will be:
Rem first column in the new workbook will be the date
Rem second is the time
Rem third is the data
End Sub
子堆栈溢出()
将wbk1设置为工作簿
将wbk2设置为工作簿
Rem创建新工作簿以收集数据
设置wbk1=ThisWorkbook
设置wbk2=工作簿。添加(XLWBATWORKEM)
Rem dc=datecolumn,列AA的编号
dc=范围(“AA”&1)。列
Rem k是新工作簿的第一行,i是可以找到数据的第一行。
k=1
i=2
做
j=范围(“AB”和1)。列
做
如果wbk1.工作表(“工作1”).单元格(i,j).值<3,则
wbk2.工作表(“工作1”).单元格(k,1)=wbk1.工作表(“工作1”).单元格(i,dc)
wbk2.工作表(“工作1”).单元格(k,2)=wbk1.工作表(“工作1”).单元格(1,j)
wbk2.工作表(“工作1”).单元格(k,3)=wbk1.工作表(“工作1”).单元格(i,j)
k=k+1
如果结束
j=j+1
循环到wbk1.Worksheets(“Work1”).Cells(i,j).Value=“”
i=i+1
循环到wbk1.Worksheets(“Work1”).Cells(i,j).Value=“”
结果将是:
新工作簿中的第一列将是日期
第二是时间
第三是数据
端接头
由于您没有在屏幕上提供行号,因此必须将“i”值设置为可以找到数据的第一行。因此,如果行号为2,数字数据所在的位置,则保持不变
还请注意,您必须将Work1重命名为工作表的名称
希望这会有帮助 您需要一个宏来执行此操作 代码示例
Option Explicit
Sub CopyPaste()
Dim LastRow As Long, LastCol As Long, Row As Long, Column As Long, x As Long
Dim wb As Workbook
Dim ws As Worksheet, ws2 As Worksheet
Dim SearchRange As Range, Cell As Range
Set wb = ThisWorkbook
Set ws = wb.Sheets("input") 'the sheet in which your data is
Set ws2 = wb.Sheets("Output") 'the sheet where you want your result
LastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = ws.Cells(1, Columns.Count).End(xlToLeft).Column
Set SearchRange = ws.Range(ws.Cells(2, 2), ws.Cells(LastRow, LastCol))
x = 2
For Each Cell In SearchRange
Row = Cell.Row
Column = Cell.Column
If Cell.Value < 3 Then
'Output is placed in 2 columns, A for time, B for date
ws2.Cells(x, 1).Value = ws.Cells(1, Column).Value 'Copy-Paste Time
ws2.Cells(x, 2).Value = ws.Cells(Row, 1).Value 'Copy-paste Date
x = x + 1
End If
Next Cell
End Sub
选项显式
子复制粘贴()
将LastRow变长、LastCol变长、行变长、列变长、x变长
将wb设置为工作簿
将ws设置为工作表,将ws2设置为工作表
将搜索范围变暗为范围,单元格变暗为范围
设置wb=ThisWorkbook
将ws=wb.Sheets(“输入”)设置为包含数据的工作表
将ws2=wb.Sheets(“输出”)设置为需要结果的工作表
LastRow=ws.Cells(Rows.Count,1).End(xlUp).Row
LastCol=ws.Cells(1,Columns.Count).End(xlToLeft).Column
设置SearchRange=ws.Range(ws.Cells(2,2),ws.Cells(LastRow,LastCol))
x=2
对于SearchRange中的每个单元格
行=单元格。行
Column=单元格。Column
如果单元格值小于3,则
'输出被放置在两列中,A表示时间,B表示日期
ws2.Cells(x,1).Value=ws.Cells(1,列).Value'复制粘贴时间
ws2.Cells(x,2).Value=ws.Cells(行,1).Value'复制粘贴日期
x=x+1
如果结束
下一个细胞
端接头
结果如下,您可以调整输出表:
其思想是在每个列中循环一个日期,并打印值。然后转到下一个日期(行)并重复该过程。输出将按照从列BA到列BC的输出进行打印
Sub CompareCopy()
Dim ws1 As Worksheet
Set ws1 = ActiveWorkbook.Worksheets("Sheet1")
Dim ws2 As Worksheet
Set ws2 = ActiveWorkbook.Worksheets("Sheet1") 'You can change the output datas sheet.
Dim lrow As Long
Dim i As Long
Dim j As Long
Dim lCol As Long
Dim k As Long
Dim Header1 As String
Dim Header2 As String
Dim Header3 As String
Dim ColumnOutput As String
k = 2 'first row where output data will be placed
lCol = ws1.Cells(1, Columns.Count).End(xlToLeft).Column 'find last column
lrow = ws1.Cells(Rows.Count, 27).End(xlUp).Row 'Find last row
ColumnOutput = lCol 'Set Column where output should start, default is the last column of your original table.
ws2.Cells(k - 1, ColumnOutput + 2) = "Date" 'Place where "Date" will be printed
ws2.Cells(k - 1, ColumnOutput + 3) = "Time" 'Place where "Time" will be printed
ws2.Cells(k - 1, ColumnOutput + 4) = "Value" 'Place where "Value" will be printed
For i = 2 To lrow 'Loop through each row
For j = 28 To lCol 'Loop through each Column
If -3 > ws1.Cells(i, j).Value Then 'If current table value is less than -3 then copy
ws2.Cells(k, ColumnOutput + 2).Value = ws1.Cells(i, 27).Value 'Copy Date
ws2.Cells(k, ColumnOutput + 2).NumberFormat = "dd-mmm-yy" 'Format time value to correct time
ws2.Cells(k, ColumnOutput + 3).Value = ws1.Cells(1, j).Value 'Copy Time
ws2.Cells(k, ColumnOutput + 3).NumberFormat = "hh:mm" 'Format time value to correct time
ws2.Cells(k, ColumnOutput + 4).Value = ws1.Cells(i, j).Value 'Copy Value
k = k + 1
End If
Next j
Next i
End Sub
一种更快的方法是使用(Get&Transform Data
)
将源数据格式化为表
,然后使用如下查询:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Date"}, "Time", "Value"),
#"Filtered Rows" = Table.SelectRows(#"Unpivoted Other Columns", each [Value] < 3),
#"Changed Type" = Table.TransformColumnTypes(#"Filtered Rows",{{"Date", type date}, {"Time", type time}, {"Value", type number}})
in
#"Changed Type"
let
Source=Excel.CurrentWorkbook(){[Name=“Table1”]}[Content],
#“Unpivoted Other Columns”=表.UnpivotOtherColumns(源,{“日期”},“时间”,“值”),
#“筛选行”=表格。选择行(#“取消筛选其他列”,每个[值]<3),
#“已更改类型”=表.TransformColumnTypes(#“筛选行”、{{“日期”、类型日期}、{“时间”、类型时间}、{“值”、类型编号})
在里面
#“已更改类型”
谢谢。它可以正常工作,但粘贴所有日期,可能是格式有问题。试着找到解决办法。好的,那么你能把这个答案标为正确答案吗?谢谢,我正在尝试修改一行,如果Cell.Value<-3或Cell.Value>3,那么我会得到所有的日期,也许你知道哪里会有问题?3.请把你写的代码贴出来。如果cell.value<-3或cell.value>3,则。。。应该是正确的。我刚刚更改了此部分,但它输出了所有日期。总和输出为Z列的“总和”。也许它会搜索所有表格<代码>对于SearchRange中的每个单元格,行=单元格。行列=单元格。列如果Cell.Value<-3或Cell.Value>3,则“输出”放在两列中,A表示日期,B表示时间ws2。单元格(x,1)。值=ws.Cells(行,1)。值“复制粘贴日期ws2.Cells(x,2)。值=ws.Cells(1,列).Value'复制粘贴时间x=x+1如果下一个单元格结束
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Date"}, "Time", "Value"),
#"Filtered Rows" = Table.SelectRows(#"Unpivoted Other Columns", each [Value] < 3),
#"Changed Type" = Table.TransformColumnTypes(#"Filtered Rows",{{"Date", type date}, {"Time", type time}, {"Value", type number}})
in
#"Changed Type"