2017-02-03 89 views
1

我有一個循環放置在Excel工作表中的每個第10個條目上的粗體字體,但是我想要每20個條目的粗體字體。我該如何解決這個問題?任何幫助是極大的讚賞。VBA:使每20個單元格粗體

這是我的工作代碼:

'insert xxxx9 after xxxx8 if needed 
For i = 3 To counter - 1 
    If Cells(i, 1).Value Mod 10 = 8 Then 
    ' counter = counter + 1 
     If Cells(i + 1, 1) <> Cells(i, 1) + 1 Then 

      i = i + 1 
      Rows(i & ":" & i).Select 
      Selection.Insert Shift:=xlDown 

      Cells(i, 1) = Cells(i - 1, 1) + 1 
      Range("A" & 20).Select 
      Selection.Font.Bold = True 
      Cells(i, 2) = "900" 
      Range("B" & i).Select 
      Selection.Font.Bold = True 
      Cells(i, 3) = "chk" 
      Range("C" & i).Select 
      Selection.Font.Bold = True 
      Cells(i, 6) = "1" 
      Range("F" & i).Select 
      Selection.Font.Bold = True 
     Else 
      i = i + 1 
      Rows(i & ":" & i).Select 
      Range("A" & i).Select 
      Selection.Font.Bold = True 
     End If 
    End If 
Next 


'count number of entries 
counter = 2 
While (Cells(counter, 1).Value <> "" Or Cells(counter, 1).Value <> Null) 
    counter = counter + 1 
Wend 

'insert xxxx9 after xxxx8 if needed 
For i = 3 To counter - 1 
    If Cells(i, 1).Value Mod 10 = 8 Then 
    ' counter = counter + 1 
     If Cells(i + 1, 1) <> Cells(i, 1) + 1 Then 

      i = i + 1 
      Rows(i & ":" & i).Select 
      Selection.Insert Shift:=xlDown 

      Cells(i, 1) = Cells(i - 1, 1) + 1 
      Range("A" & 20).Select 
      Selection.Font.Bold = True 
      Cells(i, 2) = "900" 
      Range("B" & i).Select 
      Selection.Font.Bold = True 
      Cells(i, 3) = "chk" 
      Range("C" & i).Select 
      Selection.Font.Bold = True 
      Cells(i, 6) = "1" 
      Range("F" & i).Select 
      Selection.Font.Bold = True 
     Else 
      i = i + 1 
      Rows(i & ":" & i).Select 
      Range("A" & i).Select 
      Selection.Font.Bold = True 
     End If 
    End If 
Next 
+4

改變'for ... next'迭代的方式,比如'For i = 3 To counter - 1 Step 20'。這將通過消除檢查你是否在正確的行上的必要性來加速日常工作。 – Jeeped

+0

感謝您的幫助。這在某種程度上是有效的,但現在它也停止了對第20項的大膽介入。 –

+1

不確定我完全理解,但它看起來像你正在選擇每10行進行處理 - 也就是說,它看起來像你期望第一列包含連續的數字,所以'如果Cells(i,1).Value Mod 10 = 8'選擇值爲8,18,28等的行。這可以解釋爲什麼@Jeeped的建議導致粗體停止。我認爲你需要更好地解釋你的目標,以獲得良好的幫助。 – vknowles

回答

0

國防部10除以10後回國,其餘要獲得條件是真實的,只有一次,每20行,使用國防部20來代替。相應地調整它是否相等,以確保它在正確的位置啓動。

相關問題