[英]Visual Basic Excel For Loop In Multiple Cells How To Use The Counter In The Range(“”)
[英]Visual Basic Excel - Trying to copy cells based on a counter
因此,我有A列,如果需要,可以用數據填充143到179的單元格。 循環從A中獲取數據,並刪除所有空間中尚未填充的單元格(除格式化所需的幾個空格外),並將其放入C列。
因此,問題在於,即使只有4個單元格被填充,腳本也會從143復制到179,由於存在巨大的空間,因此將其復制到我們的系統時看起來很奇怪。 有沒有辦法讓它只復制/選擇填寫的內容,例如C143:“ C”&counter?
For i = 143 To 179
If i = 163 Or i = 165 Or i = 174 Then counter = counter + 1
If Cells(i, 1).Value <> "" Then
Cells(counter + 143, 3).Value = Cells(i, 1).Value
counter = counter + 1
End If
Next i
Range("C143:C179").Select
Selection.Copy
您的問題還不清楚,但是如果您只想將A列中具有值的單元格從C1單元復制到c列中,則可以完成此工作。 我不確定您為什么要跳過某些行,但我對此進行了注釋,但是,如果您確實需要,可以將其返回到代碼中。
Sub test()
Dim i As Integer
Dim WS As Worksheet
Set WS = ActiveSheet
counter = 1
For i = 143 To 179
'If i = 163 Or i = 165 Or i = 174 Then counter = counter + 1
If WS.Cells(i, 1).Value <> "" Then
WS.Cells(counter, 3).Value = WS.Cells(i, 1).Value
counter = counter + 1
End If
Next i
WS.Range("C1:C" & counter - 1).Select
Selection.Copy
End Sub
將值設置為變量。 像這樣:
For i = 143 To 179
If i = 163 Or i = 165 Or i = 174 Then counter = counter + 1
If Cells(i, 1).Value <> "" Then
Cells(counter + 143, 3).Value = Cells(i, 1).Value
counter = counter + 1
End If
Next i
lastrow1 = Cells(Rows.Count, "A").End(xlUp).Row
Range("C143:C" & lastrow1).Select
Selection.Copy
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.