【问题标题】:Inserting text to blank row using vba macro in excel在excel中使用vba宏将文本插入空白行
【发布时间】:2015-08-28 20:15:07
【问题描述】:

我有超过 5000 行和 10 列的数据。我想根据列条件向行添加文本。

    A              B               C            D
   fname          lname         state        clustername
1.  ram           giri            NCE         ...  
2. philips        sohia           MAD         ...
3. harish        Gabari           NCE         ....

根据列状态,对于 NCE,必须将集群名称“nce.net”分配给列 D(集群名称),对于 MAD,必须将“muc.net”分配给第 2 行。

你能帮帮我吗?

这是我的代码:

dim emptyrow as string
row_number = 1
lastRow = Cells(Rows.Count, "D").End(xlUp).Row
state = sheets("sheet1").rows("C" & row_number)
for each cell in selection
 if instr(state, "NCE") = true then
    Range(Cells(emptyrow, "D").Value = Array("nce.net")
 end if
next emptyrow

你能帮帮我吗?

【问题讨论】:

  • 你为什么不在 D 列中使用像 =IF(C2="NCE";"nec.net";IF(C2="MAD";"muc.net";"…")) 这样的简单公式(将其放入 D2 并复制下来)?
  • @Ralph 我同意你的观点,但它仅适用于较少的行数。想象一下,如果您有 4000 行并且需要将文本分配给空白行。

标签: vba excel


【解决方案1】:

为什么不是一个简单的公式

D1 并复制下来

=IF(C1="NCE","nce.net",IF(C1="MAD","muc.net","No match"))

用代码做同样的事情

Sub Simple()
Dim rng1 As Range
Set rng1 = Range([c1], Cells(Rows.Count, "C").End(xlUp))
With rng1.Offset(0, 1)
    .FormulaR1C1 = "=IF(RC[-1]=""NCE"",""nce.net"",IF(RC[-1]=""MAD"",""muc.net"",""No match""))"
    .Value = .Value
End With
End Sub

【讨论】:

    【解决方案2】:

    您可以在单独的工作表中创建一个包含唯一 state 和 clustername 的引用表,然后使用 =VLOOKUP() 函数将 clustername 拉入您的原始工作表......前提是 state 和 cluster 之间存在 1:1 的关系。 .. 即使是多个州的 1 个集群也可以工作。这样可以避免硬编码,并且可以在集群名称发生变化时快速做出反应。

    示例:

    在 Sheet2 中列出所有国家及其关联的集群名称 在 Sheet1 中,按照下图将 =VLOOKUP(...) 输入到 clustername 列的第一行并复制所有行

    当然,您可能只想在集群列中包含值,而不是公式;然后您可以在输入=VLOOKUP(...) 公式后,通过复制并粘贴为聚类列的值将公式转换为值。

    或者,例如,如果您已经定义了很多集群名称,并且只想处理集群名称为空的行,您可以

    1. 过滤空白簇名并仅在此处插入=VLOOKUP(...)
    2. 使用一小段代码

      Sub DoCluster()
      Dim R As Range, S As Integer, C As Integer, Idx As Integer
      
      Set R = ActiveSheet.[A2] ' top left cell of table
      S = 3                    ' column index of State column
      C = 4                    ' column index of Clustername column
      Idx = 1                  ' start at 1st row within range
      
      ' run a loop across all rows, stop if 1st column gets blank
      Do While R(Idx, 1) <> ""
      
          ' work only on rows wher cluster not yet set
          If R(Idx, C) = "" Then
              ' now this isn't really good ... try to avoid hardcoding BY ANY MEANS
              Select Case R(Idx, 3)
      
              Case "NCE"
                  R(Idx, 4) = "nce.net"
      
              Case "MAD"
                  R(Idx, 4) = "muc.net"
      
              ' insert other cases here as per need
              ' ...
      
              ' trap undefined cases
              Case Else
                  R(Idx, 4) = "undefined"
              End Select
          End If
      
          Idx = Idx + 1
      Loop
      End Sub
      

    就我个人而言,我根本不喜欢这种硬编码,我宁愿从表中获取集群名称......所以对我来说,除非整个任务更加复杂,否则不需要编写代码比描述的多。

    【讨论】:

    • 能否请您使用 vlookup() 函数发布一些示例代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多