我正在寻找类似于vlookup的东西,但有宏观和附加功能。我不知道从哪里开始需要你的帮助。
所以我有一张Sheet 1
Cell A1: bu 200000 olacaktır
Cell A2: BCD
Cell A3: CDE
Cell A4: DEF
Sheet2:
A1: BCD
A2: DEF
因此,在板2中缺少“200000olacaktır”和CDE,所以我希望在Sheet2单元格a3和sheet2单元格中添加这些值a4
结果应与此类似 Sheet2:
A1: BCD
A2: DEF
A3: bu 200000 olacaktır
A4: CDE
有没有办法做这个公式或宏?
大家好,我用宏做了些什么,但遇到了一些小错误,需要你的帮助才能让它变得完美。
Sub yenilikleri_ekle()
Dim sh1 As Worksheet, sh2 As Worksheet, lr As Long, rng As Range
Set sh1 = ThisWorkbook.Worksheets("Input")
Set sh2 = ThisWorkbook.Worksheets("Ayarlar")
lr = sh1.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = sh1.Range("B5:B" & lr)
For Each c In rng
If WorksheetFunction.CountIf(sh2.Range("A:A"), c.Value) = 0 Then
sh2.Range("A" & sh2.Cells(Rows.Count, 1).End(xlUp).Row)(2) = c.Value
End If
Next
End Sub
它完成了这项工作,但只有当一个单元格中包含一些数字时才会混淆,例如
这个粗体值已经复制了两次,即使它已经存在,除了其他所有值都没问题。
bu200000olacaktır
那么我该如何改进编码?
答案 0 :(得分:0)
我真的不认为这可以通过公式轻松完成。
最简单的方法是:
更正了您的代码,使其正常运行。请参阅我所做更改的评论:
Sub yenilikleri_ekle()
Dim sh1 As Worksheet, sh2 As Worksheet, lr As Long, rng As Range
Set sh1 = ThisWorkbook.Worksheets("Input")
Set sh2 = ThisWorkbook.Worksheets("Ayarlar")
lr = sh1.Cells(Rows.Count, 2).End(xlUp).Row 'Changed column 1 for column 2, since you're after refering to column "B"
Set rng = sh1.Range("B5:B" & lr)
For Each c In rng
If WorksheetFunction.CountIf(sh2.Range("A:A"), c.Value) = 0 Then
sh2.Range("A" & sh2.Cells(Rows.Count, 1).End(xlUp).Row + 1) = c.Value 'Removed the (2) that had no meaning, and added a +1 to go to the next available line
End If
Next
End Sub
尝试替换
If WorksheetFunction.CountIf(sh2.Range("A:A"), c.Value) = 0 Then
与
If WorksheetFunction.CountIf(sh2.Range("A:A"), CStr(c.Value)) = 0 Then