[英]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.