iT邦幫忙

0

excel vba For 多迴圈問題

  • 分享至 

  • xImage

https://ithelp.ithome.com.tw/upload/images/20210608/20109231JqGYyFy6ov.png
https://ithelp.ithome.com.tw/upload/images/20210608/20109231VPd8TnAgXc.png
測試檔https://drive.google.com/file/d/1rXeDabdjFp_z51J7U4Dc1_PPnVII4nCm/view?usp=sharing

大師們好~想請教我在網路上查到我需要使用的For迴圈寫法,但我找不到方法怎麼將多個迴圈精簡寫法,可以請大師們幫我看看嗎?

主要需求是從50~79列對應6~35列,如果50~79列該列有值時,將該列儲存格文字插入對應6~35列儲存格註解,例如:
50列有值,將50列有值儲存格插入第6列對應儲存格註解
51列有值,將51列有值儲存格插入第7列對應儲存格註解
52列有值,將52列有值儲存格插入第8列對應儲存格註解
以此類推.....

Sub 多列註解()
For i = 5 To [MQ50].End(xlToLeft).Column
Cx = Cells(50, i)
If Cx > "" Then
With Cells(6, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ51].End(xlToLeft).Column
Cx = Cells(51, i)
If Cx > "" Then
With Cells(7, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ52].End(xlToLeft).Column
Cx = Cells(52, i)
If Cx > "" Then
With Cells(8, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ53].End(xlToLeft).Column
Cx = Cells(53, i)
If Cx > "" Then
With Cells(9, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ54].End(xlToLeft).Column
Cx = Cells(54, i)
If Cx > "" Then
With Cells(10, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ55].End(xlToLeft).Column
Cx = Cells(55, i)
If Cx > "" Then
With Cells(11, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ56].End(xlToLeft).Column
Cx = Cells(56, i)
If Cx > "" Then
With Cells(12, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ57].End(xlToLeft).Column
Cx = Cells(57, i)
If Cx > "" Then
With Cells(13, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ58].End(xlToLeft).Column
Cx = Cells(58, i)
If Cx > "" Then
With Cells(14, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ59].End(xlToLeft).Column
Cx = Cells(59, i)
If Cx > "" Then
With Cells(15, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ60].End(xlToLeft).Column
Cx = Cells(60, i)
If Cx > "" Then
With Cells(16, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ61].End(xlToLeft).Column
Cx = Cells(61, i)
If Cx > "" Then
With Cells(17, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ62].End(xlToLeft).Column
Cx = Cells(62, i)
If Cx > "" Then
With Cells(18, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ63].End(xlToLeft).Column
Cx = Cells(63, i)
If Cx > "" Then
With Cells(19, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ64].End(xlToLeft).Column
Cx = Cells(64, i)
If Cx > "" Then
With Cells(20, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ65].End(xlToLeft).Column
Cx = Cells(65, i)
If Cx > "" Then
With Cells(21, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ66].End(xlToLeft).Column
Cx = Cells(66, i)
If Cx > "" Then
With Cells(22, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ67].End(xlToLeft).Column
Cx = Cells(67, i)
If Cx > "" Then
With Cells(23, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ68].End(xlToLeft).Column
Cx = Cells(68, i)
If Cx > "" Then
With Cells(24, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ69].End(xlToLeft).Column
Cx = Cells(69, i)
If Cx > "" Then
With Cells(25, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ70].End(xlToLeft).Column
Cx = Cells(70, i)
If Cx > "" Then
With Cells(26, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ71].End(xlToLeft).Column
Cx = Cells(71, i)
If Cx > "" Then
With Cells(27, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ72].End(xlToLeft).Column
Cx = Cells(72, i)
If Cx > "" Then
With Cells(28, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ73].End(xlToLeft).Column
Cx = Cells(73, i)
If Cx > "" Then
With Cells(29, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ74].End(xlToLeft).Column
Cx = Cells(74, i)
If Cx > "" Then
With Cells(30, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ75].End(xlToLeft).Column
Cx = Cells(75, i)
If Cx > "" Then
With Cells(31, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ76].End(xlToLeft).Column
Cx = Cells(76, i)
If Cx > "" Then
With Cells(32, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ77].End(xlToLeft).Column
Cx = Cells(77, i)
If Cx > "" Then
With Cells(33, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ78].End(xlToLeft).Column
Cx = Cells(78, i)
If Cx > "" Then
With Cells(34, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next

For i = 5 To [MQ79].End(xlToLeft).Column
Cx = Cells(79, i)
If Cx > "" Then
With Cells(35, i)
.AddComment
.Comment.Visible = False
.Comment.Text Text:=Cx
End With
End If
Next
End Sub
你可以把程式碼放在 <>裡嗎?
j大~放好了!哈
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
rogeryao
iT邦超人 7 級 ‧ 2021-06-08 16:40:31
最佳解答
看更多先前的回應...收起先前的回應...

r大~我對語法好像真的~無法欸天啊

achan iT邦研究生 3 級 ‧ 2021-06-09 08:11:06 檢舉

r大給的已經是很簡單的範例了

achan iT邦研究生 3 級 ‧ 2021-06-09 09:06:03 檢舉

題外話
1.註解要加上去,需先區分 原本有無註解 作法不同
2.[MQ列].End(xlToLeft).Column 你要判斷 最後有值欄位 是否該改成[MR列].End(xlToLeft).Column ?
3.Looping is one of the most powerful programming techniques 很基礎但也是最重要的入門技巧

謝謝r大跟a大~我再來研究研究~哈

r大~可以求救一下嗎?就是我怎麼試還是不成功,你可以幫我看看到底為什麼嗎?

Sub 多列註解()
Dim Cx As String
Dim i As Integer, j As Integer, c As Integer

For i = 3 To 32
    For c = 40 To 69
        For j = 5 To 355

        Cx = Cells(c, j)
        If Cx > "" Then
        With Cells(i, j)
        .AddComment
        .Comment.Visible = False
        .Comment.Text Text:=Cx
        End With
        End If

        Next j
    Next c
Next i
End Sub
rogeryao iT邦超人 7 級 ‧ 2021-06-12 13:50:05 檢舉
Private Sub CommandButton1_Click()
Dim Cx As String
Dim i As Integer, j As Integer
For i = 3 To 33
    For j = 5 To 355
        Cx = Cells(i + (40 - 3), j)
        If Cx <> "" Then
        With Cells(i, j)
        ' 刪除註解
        .ClearComments
        .AddComment
        .Comment.Visible = False
        .Comment.Text Text:=Cx
        End With
        End If
    Next j
Next i
End Sub

0
blanksoul12
iT邦研究生 5 級 ‧ 2021-06-08 15:15:56

50-79 放 array, 兩個 for 行完二維陣列便可以了吧

b大~可以提供寫法嗎?因為我真的搞不懂耶其實~

blanksoul12 iT邦研究生 5 級 ‧ 2021-06-09 08:31:13 檢舉

看得明邏輯嗎? 不明便 F8 續行看

For i = 1 To 10
    For y = 1 To 10
        Cells(i, y) = x
        x = x + 1
    Next
Next

vba 是要多做多看,不要挑自己想看的,什麼都要看一下

嗯嗯~我放假再來研究看看,謝謝b大~

我要發表回答

立即登入回答