測試檔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
請參閱 : Excel VBA Loop - Easy Excel Macros
關鍵字 : Double Loop
r大~我對語法好像真的~無法欸天啊
r大給的已經是很簡單的範例了
題外話
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
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
50-79 放 array, 兩個 for 行完二維陣列便可以了吧