简体   繁体   English

Excel VBA:根据单元格中包含的数字插入行

[英]Excel VBA: Inserting Rows Based on Number contained in Cells

I think the easiest way to get the desired answer is to just describe the entire scenario. 我认为获得所需答案的最简单方法是仅描述整个场景。 My company tracks projects using Primavera. 我的公司使用Primavera跟踪项目。 The scheduler exports the data into Excel and manipulates the data for his own uses. 调度程序将数据导出到Excel中,并处理数据以供自己使用。 He would like this manipulation to be automated using macros so that he doesn't have to repeat the same process every time. 他希望使用宏使这种操作自动化,这样他就不必每次都重复相同的过程。 Most of it is fairly simple, but I have stumbled on this one part. 大部分都非常简单,但是我在这一部分上偶然发现。

The first column in the workbook is the project number + name. 工作簿的第一列是项目编号+名称。 There are two types of projects: small projects that have a 3 digit number with an optional subtask number and large projects that have a 4 digit number with an optional subtask number. 项目有两种类型:具有3位数字且带有可选子任务编号的小型项目和具有4位数字且带有可选子任务编号的大型项目。 The cell is comprised of both numbers and text. 该单元格由数字和文本组成。 The general format is as follows: 通用格式如下:

Small Projects 小型项目

123 Small Project #1 123小项目#1

or 要么

123-4 Small project #2 123-4小项目#2

Large Projects 大型项目

1234 Large Project #1 1234大型项目#1

or 要么

1234-5 Large Project #2 1234-5大型项目#2

He inserts three rows to separate the two project types (small projects and large projects are always lumped together). 他插入三行来分隔这两种项目类型(小型项目和大型项目始终合并在一起)。 Is there a way, using VBA, to automate this process? 有没有一种使用VBA来自动化此过程的方法?

I was thinking it would go along the lines of "if first four characters in cell are numbers, then LargeProject = LargeProject + 1, else SmallProject = SmallProject + 1". 我想这将遵循“如果单元格中的前四个字符是数字,则LargeProject = LargeProject + 1,否则SmallProject = SmallProject + 1”。 Just don't know what expression (if one exists) can be utilized to accomplish this. 只是不知道可以使用哪种表达式(如果存在)来完成此任务。

Thanks! 谢谢!

If txt Like "### *" or txt Like "###-*" Then
   'small project
Elseif txt Like "#### *" or txt like "####-*" Then
   'large project
End If

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

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