2014-10-30 73 views
-1

对于Excel VBA来说是非常新颖的,并且很挣扎。
我是初级C#开发人员,但我发现在VBA中编写简单的语句对我来说非常棘手。
任何人都可以告诉我如何编写以下伪代码要求的VBA代码吗?Excel VBA - 努力创建按列值对单元格值进行操作的宏

  1. 插入到E栏只有在有是列X一个特定的字符串值
  2. 插入到E栏那里是文本(即:什么/任何东西)在列d和无正文:在列(即没有)一个
  3. 删除X只要有A列中的特定字符串值X在任何细胞

任何帮助都会非常感激。

+0

您到目前为止尝试过什么? – gmsantos 2014-10-30 09:21:40

+0

试图写如果陈述1A和1B,他们没有工作。我什至不能找到好的简单的VBA基础知识,只是告诉我你到底写了一个WHERE条件。另外,我只能找到有关如何指定精确单元格(Range(“A3”))或单元格块(Range(“A3:A100”))的信息,但不能简单地指定“A中具有特定字符串(不是数字)值“。请帮助! – 2014-10-30 09:25:40

+0

一个好的开始是[语言参考](http://msdn.microsoft.com/en-us/library/aa338033(v = vs.60).aspx) – gmsantos 2014-10-30 09:28:07

回答

0
Public Sub Text_Process() 
    Dim lngLastRow As Long 
    Dim lngRow As Long 
    Dim strColA As String 
    Dim strColD As String 

    lngLastRow = Sheet1.UsedRange.Rows.Count 

    For lngRow = 1 To lngLastRow ' change 1 to 2 if you have headings in row 1 
     strColA = Sheet1.Cells(lngRow, 1).Value ' store value in column A 
     strColD = Sheet1.Cells(lngRow, 4).Value ' store value of column D 

     Sheet1.Cells(lngRow, 5).Clear ' clear column E 

     If strColA = "X" Then ' or whatever you are looking for 
      Sheet1.Cells(lngRow, 5).Value = True 
     ElseIf strColA = "" And strColD <> "" Then 
      Sheet1.Cells(lngRow, 5).Value = False 
     End If 

     If strColA = "X" Then ' or whatever you are looking for 
      Sheet1.Cells(lngRow, 1).Clear ' clear out the value in column A, is this is what is requried? 
     End If 



    Next lngRow 

End Sub 
+0

嗨用户 - 您的解决方案完美工作。非常感谢你!我非常感谢您花这个时间,很棒的帮助! – 2014-10-30 11:17:36

+0

不客气 – smackenzie 2014-10-30 11:22:10

0

这是基本的东西。 VBA具有基本相同的VB 6语法。

您可以阅读language reference或在VBA编辑器上点击F1以获取帮助。

举个例子,检查这段代码。除了你的要求是有点混乱,在代码结构关注和使用的功能,如Range.CellsIsEmpty

Sub Macro1() 

    Dim limit, index As Integer 

    ' Iterate limit, just for test 
    limit = 20 

    ' Iterate your worksheet until the limit 
    For i = 1 To limit Step 1 

     ' Range param is a string, you can do any string contatenation you wish 
     ' Value property is what inside the cell 

     If (Not IsEmpty(Range("D" & i).Value) And IsEmpty(Range("A" & i).Value)) Then 
      ' Requirement 2 
      Range("E" & i).Value = False 

     ElseIf (Range("A" & i).Value = "X") Then 
      ' Requiriment 1 
      Range("E" & i).Value = True 

      ' Requiriment 3 
      Range("A" & i).Value = Empty 
     End If 
    Next 
End Sub 

像亚洲时报Siddharth说,录制宏和学习生成的代码是学习一些VBA的好方法招数。

+0

谢谢你的帮助,这真是太好了。 – 2014-10-30 11:16:54

相关问题