簡體   English   中英

對於列/行第 2 部分中的所有值

[英]For all values in a column/Row Part 2

我對 VBA 非常陌生,所以請原諒我的問題的任何問題。

我的流程步驟如下

檢查第 3 行中的所有日期(有時該行中有幾個相同的日期,我想檢查所有日期)並查看它是否與單元格 A1 匹配

如果日期匹配,請檢查第 4 行(日期正下方)中的名稱是否與單元格 B1 匹配。

如果兩者都匹配,則直接在下面寫“是”,即第 5 行

如果沒有找到名字。 然后添加一個包含日期和名稱的列並寫上是

如果沒有找到日期,添加一個包含日期和名稱的列並寫上是

然后將循環 A 列和 B 列中的所有日期和名稱

示例預宏

示例帖子宏

我的問題

在進入下一個 if 語句之前,我似乎無法找到一種檢查所有日期的方法 - 因此我最終陷入了一個連續循環並且我的 excel 崩潰了。

到目前為止我所做的示例


Sub Macro1()





 Dim cel_1 As Range

 Dim cel_2 As Range







For Each cel_1 In Range("3:3")

     If cel_1.Value = Range("A1") Then

     

    cel_1.Range("A2").Select



AddInfo:

'if we find the date then we need to ensure if the team member is already there

     For Each cel_2 In Selection

     



     If cel_2.Value = Range("B1") Then



'if the team member is there we will input all information over current information



     cel_2.Offset(1, 0).Range("A1") = "Yes"



Else

'insert column to the right

ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Insert _

Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove



'label the week number, date and name

ActiveCell.Offset(-1, 1) = ActiveCell.Offset(-1, 0)

ActiveCell.Offset(0, 1) = ActiveCell.Offset(0, 0)

 

 ActiveCell.Offset(1, 1).Select

 

 GoTo AddInfo:





End If

Next cel_2

End If

Next cel_1





End Sub

我相信我已經使用以下代碼回答了我的問題 - 但是,這有效,不會遍歷 A 列和 B 列。


Sub Macro1()

    Dim myPath As String
    Dim myFile As String
    Dim myExtension As String
  
    Dim wb As Workbook
    Dim wb1 As Workbook
    
    Dim i As Variant
    Dim j As Variant
    Dim k As Variant
    
    Dim CountA_Range As Range
    Dim CountB_Range As Range
    Dim n As Range
    Dim cel_1 As Range
    Dim cel_2 As Range
    Dim lookFor As Range
    Dim srchRange As Range

'rerun forces the macro to rerun if it has added a new cloumn of information
Rerun:

'checked if the name and date is already in the table and adds data
For Each cel_1 In Range("3:3")
       If cel_1.Value = Range("A1") And cel_1.Offset(1, 0).Value = Range("B1") Then
         
k = "Match"
i = "Match"
cel_1.Range("A1").Select
End If
Next cel_1

If k = i And k = "Match" Then

ActiveCell.Offset(2, 0).Value = "Yes"

Else

'if the name and data is not in the data then the columns are added
Range("C3").End(xlToRight).Select
Selection.Offset(0, 1) = Range("A1").Value
Selection.Offset(1, 1) = Range("B1").Value

'rerun - now that we've added the column it should populate when we ran
GoTo Rerun:
End If

'sort the dates so the information is in order
Range("C3").End(xlToRight).Select

Range("C3").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveWorkbook.Worksheets("Sheet5").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet5").Sort.SortFields.Add Key:=ActiveCell.Range _
        ("A1:P1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet5").Sort
        .SetRange ActiveCell.Range("A1:P100")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlLeftToRight
        .SortMethod = xlPinYin
        .Apply
    End With

End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM