excel 带有嵌套If语句的For循环

4sup72z8  于 2023-03-24  发布在  其他
关注(0)|答案(3)|浏览(166)

bounty将在6天后过期。回答此问题可获得+50声望奖励。Saadiq希望引起更多人关注此问题。

我试图构建一个VBA工具,在每行中搜索特定的文本字符串(例如“Salary”),复制相邻的值,并将其粘贴到指定范围内的下一个空单元格中。但是,如果在行中找不到特定的文本字符串,我希望该工具将“NULL”粘贴到指定范围内的下一个空单元格中。
这是我目前为止所做的,但我认为我已经搞砸了循环和if语句的语法。我是VBA的新手,不知道如何修复它:

Sub IF_Statement_test()

Dim rng As Range
Dim row As Range
Dim cell As Range

Set rng = Range("A1:B4")

For Each row In rng.Rows
 
If row.Cells.Find(What:="Salary") = True Then
  row.Cells.Find(What:="Salary").Offset(0, 1).Copy
  Range("A8").Find(What:="Salary").Offset(0, 1).Select
  If Not IsEmpty(ActiveCell) Then
   Range("A8:E8").End(xlToRight).Offset(0, 1).PasteSpecial
  Else
  If IsEmpty(ActiveCell) Then
   ActiveCell.PasteSpecial
  ElseIf row.Cells.Find(What:="Salary") = False Then
    Range("A8").Find(What:="Salary").Offset(0, 1).Select
  If Not IsEmpty(ActiveCell) Then
    Range("A8:E8").End(xlToRight).Offset(0, 1).Value = "NULL"
  ElseIf IsEmpty(ActiveCell) Then
   ActiveCell.Value = "NULL"
 End If

Application.CutCopyMode = False
Next
End Sub
vsmadaxz

vsmadaxz1#

查找字符串

Option Explicit

Sub FindString()

    Dim ws As Worksheet: Set ws = ActiveSheet ' improve
    
    Dim srg As Range: Set srg = ws.Range("A1:B4")

    Dim dCell As Range:
    Set dCell = ws.Cells(8, ws.Columns.Count).End(xlToLeft).Offset(, 1)
    
    Dim srrg As Range, sCell As Range
    
    For Each srrg In srg.Rows
        Set sCell = srrg.Find("Salary", , , xlWhole)
        If sCell Is Nothing Then
            dCell.Value = "NULL"
        Else
            dCell.Value = sCell.Offset(, 1).Value
        End If
        Set dCell = dCell.Offset(, 1)
    Next srrg

End Sub
camsedfj

camsedfj2#

你可以试试这个(未测试):

If row.Cells.Find(What:="Salary") = True Then
  row.Cells.Find(What:="Salary").Offset(0, 1).Copy
  Range("A8").Find(What:="Salary").Offset(0, 1).Select
  If Not IsEmpty(ActiveCell) Then
   Range("A8:E8").End(xlToRight).Offset(0, 1).PasteSpecial
  ElseIf IsEmpty(ActiveCell) Then
    ActiveCell.PasteSpecial
  ElseIf row.Cells.Find(What:="Salary") = False Then
    Range("A8").Find(What:="Salary").Offset(0, 1).Select
    If Not IsEmpty(ActiveCell) Then
      Range("A8:E8").End(xlToRight).Offset(0, 1).Value = "NULL"
    ElseIf IsEmpty(ActiveCell) Then
      ActiveCell.Value = "NULL"
    End If
  End If
End If
ghhaqwfi

ghhaqwfi3#

Sub IF_Statement_test()
    Dim rng As Range
    Dim row As Range
    Dim cell As Range
    Dim salary As Variant
    Dim lastColumn As Long
    Dim destRow As Long
    
    Set rng = Range("A1:B4")
    destRow = ActiveSheet.UsedRange.Find("Salary Value", , , xlWhole).row
    
    For Each row In rng.Rows
        'find the salary value
        Set cell = row.Find("Salary", , , xlWhole)
        If cell Is Nothing Then
            salary = "Null"
        Else
            salary = cell.Offset(0, 1).Value
        End If
        
        
        lastColumn = Cells(destRow, Columns.Count).End(xlToLeft).Column
    
        Cells(destRow, lastColumn + 1).Value = salary
        
    Next row
    
End Sub

相关问题