简体   繁体   English

在Excel VBA宏中创建一个新行(我做错了什么?)

[英]Creating a new row in Excel VBA macro (what am I doing wrong?)

I am working on an Excel macro that merges two spreadsheets - a list of companies, and a list of emails associated with those companies. 我正在研究一个Excel宏,它合并了两个电子表格 - 公司列表,以及与这些公司相关的电子邮件列表。 Whenever a company has more than one email associated, I need to create a separate row for that email. 每当公司有多个电子邮件关联时,我需要为该电子邮件创建一个单独的行。

Everything goes correctly until I attempt to create the new row with the code Rows(row).Resize(1).Insert towards the end of the macro. 一切都正常,直到我尝试使用代码Rows(row).Resize(1).Insert创建新行Rows(row).Resize(1).Insert 。插入宏的末尾。 As soon as it gets to this line, Excel duplicates each row's first column ad infinitum (until column XEI ). 一旦到达此行,Excel就会无限制地复制每一行的第一列(直到列XEI )。

How do I modify my code so that I create one new row (under the row my loop is currently on) instead of a million columns? 如何修改我的代码,以便创建一个新行(在我的循环当前行的行下)而不是一百万列? My code is as follows: 我的代码如下:

Sub Commandbutton1()


ThisWorkbook.Sheets("company").Activate


Sheet2.Range("A1:A10000").Select

Selection.Copy


ThisWorkbook.Sheets("Sheet1").Activate


Sheet1.Range("A1:A10000").Value = Sheet2.Range("A1:A10000").Value

Sheet1.Range("B1").Value = "First Name"

Sheet1.Range("C1").Value = "Last Name"

Sheet1.Range("D1").Value = "Email"




Dim i As Integer


i = 1


Do While i <= 100

Dim companyName As String
companyName = Cells(i, 1).Value
firstname = Cells(i, 2).Value
lastname = Cells(i, 3).Value

'Query contacts list
'Find all rows containing companyName
'Find the email in those rows
'Add the email to row i

Dim slot As Integer

slot_email = 4


Dim result As String
Dim sheet As Worksheet
Set sheet = ActiveWorkbook.Sheets("contact")


Dim isFirstInstance As Integer

isFirstInstance = 0

Dim j As Integer

For j = 1 To sheet.Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count



    Dim k As Integer
    For k = 1 To 39
        Dim cellVal As String
        cellVal = ActiveWorkbook.Worksheets("contact").Cells(j, k).Value
            If cellVal = "" Then
            Exit For
        ElseIf cellVal = companyName Then
            Debug.Print ("For company " & companyName & ", found value on row " & j & " col " & k)
            Cells(i, 4).Value = ActiveWorkbook.Worksheets("contact").Cells(j, 4).Value
            Cells(i, 2).Value = ActiveWorkbook.Worksheets("contact").Cells(j, 2).Value
            Cells(i, 3).Value = ActiveWorkbook.Worksheets("contact").Cells(j, 3).Value

            isFirstInstance = isFirstInstance + 1
                Debug.Print (isFirstInstance & " on column " & k)

            If isFirstInstance > 1 Then
                Debug.Print ("Found a duplicate contact!")

                Dim row As String
                row = i

                Rows(row).Resize(1).Insert

                i = i + 1
            End If

        End If
    Next k
Next j

i = i + 1


Loop



End Sub

I believe Rows(row).Resize(1).Insert is going to shift your single column down instead of the whole row (for you to then insert the new row of data). 我相信Rows(row).Resize(1).Insert会将你的单列向下移动而不是整行(为了你然后插入新的数据行)。 I think you want to use: Rows(5).EntireRow.Insert for example and also use Application.CutCopyMode = False so that it doesn't try to insert your previously copied data 我想你想使用:Rows(5).EntireRow.Insert例如,并且还使用Application.CutCopyMode = False,这样它就不会尝试插入以前复制的数据

Sub InsertRowAtSecondLine()
    Dim rowOfInterest As Long
    rowOfInterest = 2
    Cells(rowOfInterest, 1).EntireRow.Insert
End Sub

will transform 会改变

a
b
c
d

to

a
<blank row>
b
c
d

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

相关问题 我究竟做错了什么? 使用Excel VBA删除重复项 - What am I doing wrong? Removing duplicates using Excel VBA Excel VBA:在for循环中跳转:“ Next for For” –我在做什么错? - Excel VBA: Jump inside a for loop: “Next without For” – What am I doing wrong? Excel VBA Find函数未返回整数值,看不到我在做什么 - Excel VBA Find function not returning an integer value, can't see what I am doing wrong VBA创建数据透视表,类型不匹配? 我究竟做错了什么? - VBA to Create PivotTable, Type Mismatch? What Am I Doing Wrong? 插入Excel VBA以在新行中运行宏 - Excel VBA to run Macro in new row is Inserted 我正在使用VBA DateSerial函数分隔字符串中的日期,但是当字符串中的年份为&#39;1000&#39;时,Excel Ends Sub会出现。 我究竟做错了什么? - I am using the VBA DateSerial function to separate dates in a string, but Excel Ends Sub when year in string is '1000'. What am I doing wrong? 我做错了什么? VBA如果这个和这个然后这个 - What I'm doing wrong ? VBA IF this AND this then this VBA运行时错误91对象变量未设置-我在做什么错? - VBA Runtime Error 91 Object Variable Not Set - What am I doing wrong? 使用VBA宏将Excel数据的每一行创建为xml文件 - Creating each row of excel data into xml files using VBA Macro Macro VBA:我的代码有什么问题? 行删除 - Macro VBA : What is wrong with my code ? Row delete
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM