iT邦幫忙

0

尋找最後一列的某字串

請問大大們,如果要尋找欄位裡最後一列的"六",然後再向上14格,找到"六",並畫上雙底線
用excel vba要如何寫呢?
https://ithelp.ithome.com.tw/upload/images/20190613/20118250pV9YDSgT3x.jpg

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
paicheng0111
iT邦大師 5 級 ‧ 2019-06-13 13:24:59
最佳解答
Sub test()
    Dim myRng As Range, i As Long
    
    Set myRng = Cells(Rows.Count, 1).End(xlUp)
    For i = myRng.Row To 1 Step -1
        With Cells(i, 1)
            If .Value = "六" Then
                .Borders(xlEdgeBottom).LineStyle = xlDouble
                Exit For
            End If
        End With
    Next i
    
    i = i - 14
    Do Until i <= 0
      With Cells(i, 1)
          If .Value = "六" Then
              .Borders(xlEdgeBottom).LineStyle = xlDouble
          End If
      End With

      i = i - 14
    Loop
End Sub
no1kent iT邦新手 5 級 ‧ 2019-06-14 06:26:20 檢舉

PCW,不好意思,如果雙底線不是只有一格,還要延伸到Z56,要加什麼程式碼呢?

.Borders(xlEdgeBottom).LineStyle = xlDouble

改成

Range(.Cells(1, 1), .Cells(1, 26)).Borders(xlEdgeBottom).LineStyle = xlDouble
no1kent iT邦新手 5 級 ‧ 2019-06-14 10:46:24 檢舉

/images/emoticon/emoticon12.gif
大感謝!

0
listennn08
iT邦高手 5 級 ‧ 2019-06-13 11:42:25

應該是這樣吧
我不會寫vba 所以也不確定@@

For i = 1 To 60
    If Cells(i * 14, 1) = "六" Then
        Cells(i * 14, 1).Font.Underline = xlUnderlineStyleSingle
    End If
Next

看了一下你要從最後一列找
那應該是這樣

For i = lastSaturday To 1 Step -1
    If i Mod 14 = 0 And Cells(i, 1) = "六" Then
            Cells(i, 1).Font.Underline =xlUnderlineStyleSingle
    End If
Next
看更多先前的回應...收起先前的回應...

請問為什麼要乘除14,而不是加減14?

當 i = 56 的話 -14 = 42
可是step= -1 當i跑到49再 -14 ,35跟42就沒有相隔兩個禮拜了

對不起,今天才看到訊息。
你誤會我的意思,往上找14格要-14,
不是指迴圈變數。

 Cells(i-14, 1).Font.Underline =xlUnderlineStyleSingle

我懂你的意思了,我餘數只是用來判斷有沒有相差14天
乘的話是找第一個14天第二個14天... 這樣@@
因為我沒寫過vba所以我看問題的直接想法是這樣
不知道這樣有沒有回答到你的問題

0
小魚
iT邦大師 1 級 ‧ 2019-06-13 13:58:35
Private Sub Test()
    Dim lastRow As Integer
    lastRow = Cells(1, 1).End(xlDown).Row
    Dim i As Integer
    For i = lastRow To 1 Step -1
        If Cells(i, 1).Value = "六" Then
            Cells(i, 1).Font.Underline = xlUnderlineStyleDoubleAccounting
            If i - 14 > 0 Then
                Cells(i - 14, 1).Font.Underline = xlUnderlineStyleDoubleAccounting
            End If
            MsgBox ("完成!")
            Exit For
        End If
    Next
End Sub

底線樣式可以看 這裡

我要發表回答

立即登入回答