简体   繁体   English

在Excel中使用VBA宏将文本插入空白行

[英]Inserting text to blank row using vba macro in excel

I have a data of say more than 5000 rows and 10 columns. 我有说超过5000行和10列的数据。 I would like to add a text to the rows based on columns conditions. 我想根据列条件向行添加文本。

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

Based on the column state, for NCE the cluster name is "nce.net" has to be assigned to column D (clustername) and also for MAD is "muc.net" to be assigned to row 2. 基于列状态,对于NCE,必须将群集名称“ nce.net”分配给D列(群集名称),对于MAD,将群集名称“ muc.net”分配给第2行。

could you please help me out. 你能帮我吗

Here is my code: 这是我的代码:

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

Could you please help me out. 你能帮我一下吗。

Why not a simple formula 为什么不是一个简单的公式

In D1 and copy down D1并向下复制

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

Doing the same this with code 用代码做同样的事情

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

You may create a reference table consisting of unique state and clustername in a seperate worksheet and then pull the clustername into your original sheet using a =VLOOKUP() function ... provided there is a 1:1 relation between state and cluster ... even 1 cluster for multiple states would work. 您可以在单独的工作表中创建一个包含唯一状态和集群名称的引用表,然后使用=VLOOKUP()函数将集群名称拉入原始表中...只要状态与集群之间存在1:1关系...即使是多个状态的1个群集也可以工作。 This way you avoid hardcoding and you can react quickly if cluster names change. 这样可以避免硬编码,并且如果群集名称发生更改,您可以快速做出反应。

Example: 例:

in Sheet2 list all countries and their associated clusternames in Sheet1 enter =VLOOKUP(...) into first row of clustername column as per below picture and copy down for all rows 在Sheet2中列出所有国家及其在Sheet1中相关的群集名称,如下图所示,在群集名称列的第一行中输入= VLOOKUP(...),并向下复制所有行

在此处输入图片说明

Of course you may want to have values only, not formulas in your cluster column; 当然,您可能只希望有值,而在群集列中不希望有公式。 then you can convert formulas into values by copying and then pasting as values that cluster column after you've entered the =VLOOKUP(...) formula. 那么您可以在输入=VLOOKUP(...)公式后,通过复制然后粘贴为群集列的值,将公式转换为值。

Alternatively, if eg you have a lot of clusternames already defined and only want to work on rows where clustername is blank, you can 或者,例如,如果您已经定义了很多集群名称,并且只想在集群名称为空的行上工作,则可以

  1. filter for blank clusternames and insert the =VLOOKUP(...) only there 筛选空白集群名称,然后仅在此处插入=VLOOKUP(...)
  2. use a small piece of code 用一小段代码

     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 

Personally I don't like this kind of hardcoding at all, I'd rather take the clusternames from a table ... so for me there wouldn't be a need to write code unless the whole task is much more complex than described. 我个人根本不喜欢这种硬编码,我宁愿从表中获取集群名称……因此对我而言,除非整个任务比所描述的要复杂得多,否则就无需编写代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM