我意识到有7个问题与这个相同的主题有关,但我无法弄清楚我的生活(愚蠢,我猜大声笑)。我正在尝试运行一个利用工作表的VBA宏。我需要宏来执行以下操作:
这是我到目前为止所做的:
Sub IndustryMatch()
Dim ws As Worksheet
Dim sh1 As Worksheet
Dim sh3 As Worksheet
Dim Wbk As Workbook
Set Wbk = ActiveWorkbook
Set sh1 = Wbk.Sheets("Database")
Set sh3 = Wbk.Sheets("Industry")
sh1.Activate
Dim row As Integer
Dim col As Integer
'Dim currentK As String
Dim numRowsK As String
numRowsK = Wbk.Worksheets("Database").Range("K2",
Range("K2").End(xlDown)).Rows.Count
For a = 1 To numRowsK + 1 'start at 1 because it is the first row. This will loop through Column K
'currentK = Wbk.Sheets("Database").Cells(k, "Category").Value 'Save the current value in column K
sh3.Activate
numRowsG = Wbk.Worksheets("Industry").Range("G2", Range("G2").End(xlDown)).Rows.Count
For c = 1 To numRowsG + 1 'start at 1 because it is the first row. This will loop through Column G
If (Wbk.Sheets("Industry").Cells(g, "G").Value = Wbk.Sheets("Database").Cells(k, "K").Value) Then 'Check if the col G value is equal to the current col K value.
Wbk.Sheets("Database").Cells(j, "J").Value = Wbk.Sheets("Database").Cells(g, "G").Value 'If so copy to column J
End If
Next c
Next a
End Sub
如何正确实现这一点的任何帮助都将非常感激。非常感谢你。
编辑:这是正确的代码,但需要再添加一个“For Loop”来浏览另一列。我没有错误,但当我进入它时,代码跳过了'For Loop'我添加了:
Sub IndustryMatch()
Dim wBk As Workbook
Set wBk = ActiveWorkbook
Dim dbSht As Worksheet
Set dbSht = wBk.Sheets("Database")
Dim indSht As Worksheet
Set indSht = wBk.Sheets("Industry")
Dim numRowsK As String
numRowsK = dbSht.Range("K2", dbSht.Range("K2").End(xlDown)).Rows.Count
Dim g As Integer
Dim k As Integer
Dim i As Integer
For k = 1 To numRowsK + 1 ' start at first row. loop Column K
numRowsG = indSht.Range("G2", Range("G2").End(xlDown)).Rows.Count
For g = 1 To numRowsG + 1 ' start at first row. loop Column G
If (indSht.Cells(g, "G").Value = dbSht.Cells(k, "K").Value) Then ' Check if the col G value is equal to the current col K value.
dbSht.Cells(k, "J").Value = "Retail Trade"
numRowsI = indSht.Range("I2", Range("I2").End(xlDown)).Rows.Count
**For i = 1 To numRowsI + 1 ' start at first row. loop Column I
If (indSht.Cells(i, "I").Value = dbSht.Cells(k, "K").Value) Then ' Check if the col I value is equal to the current col K value.
dbSht.Cells(k, "J").Value = "Services"**
End If
Next i
End If
Next g
Next k
End Sub
答案 0 :(得分:1)
我重写了你的代码
你宣布了一些工作表对象,但你没有使用它们
指向列的指针搞砸了名称
_numRows = _行分为两个
我没有测试过您的代码,但似乎没问题
一件事......如果两列中有大量数据,代码可能需要很长时间才能运行
我将代码更改为复制到J列中与K列相同的行
Sub IndustryMatch()
Dim wBk As Workbook
Set wBk = ActiveWorkbook
Dim dbSht As Worksheet
Set dbSht = wBk.Sheets("Database")
Dim indSht As Worksheet
Set indSht = wBk.Sheets("Industry")
' dbSht.Activate ' no need for this
' Dim currentK As String
Dim numRowsK As String
numRowsK = dbSht.Range("K2", dbSht.Range("K2").End(xlDown)).Rows.Count
Dim g As Integer
Dim k As Integer
For k = 1 To numRowsK + 1 ' start at first row. loop Column K
' currentK = dbSht.Cells(k, "Category").Value ' Save the current value in column K
' indSht.Activate ' no need for this
numRowsG = indSht.Range("G2", Range("G2").End(xlDown)).Rows.Count
For g = 1 To numRowsG + 1 ' start at first row. loop Column G
If (indSht.Cells(g, "G").Value = dbSht.Cells(k, "K").Value) Then ' Check if the col G value is equal to the current col K value.
dbSht.Cells(k, "J").Value = dbSht.Cells(g, "G").Value ' If so copy to column J
End If
Next g
Next k
End Sub