工作表1:
Jan 1
Jan 2
Jan 3
Feb 4
Feb 5
Mar 6
Mar 7
Apr 8
May 9
May 10
May 11
表格2:
Jan Feb Mar Apr May
1 X
2 X
3 X
4 X
5 X
6 X
7 X
8 X
9 X
10 X
11 X
我写了一个vba代码,将每个月的对应值复制到另一张纸上,但是当复制“ 1”时,我需要在B2中打印“ x”,复制“ 2”,然后在“ B3”中复制“ X” “ 分别。有人可以帮我吗?
答案 0 :(得分:0)
这是一个半硬代码示例,可以满足您的要求:
Sub months()
Dim jan As Integer, feb As Integer, mar As Integer, apr As Integer, may As Integer
Dim months As String, total As Integer
months = "Jan,Feb,Mar,Apr,May"
mon = Split(months, ",")
total = 1
While Sheets(1).Cells(total, 1) <> Empty
Sheets(2).Cells(total + 1, 1) = total
Select Case Sheets(1).Cells(total, 1)
Case "Jan"
jan = jan + 1
Case "Feb"
feb = feb + 1
Case "Mar"
mar = mar + 1
Case "Apr"
apr = apr + 1
Case "May"
may = may + 1
'Add more months if needed
End Select
total = total + 1
Wend
For j = 1 To UBound(mon) + 1
Sheets(2).Cells(1, j + 1) = mon(j - 1)
Next
For l = 1 To jan
Sheets(2).Cells(l + 1, 2) = "X"
Next
feb = feb + jan
For l = l To feb
Sheets(2).Cells(l + 1, 3) = "X"
Next
mar = mar + feb
For l = l To mar
Sheets(2).Cells(l + 1, 4) = "X"
Next
apr = apr + mar
For l = l To apr
Sheets(2).Cells(l + 1, 5) = "X"
Next
may = may + apr
For l = l To may
Sheets(2).Cells(l + 1, 6) = "X"
Next
End Sub
如前所述,请给我们您的代码,以便我们更好地帮助您!
答案 1 :(得分:0)
或者,您可以尝试使用以下代码:
Sub CopyMonths()
Dim lngLastRow As Long
Dim lngCount As Long
Dim lngColumn As Long
Dim lngRow As Long
Dim arrMonths As Variant
'two approaches: 1) either a static list of months:
'arrMonths = Array("Jan", "Feb", "Mar", "Apr", "May")
'2) or refer to Sheet2, range A1:E1
arrMonths = Array(Sheet2.Range("A1:E1").Value)
With Sheet1
lngLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For lngCount = 1 To lngLastRow
lngColumn = Application.WorksheetFunction.Match(.Range("A" & lngCount).Value, arrMonths, 0)
lngRow = .Range("B" & lngCount).Value
Sheet2.Cells(lngRow + 1, lngColumn).Value = "x"
Next lngCount
End With
End Sub