提问人:2mas 提问时间:10/30/2023 最后编辑:2mas 更新时间:10/31/2023 访问量:50
比较日期,并将数据放在日期下
Compare date, and place data under date
问:
我在一本工作簿中有三张纸。两个工作表包含用户输入的数据(SheetMain 和 SheetC1,名称是固定的)。此数据必须传输到第三个“历史记录”或“日志”工作表 (SheetDest)。日志必须以这种方式完成,没有外部文本文件等。
Sub Historie_speichern()
Dim SheetMain As Worksheet, SheetDest As Worksheet, SheetC1 As Worksheet
'Source and destination, as well as C1, another data entry sheet
Set SheetMain = ThisWorkbook.Sheets("B1 PV Application") 'Main data entry sheet
Set SheetDest = ThisWorkbook.Sheets("C0 EV (Historie)") 'Sheet for saving the history
Set SheetC1 = ThisWorkbook.Sheets("C1 EV Application") 'another data entry sheet, usually done once
SheetC1.Range("C4:F22").Copy
SheetDest.Range("B2:E20").PasteSpecial xlValues 'Table C0 Part 1
SheetC1.Range("N4:O22").Copy
SheetDest.Range("F2:G20").PasteSpecial xlValues 'Table C0 Part 2.1
SheetC1.Range("Q4:R22").Copy
SheetDest.Range("H2:I20").PasteSpecial xlValues 'Table C0 Part 2.2
SheetC1.Range("T4:U22").Copy
SheetDest.Range("J2:K20").PasteSpecial xlValues 'Table C0 Part 2.3
SheetC1.Range("W4:X22").Copy
SheetDest.Range("L2:M20").PasteSpecial xlValues 'Table C0 Part 2.4
SheetC1.Range("Z4:AA22").Copy
SheetDest.Range("N2:O20").PasteSpecial xlValues 'Table C0 Part 2.5
这部分有效。也许某个人有一个循环的想法,它只是无法让它工作。
'loop through columns
Dim i As Long
Dim LastCol As Long
Dim DataDate As Date
DataDate = SheetC1.Range("C2").value
' SheetDest.Cells(2, Columns.Count).End(xlToLeft).Column
For i = 21 To 100 'from column "S" to 100 (fixed range, 100/12months=8.3 years)
If SheetDest.Cells(2, i).value = DataDate Then
Cells(3, i).value = SheetC1.Range("AD27").value
'in Range("AD27") is a KPI value calculated from the data table for one month
'several of these values over time give a special curve that is used to assess performance
Else
Exit For
End If
Next i
End Sub
日志有两个用途:保存每个月的输入数据,并保存根据输入的数据计算出的数字,并将其放在相应月份的时间线中。第一部分,我将在另一个问题中提出。
“Datadate”(格式为“general”)将其值与表格旁边的固定时间线进行比较(从 2024 年 1 月到 2030 年 1 月的月份水平显示)。我的想法是,我遍历列/时间线,当日期匹配时,值写在日期下。时间线的值,例如“Jan.2023”是一个 vlookup。
如何使第二部分起作用?
答:
0赞
taller
10/31/2023
#1
请验证 SheetC1.Range(“C2”) 和 SheetDest.Cells(2, i) 的数据类型。确定如何准确比较它们的值至关重要。
如果您的文件与代码中的假设不一致,请提供具体详细信息。这将有助于进行任何必要的调整或澄清。
Option Explicit
Sub Demo()
'loop through columns
Dim i As Long
Dim LastCol As Long
Dim DataDate As String ' **
Set SheetC1 = ThisWorkbook.Sheets("C1 EV Application") ' Modify as needed
Set SheetDest = ThisWorkbook.Sheets("C0 EV (Historie)")
' If C2 is formatted as "general" and shows `Jan.2024`
' It means C2 is a String instead of Date
DataDate = SheetC1.Range("C2").Value
For i = 21 To 100 'from column "S" to 100 (fixed range, 100/12months=8.3 years)
' Assumes SheetDest.Cells(2, i) is formatted as Date
With SheetDest.Cells(2, i)
If IsDate(.Value) Then
If StrComp(Format(.Value, "MMM.yyyy"), DataDate, vbTextCompare) = 0 Then
Cells(3, i).Value = SheetC1.Range("AD27").Value
Exit For
End If
End If
End With
Next i
End Sub
评论
0赞
2mas
11/6/2023
SheetC1.Range(“C2”) 是通过数据 -> 数据工具 ->数据验证放置的下拉列表,格式为自定义 (MMM.YYYY)。SheetDest.Cells(2, i) 的格式完全相同,但通过另一个工作表的 vlookup 引用,因为时间线可以由用户更改(他输入一个他想要定位的日期范围,然后在这个日期范围内他做其他所有事情 - 项目管理)
0赞
VBasic2008
10/31/2023
#2
VBA 查找:“Application.Match”日期使用CLng
Sub Historie_speichern()
Const SRC_NAME As String = "C1 EV Application"
Const SRC_DATE_CELL As String = "C2"
Const SRC_KPI_CELL As String = "AD27"
Const DST_NAME As String = "C0 EV (Historie)"
Const DST_DATES_LEFT_CELL As String = "S2"
Const DST_KPI_ROW As Long = 3
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim sws As Worksheet: Set sws = wb.Sheets(SRC_NAME)
Dim sDate As Date: sDate = sws.Range(SRC_DATE_CELL).Value
Dim sKPI As Variant: sKPI = sws.Range(SRC_KPI_CELL).Value
Dim dws As Worksheet: Set dws = wb.Sheets(DST_NAME)
With dws
.Range("B2:E20").Value = sws.Range("C4:F22").Value
.Range("F2:G20").Value = sws.Range("N4:O22").Value
.Range("H2:I20").Value = sws.Range("Q4:R22").Value
.Range("J2:K20").Value = sws.Range("T4:U22").Value
.Range("L2:M20").Value = sws.Range("W4:X22").Value
.Range("N2:O20").Value = sws.Range("Z4:AA22").Value
End With
Dim drrg As Range
With dws.Range(DST_DATES_LEFT_CELL)
Dim LastCol As Long:
LastCol = dws.Cells(.Row, dws.Columns.Count).End(xlToLeft).Column
Set drrg = .Resize(, LastCol - .Column + 1)
End With
Dim cIndex As Variant: cIndex = Application.Match(CLng(sDate), drrg, 0)
If IsNumeric(cIndex) Then
drrg.Cells(cIndex).EntireColumn.Cells(DST_KPI_ROW).Value = sKPI
End If
End Sub
评论
0赞
2mas
11/6/2023
Dim sKPI As Variant
为什么你在这里使用“变体”而不是“日期”?
0赞
2mas
11/6/2023
这是什么: - 声明和代码在一行中?Dim LastCol As Long: LastCol = dws.Cells(.Row, dws.Columns.Count).End(xlToLeft).Column
0赞
2mas
11/6/2023
为什么你在这里使用“设置”?Set drrg = .Resize(, LastCol - .Column + 1)
0赞
VBasic2008
11/6/2023
1.) 它无关紧要,因为它只写入一个单元格。如果它不小心不是日期,为什么代码会失败?2.) 变量声明和在一行中为变量赋值。IMO,完全可以接受。3.) 是一个范围:你必须(引用)一个范围(或任何其他对象,例如工作簿、工作表......它是第 2 行中最右边的非空单元格的单行。sKPI
drrg
set
S2
评论