我本質上是希望將帶有文本的任何單元格從 E/F 列對面的兩列之一移動到 A。保持該行的所有其他內容相同。已經完成并找到了具有特定字串的示例,但沒有僅包含文本的示例。我認為我的問題可能在于 cell.value = IsText 的格式。也不確定如何將范圍設定為包含資料的列/作業表中的最后一行。其他例子是整張紙(甚至是空白),有時會很慢。
Sub MoveJobFunctions()
Dim row As Long
For row = 2 To 1000
'Check if is text
If Range("E" & row).Value = IsText Then
' Copy the value and then blank the source.
Range("A" & row).Value = Range("E" & row).Value
End If
Next
End Sub
uj5u.com熱心網友回復:
該代碼會將您讀取IsText為未宣告的變數。因此,代碼將檢查 E 列中的值是否“等于無”。這段代碼應該可以解決問題:
Option Explicit
Sub MoveJobFunctions()
Dim row As Long
For row = 2 To 1000
'Check if is text
If Excel.WorksheetFunction.IsText(Range("E" & row).Value) And Not IsDate(Range("E" & row).Value) Then
' Copy the value and then blank the source.
Range("A" & row).Value = Range("E" & row).Value
Range("E" & row).ClearContents
End If
Next
End Sub
如您所見,Excel.WorksheetFunction.IsText使用該IsText功能的正確方法。我添加了Option Explicit要求您宣告任何變數的陳述句(否則會發生錯誤)。它將幫助您發現錯誤,例如使您的代碼無效的錯誤。我還添加了一行來清除源值(如代碼注釋中所指定)。
新評論后添加的注釋:我添加了一個IsDate函式來檢查該值是否為日期。IsDate是存盤在VBA.Information庫中的函式。由于它是一個 VBA 函式,您不需要指定整個地址,因為IsText它是一個 Excel 函式(并且它存盤在Excel.WorksheetFunction庫中)。有關IsDate 此處的更多資訊。
為了僅涵蓋串列而不是整個作業表(或硬編碼的行數),您可以使用一些技巧,例如此鏈接中的技巧。一個可能的代碼(如果串列下沒有任何內容)可能是這個:
Option Explicit
Sub MoveJobFunctions()
Dim row As Long
Dim lastrow As Long
lastrow = Range("E" & Cells.Rows.Count).End(xlUp).row
For row = 2 To lastrow
'Check if is text
If Excel.WorksheetFunction.IsText(Range("E" & row).Value) And Not IsDate(Range("E" & row).Value) Then
' Copy the value and then blank the source.
Range("A" & row).Value = Range("E" & row).Value
Range("E" & row).ClearContents
End If
Next
End Sub
如果串列是一系列不間斷的資料,您可以使用以下代碼:
Option Explicit
Sub MoveJobFunctions()
Dim row As Long
Dim lastrow As Long
lastrow = Range("E2").End(xlDown).row
For row = 2 To lastrow
'Check if is text
If Excel.WorksheetFunction.IsText(Range("E" & row).Value) And Not IsDate(Range("E" & row).Value) Then
' Copy the value and then blank the source.
Range("A" & row).Value = Range("E" & row).Value
Range("E" & row).ClearContents
End If
Next
End Sub
或者這個:
Option Explicit
Sub MoveJobFunctions()
Dim cell As Range
Set cell = Range("E2")
Do Until cell.Value = ""
'Check if is text
If Excel.WorksheetFunction.IsText(cell.Value) And Not IsDate(cell.Value) Then
' Copy the value and then blank the source.
Range("A" & cell.row).Value = cell.Value
cell.ClearContents
End If
'Offsetting cell down to the next row.
Set cell = cell.Offset(1, 0)
Loop
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/489368.html
